0

I am new to java/android and am making a test app. It has ImageButtons that when clicked switch to a different image temporarily. The originals are cropped using

android:adjustViewBounds="true"
android:scaleType="centerCrop"

in the activity_main.xml The problem is the second image isnt cropped and is therefore too big for the button. DOes anyone know how I can fix this? HEres a an example of one of the buttons:

public void onClick(View v) {

        //switch to second img   
butt2.setImageResource(R.drawable.newimg);

//switch back to first after pause

new Handler().postDelayed(new Runnable() {

   public void run() {

       butt2.setImageResource(R.drawable.orig);


   }

}, 500L);  

            }     
    });
user718229
  • 524
  • 6
  • 20
  • Possible duplicate? http://stackoverflow.com/questions/13639773/crop-a-drawable-image-by-android-gallery-cropping-method – Manoj Awasthi Aug 20 '13 at 19:24
  • Sounds like you should be using a selector (i.e. `StateListDrawable`) instead of switching the image manually on the click event. – Alex MDC Aug 20 '13 at 19:28

2 Answers2

2

I have used the following

int normal[] = { R.drawable.num0, R.drawable.num1, R.drawable.num2,
            R.drawable.num3, R.drawable.num4, R.drawable.num5,
            R.drawable.num6, R.drawable.num7, R.drawable.num8,
            R.drawable.num9, R.drawable.del, R.drawable.go };
    int pressed[] = { R.drawable.num0_clicked, R.drawable.num1_clicked,
            R.drawable.num2_clicked, R.drawable.num3_clicked,
            R.drawable.num4_clicked, R.drawable.num5_clicked,
            R.drawable.num6_clicked, R.drawable.num7_clicked,
            R.drawable.num8_clicked, R.drawable.num9_clicked,
            R.drawable.del_clicked, R.drawable.go_clicked };
                  StateListDrawable sld;
                  for (int i = 0; i < 12; i++) {
            sld = new StateListDrawable();
            sld.addState(new int[] { android.R.attr.state_pressed },
                    getResources().getDrawable(pressed[i]));
            sld.addState(new int[] {}, getResources().getDrawable(normal[i]));
            btns[i].setImageDrawable(sld);
        } 
Aditya Rai
  • 139
  • 8
0

There's another way to attempt this thing , if you want like when you click the button its background should change and when you release it , background gets to previous one .

This can be achieved as follows

Now in your main.xml file that you want to show as an activity and where you have declared the button tag , add a statement

android:background="@drawable/customDefaultBackground"

and it will be like

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/customDefaultBackground" />

Now add an xml file of selector tag type and name it as customDefaultBackground.xml, open that file and add following to its contents :-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:state_pressed="true" android:drawable="@drawable/onPressBackground" ></item>
 <item android:drawable="@drawable/defaultBackground"></item>
</selector>

Item tag with android:state_pressed set to true , denotes that when a user clicks on a button and keep it pressed the backgroundOnPressed version will be displayed else it will show default background.

Udit Kapahi
  • 2,277
  • 1
  • 27
  • 25
  • would you need a separate xml file for each button? If they have different images – user718229 Aug 21 '13 at 13:40
  • Yes you should have different xmls for each because you are going to set different background for each button. If you want to have same background for each button you can use a single xml. – Udit Kapahi Aug 21 '13 at 13:50