Ammu Naveen
Ammu Naveen

Reputation: 1

Getting text from Dynamically created button :Android

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.

Upvotes: 0

Views: 595

Answers (1)

YuviDroid
YuviDroid

Reputation: 1546

The View in onClick() is your Button.

Just downcast it:

Button btn = (Button) arg;
String btnText = btn.getText();

Upvotes: 2

Related Questions