Android ImageButton with a selected state?
This works for me:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: order is important (the first matching state(s) is what is rendered) -->
<item
android:state_selected="true"
android:drawable="@drawable/info_icon_solid_with_shadow" />
<item
android:drawable="@drawable/info_icon_outline_with_shadow" />
</selector>
And then in java:
//assign the image in code (or you can do this in your layout xml with the src attribute)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));
//set the click listener
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
//Set the button's appearance
button.setSelected(!button.isSelected());
if (button.isSelected()) {
//Handle selected state change
} else {
//Handle de-select state change
}
}
});
For smooth transition you can also mention animation time:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
Selected state for ImageButton
Because the selected state isn't automatically shown by an ImageButton, which - normally (as opposed to artificially) - shows only the normal and pressed statuses (not sure about the focused state, but it should).
You could else use a custom ToggleButton (or a Switch or a CheckBox).
Anyway, your solution doesn't look that bad at all, to me.
How do I set the selected state of an image button with xml
It doesn't look like you can -- sorry!
keep android button selected state
I searched on Google and found this Stack Overflow post:
How can i keep one button as pressed after click on it?
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
But read the comment, it's pretty interesting!
ImageButton with different states images size
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);
How to keep imagebutton in pressed state and display the drawable for pressed state
You can try with selected state of the drawable ::
ibS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ibS.setPressed(true);
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show();
}});
Change the line ibS.setPressed(true);
to ibS.setSelected(!ibs.isSelected());
So, it will change the drawble of image to the selected_state image define in the drawable the putting the state selected too..on clicking again..it will revert the last state...
Try this..
How do I change an ImageButton to its selected state in an android widget?
try this..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed1" android:state_pressed="true"/>
<item android:drawable="@drawable/nopressed"/>
</selector>
in ur drawable.
Set ImageButton state programmatically
You should use View v
which is passed in, no need to find the ImageButton again.
Also, if you're setting the button background to an image use setBackgroundResource
instead of setBackgroundColor
public void btnErase_click(View v) {
ImageButton btnErase = (ImageButton) v;
if (pressed == true)
btnErase.setBackgroundColor(Color.YELLOW);
else
btnErase.setBackgroundResource(android.R.drawable.btn_default);
}
Related Topics
Create Custom Notification, Android
How to Change Progressbar's Progress Indicator Color in Android
Locationsettingsrequest Dialog to Enable Gps - Onactivityresult() Skipped
Programmatically Check Play Store for App Updates
Android Timepickerdialog Set Max Time
How to Delete a Whole Folder and Content
Android 4.2 Broke My Encrypt/Decrypt Code and the Provided Solutions Don't Work
How to Hide the System Bar in Android 3.0? It's an Internal Device and I'm Managing Navigation
Service Won't Stop When Stopservice Method Is Called
Android How to Create My Own Activity and Extend It
Android Push Notification (Gcm), Is There Any Daily Limit
Android Dex Gives a Bufferoverflowexception When Building
Get and Parse CSV File in Android
Android Create Folders in Internal Memory