DEV Community

Grace
Grace

Posted on

Android studio fragments

I really need help for my HW, I'm not good with java but I need to pass this class to graduate. How do I send input text from an EditText (in FragmentOne) to a TextView (in FragmentTwo)? Also, "transfer" the color of a button (FragmentOne) to an ImageView (FragmentTwo), how could I do that?

In summary, I need it to show the text I wrote in FragmentTwo when I click a button and the ImageView should change color depending wich button I clicked.

I tried watching tutorials and reading answers but I don't understand what to do, I know how to do with if they were normal classes but with fragments I have no idea, any help I can get I would be extremely thankful!

Thanks for reading!

Main Activity

public class MainActivity extends AppCompatActivity implements
FragmentOne.OnFragmentInteractionListener,
FragmentTwo.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
FragmentOne fragment = FragmentOne.newInstance("HOLA", 0);
fragmentTransaction.add(R.id.fragmentOne, fragment);
fragmentTransaction.commit();
}
@Override
public void onFragmentInteraction(Uri uri) {

}

@Override
public void onFragmentInteraction(String message, int colorid) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
    FragmentTwo fragment = FragmentTwo.newInstance(message, colorid);
    fragmentTransaction.replace(R.id.fragmentTwo, fragment);
    fragmentTransaction.commit();
}

}

FragmentOne

public class FragmentOne extends Fragment {

EditText editText;
Button buttonFragmentOne;
Button button;
Button button2;
private ImageView imageView;
private int colorid;

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private int mParam2;

private OnFragmentInteractionListener mListener;

public FragmentOne() {
    // Required empty public constructor
}

public static FragmentOne newInstance(String param1, int param2) {
    FragmentOne fragment = new FragmentOne();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putInt(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}
private void changeColor(){
    imageView.setColorFilter(ContextCompat.getColor(getContext(), colorid));
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getInt(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);
    editText= rootView.findViewById(R.id.editText);
    button = rootView.findViewById(R.id.button2);
    imageView = rootView.findViewById(R.id.imageView);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            colorid = android.R.color.holo_red_dark;
            changeColor();
        }
    });
    button2 = rootView.findViewById(R.id.button3);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            colorid = android.R.color.holo_orange_dark;
            changeColor();

        }
    });

    buttonFragmentOne = rootView.findViewById(R.id.buttonFragmentOne);
    buttonFragmentOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onButtonPressed(null);
        }
    });
    // Inflate the layout for this fragment
    return rootView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(editText.getText().toString(), colorid);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}


@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(String message, int colorid);
}

Fragment Two

public class FragmentTwo extends Fragment {

TextView textView;


private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private int mParam2;

private OnFragmentInteractionListener mListener;

public FragmentTwo() {
    // Required empty public constructor
}

public static FragmentTwo newInstance(String param1, int param2) {
    FragmentTwo fragment = new FragmentTwo();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putInt(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getInt(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_two, container,
            false);
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

Top comments (2)

Collapse
 
thefern profile image
Fernando B 🚀 • Edited

I'm on mobile kind of hard to read your code, but there are several ways to do this. The easiest way is probably to write listener interface and then get the data on parent activity to send to second fragment.

A couple of links to help you:

youtu.be/i22INe14JUc

journaldev.com/14207/android-passi...

By the way writing and implementing interfaces is not just for Android, so you could look for more tutorials without Android keyword.

Collapse
 
gracesros profile image
Grace

ohhh I had no idea, all of this time I was googling my questions wrong

Thank you so much, I will try to follow the tutorials :D