0

I want to create a dynamic button and have to access it in action listener. I mean to say the id of it and have to perform required action.Can any one help me please

package com.check.widget;

import android.app.Activity;
import android.widget.*;
import android.os.Bundle;

 public class DynamicWidgetActivity extends Activity {
  /** Called when the activity is first created. */
 @Override
   public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int size=10;
    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    TextView tv = new TextView(this);
    tv.setText("Dynamic layouts ftw!");
    ll.addView(tv);

    EditText et = new EditText(this);
    et.setText("weeeeeeeeeee~!");
    ll.addView(et);

    Button buttons[] = new Button[size];             
    for(int i=0;i<size;i++)
    {
        buttons[i]=new Button(this);
        ll.addView(buttons[i]);

    }
}

}

sai sindhu
  • 1,155
  • 5
  • 20
  • 30

1 Answers1

1

Look at this: Android: How to programmatically add button to view

As far as accessing the Button Id. It probably wont have one unless you set it.

Community
  • 1
  • 1
slayton
  • 20,123
  • 10
  • 60
  • 89