0

I'm new to Android app programming.

I have two Fragments - fragmentA.xml and fragmentB.xml.

In fragmentA there are two Buttons - Button One (id: btnOne) and Button Two (id: btnTwo).

In fragmentB there is TextView (id- textView)

In fragmentB java class there are two String - string1 and string2

Both buttons refers to fragmentB.

I want two set the textView text to string1 when I press Button One and string2 when I press Button Two.

How can I do that?

I was using following code in fragmentB java class to display only string1

public View onCreateView(
    LayoutInflater inflater, 
    ViewGroup container, 
    Bundle savedInstanceState) 
{
    View rootView = inflater.inflate(R.layout.oop_page,container,false);
    TextView textV = (TextView)rootView.findViewById(R.id.textView);
    textV.setText(string1);
    return rootView;
}

Can I use any if-else statement here?

Thanx

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
MaxySpark
  • 555
  • 2
  • 10
  • 26
  • "Both Buttons refer to FragmentB" what does this mean? – thedarkpassenger Feb 03 '16 at 16:34
  • make only one string in your global class (accessible to both fragment) and set it to your textview,....just change string on button click it will be automatically disply changed one... – Iamat8 Feb 03 '16 at 16:34
  • @AnshulJain I use a method to open the FragmentB and I am using the method in Button One and Button Two's onClick method – MaxySpark Feb 03 '16 at 16:41
  • have you tried what i have suggested – Iamat8 Feb 03 '16 at 16:45
  • @Mohit you mean i should declare a string in MainActivity.java . can you provide me the code. and what if there is more than two botton. I mean for every button different string. – MaxySpark Feb 03 '16 at 16:50
  • it deos not matter how many button you have,you will need only one string till you want to display one... – Iamat8 Feb 03 '16 at 16:53
  • let me test. I'll inform you. – MaxySpark Feb 03 '16 at 17:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102501/discussion-between-mohit-and-maxyspark). – Iamat8 Feb 03 '16 at 17:15

2 Answers2

2

Create a Global class in your project and define a public string ..

public class Global{
  public static String TEXT = "";
}

In your Fragment A change this TEXT on button click event..

but1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Global.TEXT = "string 1";
    }
});
but2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Global.TEXT = "string 2";
    }
});

same as on other button if you have

In your FragmentB just setText()....it will take lase value change to Text

text.setText(Global.TEXT);
Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • it worked. can I use string from string.xml resource file? – MaxySpark Feb 03 '16 at 17:38
  • i would not suggest that because you can get what i stored in string but you can modifiy during run time..have a look at [this](http://stackoverflow.com/questions/9674925/change-value-of-r-string-programically) post... – Iamat8 Feb 03 '16 at 17:43
0

FragmentA.java

public boolean btn1Clicked, btn2Clicked = false;
        Button button1 = (Button) rootViewfindViewById(R.id.button1);
            button1.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                btn1Clicked = true;
              }
            });

            Button button2 = (Button) rootView.findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                btn2clicked = true;
              }
            });

FragmentB.java

 public View onCreateView(
        LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) 
    {
        View rootView = inflater.inflate(R.layout.oop_page,container,false);
        TextView tv1 = (TextView)rootView.findViewById(R.id.textView);
        if(!(FragmentA.btn1Clicked))
         {
        // Not Clicked
         }
        else{
           tv1.setText("Button 1 Clicked!")'
    }
        return rootView;
    }
Sathish Kumar
  • 919
  • 1
  • 13
  • 27