10

How can I prevent an ImageButton from having a fixed size? The drawables used in the selector have different size, and I want the ImageButton to resize itself to match the image size.

I've tried adjustViewBounds without success.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp" >

    <ImageButton
        android:id="@+id/picture_imagebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/tab_background_selector" />

</RelativeLayout>
Nick
  • 4,302
  • 2
  • 24
  • 38
gdelente
  • 776
  • 8
  • 14

6 Answers6

6

Instead of using ImageButton, use ImageView because ImageButton cannot be resized itself based on ImageSize. ImageView is the best alternative way that i can suggest for your problem. Acheive that by the following way:

layout.xml:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/image_selection"/>

 </RelativeLayout>

image_selection.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    
   <item android:state_focused="true" 
       android:state_pressed="true" 
       android:drawable="@drawable/default_image"/> //default_image.png is displayed when the Activity loads.   
   <item android:state_focused="false" 
       android:state_pressed="true" 
       android:drawable="@drawable/default_image_hover" />  //default_image_hover.png is an image displaed during Onclick of ImageView  
   <item android:state_focused="true"
       android:drawable="@drawable/default_image_hover" />   
   <item android:state_focused="false" 
       android:drawable="@drawable/default_image"/>
 </selector>

SampleActivity.java:

  ImageView myImage= (ImageView)findViewById(R.id.image);
    myImage.setOnClickListener(this);
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
0

As you may have already know there is not an easy way to set drawables in a selector, and I don't think there is an XML based solution to your problem. Try this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageButton 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/default_image"
        android:contentDescription="@null"/>

</RelativeLayout>

Then you need to add this code to onCreate or onResume

    final ImageButton button = (ImageButton) findViewById(R.id.button);

    button.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
                button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("pressed_image", "drawable", getPackageName())));
            else if(event.getAction() == MotionEvent.ACTION_UP)
                button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("default_image", "drawable", getPackageName())));

            return false;
        }
    });
akaya
  • 1,140
  • 9
  • 27
  • 1
    You're basically doing programmatically what is done by the selector, which is not elegant. I would really like an XML based solution. – gdelente Feb 21 '13 at 19:52
  • @gdelente I know it is not elegant, but this is not the same with selector. Selector doesn't resize your ImageButton. Also if you look for an XML solution only, you have to state it in your question. – akaya Feb 26 '13 at 12:35
0

Don't use an ImageButton, just use an Image an supply it with an onClick method

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
0

As others have mentioned, you'll be best of using an ImageView as it will adjust its height. Set the property setClickable to use it like a button. Also swap your RelativeLayout for a LinearLayout, this will wrap its height to your ImageButton:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp" >

<ImageView
    android:id="@+id/picture_imagebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:setClickable="true"
    android:src="@drawable/tab_background_selector" />

</LinearLayout>

Alternatively you can make the LinearLayout clickable and add a background resource (transparent when not clicked, colored when clicked) to act as a selector.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Ljdawson
  • 12,091
  • 11
  • 45
  • 60
0

Try

android:background="@drawable/tab_background_selector" 

insted of

android:src="@drawable/tab_background_selector"

hope that helps.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
0

You can use styles and set different styles based on the state of the ImageBUtton. Because styles support background, size and many other properties that helps you to get full control of styling depending on problem at hand.