I want that in my android application, the ImageButton change its image when it is pressed and released, and when it is pressed released again, the image for ImageButton will be changed back , how to do that?
I have tried it with selector.
But it is not working.
Please anyone help me.
Thanks
make file in drawable folder a.xml ::
<?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/oneImage" />
<item
android:state_pressed="false"
android:drawable="#drawable/secondImage" />
</selector>
and in your main file :: button have implment following code ::
<ImageButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="anything"
android:id="#+id/first"
android:background="#drawable/a"
android:textColor="#FFFFFF"
android:textStyle="bold"
></ImageButton>
use the following selector.
<?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/pressedImage"/>
<item
android:state_focused="true"
android:drawable="#drawable/normalImage"/>
<item android:drawable="#drawable/normalImage"/>
</selector>
Related
I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />
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"/>
Im trying to create custom button and i cant see my problem.
i create in res/drawable/ custom.xml
<?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/forwardpress"></item>
<item android:state_focused="true" android:drawable="#drawable/forwardhover"></item>
<item android:drawable="#drawable/forward"></item>
</selector>
and on main.xml i have this button
<Button
android:id="#+id/bFor"
android:background="#drawable/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right" />
The problem is, button is stay presed without animation, (yes i have 3 diferent picture in drawable folder)
Funny thing is, I for one button works but the other does not
i try this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/backpress" android:state_pressed="true"/>
<item android:drawable="#drawable/back"/>
its work but for second button i try the same process
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/forwardpress" android:state_pressed="true"/>
<item android:drawable="#drawable/forward"/>
</selector>
for second button doesnt work, wierd...
Clean and build your project again.
I my app i have many buttons,what i want is that when user selects particular button,its background should change so that user knows which button is selected.So for this i have used selectors.But when ever i run my app the event on button click works fine but the button image doesnt change
Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/people_1" android:state_selected="true"></item>
<item android:drawable="#drawable/people_1" android:state_active="true"></item>
<item android:drawable="#drawable/places_1" android:state_focused="true"></item>
<item android:drawable="#drawable/hotel_icon"></item>
</selector>
Xml where i used the selector
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/hotel"
android:layout_weight="1"
android:id="#+id/home" />
I solved it,the only thing missing was
<item android:drawable="#drawable/selected" android:state_pressed="true" />
I've been trying to customize the toggle button look but with no success.
Here is how I want it to look like:
Can someone give me a tutorial?
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
I don't know if this is the best solution but it worked fine for me:
1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.
2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi
3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off
4.- Create the following xml files in a new folder called drawable (if it does not exist yet):
ic_name1_toggle.xml
ic_name1_toggle_bg.xml
ic_name2_toggle.xml
(...)
ic_name5_toggle_bg.xml
5.- In ic_name1_toggle.xml the code must be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_name1_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_name1_on" />
</selector>
6.- In ic_name1_toggle_bg.xml the code must be:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/ic_name1_toggle" />
</layer-list>
7.- Finally in your layout.xml:
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="56dp"
android:layout_height="76dp"
android:background="#android:color/transparent"
android:button="#drawable/ic_name1_toggle_bg"
android:textOff=""
android:textOn="" />
I think you need to define a custom background for your button.
Take a look at the Developer Guide on customizing a Button's background.
However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_da"
android:state_checked="true" />
<item android:drawable="#drawable/button_nu" />
</selector>
The element android:state_checked="true" is what defines that state as the checked background.
Let me know if this works for you.
Create selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_da" android:state_checked="true"/>
<item android:drawable="#drawable/btn_nu"/>
</selector>
and use it as background for your ToggleButton.