-1

i have in my layout this ImageButton:

   <ImageButton
        android:id="@+id/grid_item_image"
        android:layout_width="@dimen/button_main_size"
        android:layout_height="@dimen/button_main_size"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
              >
    </ImageButton>

I don't now if is possible, but can I add text or image to this image button dynamically? I want to display an image (default image) every time, then, I want to add a number (which is variable) to the default image.

clearlight
  • 11,299
  • 11
  • 52
  • 70
DevOps85
  • 6,395
  • 6
  • 21
  • 40

7 Answers7

1

For ImageButton you cannot set the text but you can set the background drawable programmatically

ImageButton imageButton = (ImageButton) findViewById(R.id.grid_item_image);
imageButton.setBackgroundResource(R.drawable.yourimage);

If you want to change both text and background image, it is preferable to use a simple Button

button.setText("Text");
CAS
  • 545
  • 1
  • 8
  • 15
0

You cannot add a text to imagebutton try to replace it with Button where we can settext as well as image using button.setImageResource();

Karthika PB
  • 1,363
  • 9
  • 16
0

Use the following code to set an image in imagebutton dynamically:

 if (Build.VERSION.SDK_INT >= 16) {
       ivcomplete.setBackground(getResources().getDrawable(R.drawable.completed));  
   }else {
    ivcomplete.setBackgroundDrawable(getResources().getDrawable(R.drawable.completed)); 
 }

ImageButtons can't have text i.e. android:text attribute is not listed in imagebutton.For that case you can use a simple Button.

kgandroid
  • 5,459
  • 5
  • 37
  • 68
0
    ImageButton imgbtn = (ImageButton) findViewById(R.id.grid_item_image);
    imgbtn.setBackgroundResource(R.drawable.yourimage);

Or

    imgbtn.setImageBitmap(bitmap);
Prashant Bhoir
  • 885
  • 6
  • 8
0

first of all, You can not add text to ImageButton as, ImageButtons doesn't have any method as setText() and android:text property.

Alternative solution can be you can use Button instead of ImageButton and can look for the properties drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int))

You can add image though with the help of below code:

yourImageButton.setBackgroundResource(R.drawable.anyimage);
Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49
0

You should simply change xml for button like this :

<Button
 android:id="@+id/grid_item_image"
 android:layout_height="@dimen/button_main_size"
 android:gravity="center"
 android:layout_gravity="center_horizontal"
 android:padding="10dp"
 android:drawableLeft="@android:drawable/this_is_your_default_image"
 android:text="default text" />

When you want to update text default text :

button.setText("updated text");

The updated text will now appear with default image on button

YOUR              |    UPDATED

DEFAULT           |    TEXT

IMAGE             |    HERE

There is good tutorial available at : here

Kushal
  • 7,439
  • 7
  • 58
  • 76
0

Yes, As @Sleiman said you need to use Button if you want text+image. button.setBackgroundDrawable() and button.setText()

Refer this link : Android: combining text & image on a Button or ImageButton

Community
  • 1
  • 1
Bharatesh
  • 8,765
  • 3
  • 37
  • 66