<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Grace</title>
    <description>The latest articles on DEV Community by Grace (@gracesros).</description>
    <link>https://dev.to/gracesros</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F252954%2Fefb5b4e6-9318-4fc1-a9e8-37a810c2c441.png</url>
      <title>DEV Community: Grace</title>
      <link>https://dev.to/gracesros</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gracesros"/>
    <language>en</language>
    <item>
      <title>Android studio fragments </title>
      <dc:creator>Grace</dc:creator>
      <pubDate>Sat, 19 Oct 2019 22:29:53 +0000</pubDate>
      <link>https://dev.to/gracesros/android-studio-fragments-3jmc</link>
      <guid>https://dev.to/gracesros/android-studio-fragments-3jmc</guid>
      <description>&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Main Activity&lt;/p&gt;

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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}

@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();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;FragmentOne&lt;/p&gt;

&lt;p&gt;public class FragmentOne extends Fragment {&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Fragment Two&lt;/p&gt;

&lt;p&gt;public class FragmentTwo extends Fragment {&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>help</category>
      <category>android</category>
    </item>
  </channel>
</rss>
