8

I am inflating an xml having button, multiple times and i am able to do so perfectly but the problem is when I click the button,i want to show which button is clicked.

   public class InflateExActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    Button b;


    LinearLayout lLayout;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for (int i = 0; i < 3; i++) {

             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             b = (Button) inflater.inflate(R.layout.buttons, null);
            t = (TextView) inflater.inflate(R.layout.texts, null);


            b.setTag(i); // you'll get 0,1,2 as

            lLayout = (LinearLayout) findViewById(R.id.layout1);
            lLayout.addView(b);

            b.setOnClickListener(this);

        }

    }

    public void onClick(View v) {

        }

}
Rookie
  • 8,660
  • 17
  • 58
  • 91

3 Answers3

6

You can use setTag() to each button. Inside the for loop you can assign button.setTag(). And you can use getTag() to retrieve button's tag. After you inflate the layout, add a tag to your button

EDIT2: You should inflate the layout and then look up for your button id. See below:

    public class InflateExActivity extends Activity {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                LinearLayout lLayout;
                final Button b = null;
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                for(int i=0;i<3;i++){
                    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.buttons, null);
b = v.findViewById(R.id.your_button_id);
    //                 b = (Button) inflater.inflate(R.layout.buttons, null);
                       b.setTag(i); // you'll get 0,1,2 as tags
                    lLayout = (LinearLayout) findViewById(R.id.layout1);
                    lLayout.addView(b);
                    b.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
    int specificButton = (Integer)v.getTag();//Changed here.......
                            Toast.makeText(InflateExActivity.this, "Button Clicked"+Integer.toString(specificButton),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }  
        }
Rookie
  • 8,660
  • 17
  • 58
  • 91
Radu Dan
  • 453
  • 4
  • 22
  • Hey this also worked..We were doing one mistake..in onClick, instead of b.getTag().....it should be v.getTag(); – Rookie Mar 24 '12 at 13:16
  • i edited it..please accept..so that i can mark it as upvote to show this is also the right method.. – Rookie Mar 24 '12 at 13:19
4

items you are adding programmatically, you must have to assign ids to them.

b.setId(1);

EDITED:

public class DynamicLayoutActivity extends Activity implements OnClickListener{
private static final int MY_BUTTON = 9000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);

    // add button
    Button b = new Button(this);
    b.setText("Button added dynamically!");
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(this);
    ll.addView(b);
}
 public void onClick(View v) {
        Toast toast;
        Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
        switch (v.getId()) {
        case MY_BUTTON:
            toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();    
        }
    }

LATEST:

public class InflateExActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        LinearLayout lLayout;
        Button b = null;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int i=0;i<3;i++){
            final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             b = (Button) inflater.inflate(R.layout.buttons, null);
             b.setId(i);
            lLayout = (LinearLayout) findViewById(R.id.layout1);
            lLayout.addView(b);
            b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Toast.makeText(InflateExActivity.this, "Button Clicked :"+v.getId(),
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}  
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
3

Use the view tag

View.setTag(Object tag);

You can set a string or a complex object like a class to the tag.

triggs
  • 5,890
  • 3
  • 32
  • 31