0

In Android, whenever I click a button (namely an ImageButton), I want to give the user feedback by changing the color of the button (maybe inverting colors, or darkening, etc). At this point, I don't want to change to a new button or anything, just want to change state so as to reflect the click.

I realize I can add a new drawable, so each button would have 2 states (when clicked, I would change to the second state).

That is (pseudocode):

onClick {

 ImageButton.setDrawable(R.drawable.myclickedbutton) 

}

Is there a better way to do this (I believe iOS will play with ImageColors for you when you click, and that's what I want here in Android), or is what I am thinking the only possibility?

I feel I'm missing something major with this concept.

Thanks in Advance.

Edit: If it makes a difference, my app runs on minimum target 2.2.

Edit 2: For clarification, I'm working with an Image Button which already has a custom drawable resource. That's what I want to manipulate onClick.

Edit 3: I don't think I'm being clear enough here. I know how to change the state using two drawables (whether it be onTouch, XML, onClick, etc.) I was asking if there was a way that Android (or some sort of method) can invert or darken colors for all buttons automatically when clicked (as iOS does, I believe). I am not asking how to do this on a button-to-button basis. This can be accomplished with 2 drawables per button, and I realize that.

3 Answers 3

2

button.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="5dp"/> <solid android:color="#f00"/> <padding android:left="10.0dip" android:top="10.0dip" android:right="10.0dip" android:bottom="10.0dip"/> <stroke android:width="1.0dip" android:color="#222"/> </shape> </item> <item android:state_pressed="false"> <shape android:shape="rectangle"> <corners android:radius="5dp"/> <solid android:color="#f1f1f1"/> <padding android:left="10.0dip" android:top="10.0dip" android:right="10.0dip" android:bottom="10.0dip"/> <stroke android:width="1.0dip" android:color="#222"/> </shape> </item> </selector> 

Apply this button as background

<Button android:background="@drawable/button"/> 

and in your class file do like this

public void onClick(View v) { pressedButton.setPressed(true); } 

so that the red color will be stable

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure if this is what I want-- I don't want the button to have a single color (which is changed when clicked). It's an ImageButton which should be changed on click. It does already have a custom drawable source, the colors of which I would like to (invert, darken, etc.) on click to reflect the action.
0

You dont have to use the code you mentioned in order to switch the drawables . Rather create a drawable with two different states.

As you said you already have custom drawables, you can use them for different states.

<?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/drawable1" /> <!-- for clicked state--> <item android:drawable="@drawable/drawable2" /> <!-- for normal state--> </selector> 

place this xml as drawable_1.xml in drawable folder and use this drawable in your code,

yourButton.setDrawable(R.drawable.drawable_1) 

1 Comment

I am aware of this method, just wondering if there's a workaround of using 2 drawables per button. As of now, guess not.
0

You can use onTouchListener for button and change the state of button according to button click..

 button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN) { // here u can change the button color or image as clickable as button.setBackgroundImage(R.drawable.image_pressed); } if(event.getAction()==MotionEvent.ACTION_UP) { // here u can change the button color or image as released as button.setBackgroundImage(R.drawable.image_released); } return false; } }); 

I think it may helpful to you..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.