I have a simple Button, which has a drawable set as icon:
<Button
android:id="#+id/bOk"
android:drawableStart="#drawable/icon_ok"
android:text="#string/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
when I disable the button, either in XML layout file: android:enabled="false"
or programmaticaly: bOk.setEnabled(false);
The button gets disabled, it is 'grayed out', but the icon remains as it was ine the enabled state.
How can I get a look, that the icon is also 'grayed out'?
Create a new grayed icon and add both inside a selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_ok" android:state_enabled="true" />
<item android:drawable="#drawable/icon_ok_disabled" android:state_enabled="false" />
</selector>
Use inside button like: android:drawableStart="#drawable/selector"
For TextColor,
Create another selector inside res/color/mycustomtextcolor.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#666" android:state_enabled="false" />
<item android:color="#000" android:state_enabled="true"/>
</selector>
Inside your widget call using: android:textColor="#color/mycustomtextcolor"
Or
inside your style add another item using: <item name="android:textColor">#color/mycustomtextcolor</item>
Like a regular Button, a Button that has an image background is not grayed when disabled.
You have to use another image it appears grayed.
<?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/button_gray" /> ***button_gray is a Drawable image***
<item android:state_pressed="true"
android:drawable="#drawable/button_gray" />
<item android:drawable="#drawable/button_red" /> ***button_red is a Drawable image***
</selector>
You can tint your drawable on enabled and disabled state using a selector with bitmaps:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/disableColorWithAlpha" />
</item>
<item android:state_enabled="true">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/colorAccent" />
</item>
</selector>
Related
I have two images. In one is a number, the other is shadow. I need to show this two images as can be seen in the second photo.
This is my code. Right now it only shows or the number (when is not pressed), or the shadow(when is pressed).
How is possible to show both images when the user press the button?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_select" android:state_pressed="true"
android:alpha="0.75"/>
<item android:drawable="#drawable/number_1" android:state_enabled="true" />
<item android:drawable="#drawable/number_1" android:state_pressed="false"
/>
</selector>
this are the two images
this is the goal
Also I was trying using layerDrawable in this way, but didn't work:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_select" android:state_pressed="true"
android:alpha="0.75"/>
<item android:drawable="#drawable/number_1" android:state_enabled="true" />
</selector>
</item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/number_1" android:state_pressed="false"
/>
</selector>
</layer-list>
You should use two different images...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image" android:state_pressed="false"/>
</selector>
You can use Relative Layout for that
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_select"/>
<ImageView
android:id="#+id/iv_img"
android:layout_width="wrap_content"
android:padding="5dp"
android:layout_height="wrap_content"
android:background="#drawable/number_1"/>
</RelativeLayout>
Just change image of iv_img id GOOD LUCK
please change the drawable selector file with this code..
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/number_1" />
<item android:bottom="#dimen/margin_5" android:left="#dimen/margin_5" android:right="#dimen/margin_5" android:top="#dimen/margin_5">
<bitmap android:src="#drawable/button_select" />
</item>
</layer-list>
</item>
<item android:drawable="#drawable/number_1" android:state_pressed="false"/>
Limitation ::
It can use for single image of number ..for multiple numbers you have to create different selector.xml for each one..
Please tell if need more explanation or help..
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 tried to track down, how Lollipop displays a button, which is disabled with android:enabled="false" in the layout file.
Holo
With Holo, it's easy: In the styles_holo.xml, I find the style Widget.Holo.Button, which gives me a reference to #drawable/btn_default_holo_dark. There I find the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
Lollipop
When I try to apply the same logic to Lollipop, I got stuck:
In styles_material.xml I find the style <style name="Widget.Material.Button"> where I find the reference to <item name="background">#drawable/btn_default_material</item>. But there is no selector??!! Instead I find:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="#drawable/btn_default_mtrl_shape" />
</ripple>
Could someone please explain, which specific style Lollipop uses for a disabled button.
Thanks a lot!
Edit
I can partially answer myself: In #drawable/btn_default_mtrl_shape I find a reference to <solid android:color="?attr/colorButtonNormal" />, which in turn points to #color/btn_default_material_light, which includes a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:alpha="#dimen/disabled_alpha_material_light"
android:color="#color/button_material_light"/>
<item android:color="#color/button_material_light"/>
</selector>
But that alpha value only explains half of it. Somehow Lollipop also set the elevation down to 0?
This is how I resolved this, thanks to your partially answer.
First: add new Folder "color" under "res" folder, if it doesn`t exist.
Add new .xml file in "color" folder (I will call this file ButtonColorSelector.xml) where we will create new ColorStateList like this:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#F2F2F2"/> <!-- disabled -->
<item android:state_pressed="true" android:color="#FF0000"/> <!-- pressed -->
<item android:state_focused="true" android:color="#FF0000"/> <!-- focused -->
<item android:color="#0000FF"/> <!-- default -->
</selector>
Second: Add that ripple effect .xml file you mentioned, under "drawable" folder and reference your colorSelector instead of btn_default_mtrl_shape. I will call this file RaisedButton.xml.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape>
<solid android:color="#color/buttoncolorselector"/>
<corners android:radius="5dp" />
</shape>
</item>
</ripple>
Third: Now you can use your drawable for button background in your layouts, like this:
<Button
android:background="#drawable/raisedbutton"
android:text="#string/SomeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="DoStuffitto"
android:enabled="false"
android:id="#+id/someButton" />
I have some Buttons on my android app. They have an icon and text. I can set the background color of a Button in java code. If the button is clicked I want to display with a different color. So, how do I set a different color for the pressed state of the Button?
<Button
android:id="#+id/save"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:scaleType="center"
android:drawableTop="#drawable/save"
android:text="Save"
android:textColor="#FFFFFF"
android:textSize="14dip"
>
The onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
save = (Button)findViewById(R.id.save);
save.setBackgroundColor(Color.rgb(27,161,226)); }
create xml file using the button image like this with mybutton.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>
and use this in button xml code
android:background="#drawable/mybutton"
add those color codes in the resource-->values-->colors.xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#0066cc</color>
<color name="gold">#e6b121</color>
<color name="grey">#cccccc</color>
</resources>
Reference : Change button background on touch
Below is the sample code for color state list used for a button
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#c0c0c0"
android:state_selected="true"/>
<item
android:color="#ffffff"
android:state_pressed="true"/>
<item
android:color="#9A9A9A"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
</selector>
Also please check below link for color state list
http://developer.android.com/guide/topics/resources/color-list-resource.html
Use a StateList. Below is an example of a selector with a different drawable for the pressed state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_for_pressed_state" android:state_pressed="true"/>
<item android:drawable="#drawable/drawable_for_normal_state"/>
</selector>
You need to use a drawable with selector for pressed states, more commonly done in xml links below.
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
edittext_modified_states.xml
<?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/apptheme_textfield_activated_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/apptheme_textfield_focused_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/apptheme_textfield_disabled_focused_holo_light"/>
<item android:drawable="#drawable/apptheme_textfield_default_holo_light" />
</selector>
here: http://android-holo-colors.com goto this website and select your color and imort into your drawable. goto layout xml and set button background. android:background="#drawable/edittext_modified_states"
If you want to change button background color then just do as follow..
#Override
public void onClick(View v) {
if(v.getId() == R.id.btn01) {
btn1.setBackgroundColor(Color.RED);
btn1.setTextColor(Color.WHITE);
}
just add this code in onclick event of button.
I want to change the background image of a button when clicked or focused.
This is my code:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
Is this code right? Does it calls a button on its event?
you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="#drawable/your_imagename_while_pressed" />
<item android:drawable="#drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)
Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.
button_background_selector.xml
<?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/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/your_hover_image"/>
<item android:drawable="#drawable/your_simple_image" />
</selector>
Now set the above file in button's background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/grey_text"
android:background="#drawable/button_background_selector"/>
Sorry this is wrong.
For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.
For example: button_selector.xml (define this file inside the drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
<!-- IF you want image instead of color then write
android:drawable="#drawable/your_image" inside the <item> tag -->
And apply it as:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/button_selector.xml" />
use this code
create xml file in drawable folder name:button
<?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/buutton_pressed" />
<item
android:drawable="#drawable/button_image" />
</selector>
and in button xml file
android:background="#drawable/button"
To change the button background we can follow 2 methods
In the button OnClick, just add this code:
public void onClick(View v) {
if(v == buttonName) {
buttonName.setBackgroundDrawable
(getResources().getDrawable(R.drawable.imageName_selected));
}
}
2.Create button_background.xml in the drawable folder.(using xml)
res -> drawable -> button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/tabs_selected" /> <!-- selected-->
<item android:state_pressed="true"
android:drawable="#drawable/tabs_selected" /> <!-- pressed-->
<item android:drawable="#drawable/tabs_selected"/>
</selector>
Now set the above file in button's background file.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_background"/>
(or)
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
2nd method is better for setting the background fd button
You just need to set background and give previous.xml file in background of button in your layout file.
<Button
android:id="#+id/button1"
android:background="#drawable/previous"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
and done.Edit Following is previous.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/onclick" android:state_selected="true"></item>
<item android:drawable="#drawable/onclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/normal"></item>
You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#81ba73" />
<corners android:radius="6dp" />
</shape>
<ripple android:color="#c62828"/>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="#788e73" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#add8a3" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).
Create a file in drawable play_pause.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/pause" />
<item android:state_selected="false"
android:drawable="#drawable/play" />
<!-- default -->
</selector>
In xml file add this below code
<ImageView
android:id="#+id/iv_play"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#drawable/pause_button"
android:gravity="center"
android:scaleType="fitXY" />
In java file add this below code
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);
and also add this
iv_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iv_play.setSelected(!iv_play.isSelected());
if (iv_play.isSelected()) {
((GifDrawable) gif_1.getDrawable()).start();
((GifDrawable) gif_2.getDrawable()).start();
} else {
iv_play.setSelected(false);
((GifDrawable) gif_1.getDrawable()).stop();
((GifDrawable) gif_2.getDrawable()).stop();
}
}
});
use <androidx.appcompat.widget.AppCompatButton /> instead of 'Button'