Is there a way to highlight an ImageButton when it's pressed?
You can define a drawable via XML and use the selector, like below, to use different (i.e. highlighted) images for different button states:
i.e. res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_catlocfilter" android:state_pressed="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_pressed="true" />
<item android:drawable="#drawable/bg_catlocfilter" android:state_focused="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_focused="true" />
</selector>
Use this resource then for the ImageButton view.
Related
I want to create effects for ImageButton. For example, it will change color when clicked...How I do it? I want to do this in .xml file. Can you help me! Thank you!
I tried to create the state.xml file as follwing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_0" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_ac" />
</selector>
However, I can't set background for ImageButton. The error like this:
All you have to do it to add the "android:background" attribut to your ImageButton and set a drawable.
Your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_btn"
android:background="#drawable/btn_drawable"/>
btn_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue"
/>
<item android:state_focused="true"
android:drawable="#drawable/white"
/>
<item android:drawable="#drawable/green" />
</selector>
In that code above you set a different drawable when your ImageButton is pressed (state_pressed), focused (state_focus) or when is normal (not pressed and not focused).
Here you can find with more detail.
Create a drawable like below and name it as btn_drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/btn_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_click" />
</selector>
This is for an example,Like this you can add the <item/> according to your needs and state of the image button.
Then set the drawable as image button background.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_drawable"/>
I want to change the background colour of my StaggeredGridView item when pressed, but currently I am getting an ugly gingerbread orange by default, as shown
here. I tried setting the gridview items background as a selector, but when I did that, if I click one item, all the items' background colours are changed.
<!-- My selector -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_blue_light"
android:state_pressed="true" />
<item android:drawable="#android:color/transparent" />
</selector>
And this was in my StaggeredGridView, but it didn't help:
<!-- In StaggeredGridView -->
android:listSelector="#drawable/selector"
By the way, I am using this StaggeredGridView Library. Thanks in advance!
at the implementation of staggeredGridViews onClick event:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for(int i=0;i<mGridView.getChildCount();i++) {
mGridView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.yellow));
// set all items to yellow
}
mGridView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.blue));
//set the selected items color to blue
}
hope this helps
The solution was to stay away from any #color or #drawable that refers
to a color inside listSelector. I created two 3x3 pixel .png files.
Each saved with the gamma layer. In my case it's two of the same color
each mixed down in Gimp with a different transparency on the color
layer. So when you select an item you get an overlay with 25% color,
and when you press it you get a png with 50% color. I put them in my
drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/bg_list_item_highlighted" /> <!-- #drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/bg_list_item_pressed" /> <!-- #drawable/tab_press -->
</selector>
then
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
you should change a line in that library and than make your own selector
1) in the library there is a class called: StaggeredGridView
there is a method in that class called: useDefaultSelector()
inside that comment the setSelector line and add this line:
setSelector(getResources().getDrawable(R.drawable.selector));
2)make an xml file inside drawable folder in the library as follow: selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/yourColor1" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/yourColor2" />
<item android:state_focused="true" android:drawable="#color/yourColor3" />
</selector>
My purpose is to change the color of a button when click and my codes are
<?xml version="1.0" encoding="utf-8"?>
selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
</selector>
It works but if i make a small change like below :
xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When not selected, use white-->
item android:drawable="#drawable/btn_askering" />
<!-- When selected, use grey -->
item android:drawable="#drawable/btn_askering_active" android:state_selected="true" />
</selector>
It does not work anymore....
I need some help...Any comments are welcomed here.Thanks
I think you need to mention stats in selector like pressed or focused and change image accordingly.
Here i have attached sample selector file,have a look and try accordingly.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/estimator_hover_new" />
<item android:drawable="#drawable/estimator_new" />
</selector>
All The Best....
I am using the Android SDK icon-button for refresh (ic_menu_refresh) in a widget and I need to change the selection state when it is pressed. How is this done? Do I define an XML for the button?
You define the different states in xml via selector.
Sample (esp. see the state-attributes):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_catlocfilter" android:state_pressed="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_pressed="true" />
<item android:drawable="#drawable/bg_catlocfilter" android:state_focused="false" />
<item android:drawable="#drawable/bg_catlocfilter_dark" android:state_focused="true" />
</selector>
I am wanting to allow the user of my android application the ability to set some parameters. The radio button is ideal for this situation. However, I don't like the radio buttons are rendered.
Is it possible to change the radio button icon? For example, is it possible to create a custom layout for each row and in that layout reference my own icon and change the font et al.
Yes that's possible you have to define your own style for radio buttons, at res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:radioButtonStyle">#style/RadioButton</item>
</style>
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/radio</item>
</style>
</resources>
'radio' here should be a stateful drawable, radio.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:drawable="#drawable/radio_hover" />
</selector>
Then just apply the Custom theme either to whole app or to activities of your choice.
For more info about themes and styles look at http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/ that is good guide.
You can put custom image in radiobutton like normal button.
for that create one XML file in drawable folder
e.g
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sub_screens_aus_hl"
android:state_pressed="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_checked="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_focused="true" />
<item android:drawable="#drawable/sub_screens_aus_dis" />
</selector>
Here you can use 3 different images for radiobutton
and use this file to RadioButton like:
android:button="#drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content"
The easier way to only change the radio button is simply set selector for drawable right
<RadioButton
...
android:button="#null"
android:checked="false"
android:drawableRight="#drawable/radio_button_selector" />
And the selector is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_checkbox_checked" android:state_checked="true" />
<item android:drawable="#drawable/ic_checkbox_unchecked" android:state_checked="false" /></selector>
That's all
yes....`
from Xml
android:button="#drawable/yourdrawable"
and from Java
myRadioButton.setButtonDrawable(resourceId or Drawable);
`
Here's probably a quick approach,
With two icons shown above, you shall have a RadioGroup something like this
change the RadioGroup's orientation to horizontal
for each RadioButton's Properties, try giving the icon for Button
under CompoundButton,
adjust the Padding and size,
and set the Background attribute when checked.
In case you want to do it programmatically,
checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);