matrix
matrix

Reputation: 11

how can change textview of class with button?

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??

Upvotes: 1

Views: 62

Answers (3)

Jorgesys
Jorgesys

Reputation: 126455

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);
        }

}

Upvotes: 0

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

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);
        }
    }
}

Upvotes: 0

GuilhE
GuilhE

Reputation: 11861

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);
}

Upvotes: 1

Related Questions