Reputation: 342
Hi I Want To Change TextView Text Each Time I Click A button. For e.g. first Click It Change the text to "Hi" Second Time "Bye" Third Time "Again ?" And Make It A loop. How Can I ?
Upvotes: 0
Views: 94
Reputation: 468
Try this
int count =0;// Global Variable
@Override
protected void onCreate(Bundle savedInstanceState) {
Button btn =findViewById(R.id.button);
btn.setOnClickListener(this::when_clicked);
TextView txt = findViewById(R.id.text);
}
//when_clicked funtion
public void when_clicked(){
String set_text;
if(count%3==0) set_text= "Hi";
if(count%3==1) set_text= "Bye";
if(count%3==2) set_text ="Again";
txt.setText(set_text);
count++;
}
Upvotes: 1
Reputation: 29
Have you tried something?
You can accomplish it by using a counter and a list of strings. Here's a starting point:
int counter = 0;
List<String> messages = new ArrayList<>();
// populate your list
messages.add("Hi there");
......
......
......
// set the click listener
myButton.addOnCLickListener(v -> {
if(counter >= messages.size())
counter = 0;
myTextView.setText(messages.get(counter));
counter++;
});
Upvotes: 1