Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change button text in code for Android app

This is an extremely basic question and I'm embarrassed I haven't been able to find the solution online.

I would like to be able to update the text of my button in code. I am using the new Android Studio IDE. I created a blank activity when starting the project and placed a Button in activity_main.xml. How do I access this button in code to change its properties? I am trying in the mainactivity.java file but the intellisense isn't giving me anything that looks promising.

like image 427
Ben Walker Avatar asked Dec 01 '22 21:12

Ben Walker


2 Answers

Button button = (Button)findViewById(R.id.button);
button.setText("Click Me !");

I hope this helps you.

like image 80
Vinuthan Avatar answered Dec 03 '22 12:12

Vinuthan


// Clean the code then

Button button = (Button)findViewById(R.id.button);

like image 42
Pawan Yadav Avatar answered Dec 03 '22 12:12

Pawan Yadav