How to change text on Button click ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to change text on Button click ?

in android studio I have Button and Textview I need if i press button change text to(this is text 1) and if i press again change to (this is text 2) and if i press again change to (this is text 3 ) Provided that the texts are mixed and not arranged Thanks to everyone who is interested

9th Sep 2017, 8:19 AM
Mohammed Elhafed Messini
Mohammed Elhafed Messini - avatar
1 Answer
+ 10
Button btn = (Button) findViewById(R.id.my_button); TextView textView = (TextView) findViewById(R.id.my_textview); int count = 1; btn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { textView.setText("this is text " + count++); } }); Where my_button is the id for your button and my_textview is the id for your TextView in your corresponding layout.xml file. As far as changing the TextView's text each time the button is pressed goes, more info is needed. Are you storing the text strings in the strings.xml file? Are you going to store them in an array? Are they random? Do they have an actual incrementing number like they do in your example or was that just to show that they change? In the example code above the count increments each time the button is pressed.
9th Sep 2017, 8:46 AM
ChaoticDawg
ChaoticDawg - avatar