Ashu Kumar
Ashu Kumar

Reputation: 832

change TextView Text after Button click in android (OnClickListener)

i m trying to make a function in which array will show on TextView and chnage the Text in TextView after onCLickListener()

can u please give Suggestion how can i change the Text in TextView after clicking on Button and do some Specific work in onCLickListener()

 static int count;
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
    				{
    	count=0;
    	String[] qlist = { "Where is Captial of India", "Captial Of America"};
    	TextView show_q =(TextView) findViewById(R.id.textView2);
    	if(count <qlist.length){
    	show_q.setText(qlist[count]);
    	count= count+1;
    	}
    	else{
    	// reset count here
    	count=0;
    	}
   }
    				
});

thanks in Advance

Upvotes: 0

Views: 3664

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

how can i change the Text in TextView after clicking on Button:

Use an counter variable to get item from Array on every Button click:

    Button button=(Button)findViewById(R.id.btnid);
    int count=0;
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             if(count <qlist.length)
              show_q.setText(qlist[count]);
                count++;
             }else{
                  // reset count here
                 count=0;
              }
        }
    });

Upvotes: 1

droidd
droidd

Reputation: 1371

I am attaching a sample code using your code. Please use according to your requirements.

Button button=(Button)findViewById(R.id.clickme);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                final String[] qlist = { "Where is Captial of India", "Captial Of America", "India Gate ", "Residence Eviel", "Chiti " };

                TextView show_q =(TextView) findViewById(R.id.textView2);
                StringBuilder builder = new StringBuilder();
                for (String s : qlist){
                builder.append(s+" ");
                show_q.setText(builder.toString());
                }

            }
        });

If i understand your question wrong, Please comment.

Upvotes: 1

Related Questions