1

I want to create ImageButton with custom background, custom Icon and text below that icon

What I have so far is

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="140dp"
    android:layout_height="120dp"
    android:layout_marginLeft="3dp"
    android:layout_marginBottom="6dp"
    android:background="@drawable/btn_rectangle_background"
    android:src="@drawable/ic_action_search"
/>

However, If I put there android:text="blablabla" it won't shows up :/

ic_action_homework is .PNG icon, but btn_rectangle_background is XML file, which defines shape

That's what I would like to achieve enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
jpact
  • 1,042
  • 10
  • 23
  • Take a look at this [Post](http://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton) – M D May 31 '14 at 14:52
  • Thanks for reply, however it still wont solve my problem, as in my case, background and icon are two separate "images" :( So what I need to do, is set background on ImageButton, and then, on that background set an icon with text. – jpact May 31 '14 at 14:59
  • 1
    You can do it all in a layout file with one button and with no code - check my answer. – iosjdjoisdfijodjoi893 May 31 '14 at 15:37

1 Answers1

1

1st answer:

Must be structure of layout likes here:

<LinearLayout
   android:widht_layout="80dp"
   android:height_layout="80dp"
   android:padding="10dp"
   android:gravity="center"
   android:layout_gravity="center"
   android:bacgkground="your_color ARGB"
   >
    <ImageView />
    <TextView />
</LinearLayout>

or 2nd answer:

Create custom view

public class customView extends View{
   public customView(Context context){
       super(context);
   }
   public customView(Context context, String s, Drawable d){
       super(context);

       // Set Width&Height for this view
       this.measure(80,80);
       // or layout params with specified height&width for this view
       Resources r = getResources();
       int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **Your width**,
            r.getDisplayMetrics());
       int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **your height**,
            r.getDisplayMetrics());
       ViewGroup.LayoutParams pp = new ViewGroup.LayoutParams(width,height);
       this.setLayoutParams(pp);


       TextView _text = new TextView(context);
       ImageView _image = new ImageView(context);
       _text.setText(s);
       _image.setBackground(d);
       this.addView(_image);       
       this.addView(_text);

   }
   public customView(Context context, String s, Bitmap b){
     ....
     _image.setImageBitmap(b);
     ...
   }  
}

also add view into root view #id=content of layout from activity:

findByView(R.id.content).addView(new customView((Context)this,"Your Text",getResources().getDrawable(R.drawable.icon));

or with parametr bitmap by path:

findByView(R.id.content).addView(new customView((Context)this,"Your Text",BitmapFactory.decodeFile("/sdcard/file.png"));
machei
  • 337
  • 2
  • 16