0

I have just started working with fragments in android. In my main layout, I have two buttons. When I click one button it displays the fragment (which is a piece of text). Now what I want is that when I click the other button, it should change the color of that text inside the fragment. Is that possible ? If yes, then kindly give me an example code. Because I could not find it on internet.

Thank you.

Vishal Chhatwani
  • 167
  • 1
  • 4
  • 14

2 Answers2

0

You can achieve it by:

Bundle b = getIntent().getExtras();
b.putExtra("Color" , "Red");
Fragment_2 f2 = new Fragment_2();
f2.setArguments(b);

and in your fragment, onCreateView() you can get it as:

String color = getArguments().getString("Color");

Now, set that color in your textView or related views.

Kinnar Vasa
  • 397
  • 1
  • 9
0

If you have two classes; one which is your main layout or activity that displays fragment on click of button and another class which is your fragment then make a method in your fragment class

public void changeColor(){textView.setColor(xxx);}

And call that method from your other class at button click like this

new YourFragmentClass().changeColor();
rusted brain
  • 1,052
  • 10
  • 23
  • This looks quite straight forward. I would like to use this method. But I cant use this "FindViewbyID" in fragment class. How do i declare TextView there? – Vishal Chhatwani Jul 16 '15 at 10:28