0

buttons

Here are 2 screenshots, they dont have the same size because the left is from emulator the right from the device. Should have taken both from the device, sorry for this.

Both use the same drawable.

Left: background set in the layout:

        <ImageButton
        android:id="@+id/backFromOldCurves"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/backunpressed"
        android:paddingLeft="10sp"
        android:paddingRight="10sp"
        android:src="@drawable/navigationpreviousitem" />

Right: background set dynamically in onTouch on ACTION_UP:

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.backpressed));

    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.backunpressed));
        onClick(v);

    }

    return true;

}

Casting the Drawable to NinePatchDrawable in setBackgroundDrawable doesnt work. How could i work this around ?

stefple
  • 1,007
  • 17
  • 28
  • I don't know if this will help your actual question, but you should only use `sp` for text sizing. `dip` or `dp` should be used for layout sizing and padding/margins. Also you should really use an `Selector` for buttons. More info here: http://stackoverflow.com/questions/3506319/android-linearlayout-with-color-resource-what-am-i-doing-wrong/3507264#3507264 – Austyn Mahoney Jun 14 '12 at 22:25
  • I tired this with selector but it didnt work with an imagebutton. Id like to set 2 different pics for android:src and android:background . But maybe i just did the selector wrong. Will look into it again. – stefple Jun 14 '12 at 22:36
  • The Android documentation has a selector as one of its examples for ImageButton: http://developer.android.com/reference/android/widget/ImageButton.html. Also: "The order of the elements is important because they are evaluated in order. This is why the "normal" button image comes last, because it will only be applied after android:state_pressed and android:state_focused have both evaluated false." – Austyn Mahoney Jun 14 '12 at 22:38
  • Yes there you can see exactly the problem. I want to leave the right arrow (android:src="...") in place and only change the background ontouch. In order to use the selector i have to set it as android:src="". – stefple Jun 14 '12 at 22:48
  • Create an image where only the background changes and the arrow stays? – Austyn Mahoney Jun 14 '12 at 22:54

1 Answers1

1

Why you don't use

v.setBackgroundResource(R.drawable.backpressed);

?

Edit: Don't return true in onTouch(). Return false and you won't have to call onClick() when MotionEvent.ACTOIN_UP gets triggered.

public boolean onTouch(final View v, final MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        v.setBackgroundResource(R.drawable.backpressed);
    case MotionEvent.ACTION_UP:
        v.setBackgroundResource(R.drawable.backunpressed);
    default:
        Thread.sleep(50); // sleep for performance, otherwise you'd get flooded with ACTION_MOVE
    }
    return false; // return false to not consume the event
}
DragonWork
  • 2,415
  • 1
  • 18
  • 20
  • thanks for the ontouch tip, didnt know about that. i will try the setBackgroundResource method and will let you know if it worked – stefple Jun 17 '12 at 11:46
  • Did setting the image by setBackgroundResource() helped you? If so, then mark the post above as solution, please. – DragonWork Jun 17 '12 at 11:49
  • Same thing wont scale as it should, the black lines of the area which should be scaled are visible. I redesigned the App and dont need this anymore. If you edit your question to something like: Seems like Nine Patches will only be scaled right when used in xml with android:background="", i will accept it, so others see the right answer. – stefple Jun 18 '12 at 11:54
  • Sorry, I can't. This won't be correct. It seems like you have an error in your code. I'm also setting NinePatchDrawables programmatically. And however, it works for me. How about using `ImageView` instead of `ImageButton`? (But in both cases, the code above _should_ work.) – DragonWork Jun 18 '12 at 12:22
  • Ah ok i thought it would never scale correctly. Then i will try the above again and report back, thanks for your help. – stefple Jun 18 '12 at 15:43