Question

I've stored the imagebutton info in an xml. It's image path,width,height and so on. But since there are multiple images I want to store data for each imagebutton while it's clicked. for example, if there are two buttons named male and female, i want to set String value = "male"; if male image button is clicked. But it did not work. Can somebody help me?

 public void AddAllImageButtons() throws IOException {
        AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
        ImageButton btn = new ImageButton(this);
        Layout.addView(btn);
        AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn
                .getLayoutParams();

        absParams.x = x;
        absParams.y = y;
        btn.setLayoutParams(absParams);
        btn.setBackgroundDrawable(null);
        btn.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(path)));

        if(type.equals("imagebutton")){
        if(elementId.equals("female")){
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                gender = elementId;
                GenerateAlertBoxes(gender);
            }
        });
}

Id is the image button id which is created in xml file. AlertBox text does not show "female" for this example.

Was it helpful?

Solution

you can setTag("value") to set the value in the ImageButton btn and on the onclick event you can use getTag() function to retrieve the value and set it to gender.

use btn.setTag(elementid); whenever you add a btn to the layout.......

inside btn.setOnClickListener you can write v.getTag().toString() to get the value and set it to gender...... 

btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                gender = v.getTag().toString();
                GenerateAlertBoxes(gender);
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top