1

I have Button 1 and Button 2 . I also have 2 TextViews and i have 1 Activity named B(Buttons are in Activity A).

I want that onclick on Button A, TextView A will show in Activity B,when click on Button B text2 shows in Activity B. like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_A);
    Button btn1 = (Button) findViewById(R.id.button1);


    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub

            Intent i = new Intent(getBaseContext(), B.class);
            TextView tv= (TextView) findViewById(R.id.text1);

            startActivity(i);

        }
    });

    Button btn2 = (Button) findViewById(R.id.button2);


    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            Intent i = new Intent(getBaseContext(), B.class);
            TextView tv= (TextView) findViewById(R.id.text2);

            startActivity(i);


        }
    });

keep it mind i have 23 buttons like these and 23 text.How can I do it?

how can i do it with public static final string??

matrix
  • 11
  • 3
  • Possible duplicate of [how to change text in Android TextView](http://stackoverflow.com/questions/2300169/how-to-change-text-in-android-textview) – Chris Stillwell Feb 10 '16 at 22:49

3 Answers3

1

By class you mean Activity and what you want to do is send the text from Activity A to Activity B so that you in Activity B know which button you have clicked to enter in Activity B, am I correct? Let's assume I am, you have to put an extra in that intent your using to call the Activity B and inside Activity B retrieve that intent String extra.

Something like:

Activity A:

    Intent i = new Intent(getBaseContext(), b.class);
    TextView tv= (TextView) findViewById(R.id.text1);
    i.putExtra(ActivityB.CALLER, tv.getText());
    startActivity(i);

Activity B:

public static final String CALLER = "caller";

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.your_activity_b_layout);

     String buttonText = getIntent().getStringExtra(CALLER);
}
GuilhE
  • 11,591
  • 16
  • 75
  • 116
  • my text is in my strings.xml not ActvityA,.....some friean say use public static final string,,,,i dont kown how use it,can u say me how.tnx – matrix Feb 10 '16 at 23:22
  • But you use that text inside your layout.xml as a R.id.text1 attribute right? If so, tv.getText() will give you the text you want. – GuilhE Feb 10 '16 at 23:38
0

The way you would do this is to simply trigger some sort of flag in Class A when Button A is clicked. Then when you start Class B, you would pass an Intent extra that notifies Class B that it should show TextView A. So it would go something like this.

In Class A, you would need to listen for Button A clicks, and set the appropriate variable that you will later send to Class B.

public class A extends AppCompatActivity {

    private boolean buttonWasClicked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_activity);

        buttonWasClicked = false;

        Button buttonA = (Button) findViewById(R.id.buttonA);
        buttonA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonWasClicked = true;
            }
        });

    }
}

Then when you start your Class B, you would pass a boolean extra with the intent to notify that buttonA had been clicked. Like this...

Intent intent = new Intent(this, B.class);
intent.putExtra("WasButtonClicked", buttonWasClicked);
startActivity(intent);

Then when B starts, you would retrieve the extra and see if buttonA was clicked. If it was, show the TextView, if not, hide it.

public class B extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b_activity);

        TextView textViewA = (TextView) findViewById(R.id.textViewA);

        boolean buttonWasClicked;
        Bundle args = getIntent().getExtras();
        if(args != null) {
            buttonWasClicked = args.getBooleanExtra("WasButtonClicked", false);
        }

        if(buttonWasClicked) {
            textViewA.setVisibility(View.VISIBLE);
        } else {
            textViewA.setVisibility(View.INVISIBLE);
        }
    }
}
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
0

From Activity A, if your button was clicked, send the info via bundle:

Activity A)

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {            
     Intent intent = new Intent(this, B.class);
     intent.putExtra("buttonClicked1", true);
     startActivity(intent); 
    }
});

Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     Intent intent = new Intent(this, B.class);
     intent.putExtra("buttonClicked2", true);
     startActivity(intent);
    }
});

In Activity B, receive a boolean value indicating what was the selected button , buttonClicked1 or buttonClicked2, then set the corresponding text from the Strings.xml file.

Activity B)

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.your_activity_b_layout);

     boolean buttonClicked1;
     boolean buttonClicked2;
        Bundle args = getIntent().getExtras();
        if(args != null) {
            buttonClicked1 = args.getBooleanExtra("buttonClicked1", false);
            buttonClicked2 = args.getBooleanExtra("buttonClicked2", false);
        }

    if(buttonClicked1) {
       //Load text from Strings.xml for button1.
       String buttonText =    getApplicationContext().getResources().getString(R.string.yourText1);
               textViewA.setVisibility(View.VISIBLE);
 } else  if(buttonClicked2) {
       //Load text from Strings.xml for button1.
       String buttonText =    getApplicationContext().getResources().getString(R.string.yourText2);
            textViewA.setVisibility(View.VISIBLE);
        }

}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268