Jiří Polesný Jiří Polesný - 2 months ago 13
Android Question

Change button text over other button android

I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.

NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);

final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//Set Text on button click via this function.

finalNewText1.setText(" (Frohe Weihnachten) ");

}
});

A.J A.J
Answer

Same concept as you did for textView

Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);

ChangeText.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        //This changes the text on the second button

        SecondButton.setText("New Text Here");

    }
 });

SecondButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        //Do anything

    }
 });
Comments