0

I created buttons dynamically, which is assigned to url address obtained from handlers as settext(). But I am not able to get text of that button,if it is clicked,since arg.gettext() is not working in OnClickListener. Is there any way to get text of the button which is created dynamically

      for ( i = 0; i <itemList.getTitle().size()-1; i++) {
    title[i] = new TextView(this);
    title[i].setTextColor( -16711936 );
    title[i].setTextSize(18);
    title[i].setText("Title = "+itemList.getTitle().get(i));
    description[i] = new TextView(this);
    description[i].setTextColor(-16776961);
    description[i].setText("Description = "+itemList.getDescription().get(i)+"......");
    more[i]=new Button(this);
    more[i].setText(itemList.getLink().get(i));






    layout.addView(title[i]);
    System.out.println("Title view is set");
    layout.addView(description[i]);
    //System.out.println("Description view is set");
    layout.addView(more[i]);




    more[i].setOnClickListener(listener);


}

private OnClickListener listener=new OnClickListener(){

    public void onClick(View arg) {
        // TODO Auto-generated method stub
          String value=(should get the text of the selected button)
               }

Any help would be greatly appreciated.

1 Answer 1

2

The View in onClick() is your Button.

Just downcast it:

Button btn = (Button) arg;
String btnText = btn.getText();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.