Search code examples
androidandroid-studiobuttontextview

Changin a textview text each time clicking a button


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 ?


Solution

  • 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++;
    }