<?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/tab_profile_pressed_mdpi" />
<item android:drawable="#drawable/tab_profile_unselected_mdpi" />
</selector>
And how I set them:
((ImageView)tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.single_tab_img)).setImageResource(unselected_img[i]);
You need to use setBackgroundResource not setImageResource. State drawables work on the background image, not the foreground image.
I believe you need another xml file that lays out what will happen when the tab is pressed.
Example:
tab.xml
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/tab_bg_selected" />
tab_bg_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>
Related
I have a layout in which I have like button which is a simple textview and I have applied onclick event on this. Now I want to show the same effect which facebook shows when like button in facebook android app get clicked on the news feeds.
How can I do this in my android application. Please help me if you have any idea here.
Use selector if you want to give a highlight effect only when the button in touched.
Save this as an xml drawable and assign this drawable to the button :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_white_09_rectangle_unpressed" android:state_focused="true"
android:state_pressed="false" />
<item android:drawable="#drawable/btn_white_09_rectangle_pressed" android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_white_09_rectangle_pressed" android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_white_09_rectangle_unpressed" />
</selector>
Create 2 other drawable xml files which is being used by the above xml.
btn_white_09_rectangle_unpressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item >
<shape
android:shape="rectangle"
>
<corners
android:radius="9dp"/>
<gradient
android:angle="45"
android:endColor="#27FFFFFF"
android:startColor="#18FFFFFF"
/>
</shape>
</item>
</layer-list>
btn_white_09_rectangle_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item >
<shape
android:shape="rectangle"
>
<corners
android:radius="11dp"/>
<gradient
android:angle="90"
android:endColor="#09FFFFFF"
android:startColor="#02FFFFFF"
/>
</shape>
</item>
</layer-list>
If you want to change the background of the button(and not revert) when user clicks on it then you have to do it through code.
Use the following code in your onClick()
your_button.setBackgroundResource(R.id.desired_drawable);
I've noticed that the effect created by the attribute ?attr/selectableItemBackground only shows when I long tap the view. But I want it to be shown on every tap.
View is clickable and has on click listener.
How to do that?
I've noticed there was a change in the behavior between Lollipop and Marshmallow:
Lollipop - it would start the ripple on press.
Marshmallow - the ripple starts on release.
Could that be the issue?
I would stick to the device Look & Feel but you could
try this suggested solution:
https://stackoverflow.com/a/34167312/348378
Or use instead a library instead, maybe like this one:
https://github.com/balysv/material-ripple
You can achieve this by setting the background of a view. First of all you have to make an item_selector.xml drawable in drawable and drawable-v21.
For drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/item_pressed" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/item_pressed" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/item_normal" android:state_focused="true"/>
<item android:drawable="#drawable/item_normal" android:state_focused="false" android:state_pressed="false"/>
</selector>
For drawable-v21 folder-
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/item_pressed"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="#drawable/item_pressed"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="#drawable/item_normal"
android:state_focused="true"/>
<item
android:drawable="#drawable/item_normal"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
</item>
</ripple>
Now all you need to do is put android:background="#drawable/item_selector" in the view tag. For example-
<LinearLayout
android:id="#+id/traveller_select_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/item_selector"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/textTravellers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="#string/passengers"
android:textColor="#color/baggage_grey"
android:textSize="12sp"/>
</LinearLayout>
Now make item_pressed.xml in the drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/filter_bg"/>
</shape>
And the item_normal in the drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#color/white"/>
</shape>
Without your code, it's hard to know where goes wrong. But since I have just got this implemented, share with you what I've done, and perhaps you just follow, would help you figure out the issue.
In your view that need the click for ripple
<TextView
android:id="#+id/your_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content
android:test="Testing View"
android:background="#drawable/below_drawable">
In your /drawable-v21 folder, you have your below_drawable.xml with content
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/your_click_background_color">
<item android:id="#android:id/mask">
<shape android:shape="rectagle" >
<solid android:color="#android:color/your_mask_color" />
</shape>
</item>
</ripple>
Since the above for Lollipop (v21) only, if you want some impact to your non-lollipop (just show the color, but no ripple). You could have the below in /drawable folder the below_drawable.xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/your_click_background_color" />
</shape>
</item>
</selector>
Hope this helps.
To give users visual feedback when they touch a view, I do
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" />
But ?attr/selectableItemBackground is a gray color. I want to use a different color. To do that I do
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android.com/apk/res/android">;
<item android:drawable="#color/mine" android:state_selected="true"></item>
<item android:drawable="#color/mine" android:state_pressed="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
but it does not work, even after I set clickable="true" for the view in question.
Follow the code
button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_light_green"/>
<corners android:radius="5dp" />
</shape>
button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_light_green_selected"/>
<corners android:radius="5dp" />
</shape>
button_background.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/button_selected"/>
<item android:state_focused="true" android:drawable="#drawable/button_selected"/>
<item android:drawable="#drawable/button_normal"/>
</selector>
assign button_background.xml as background for the button by changing the colors which u desire. Hope it works!!!
I have a ListView item which has a set background. This overrides the default blue highlight that appears when the item is pressed/selected. Is there a way to have both the background and the selector?
This is my attempt at merging both a background and selector...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/red"/>
</selector>
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="6dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
This is in my drawable folder, and I set it with this in my ListItem xml:
android:background="#drawable/my_background
To have a custom background and the default selector effect (another drawalbe when pressed / selected) is a little difficult, after a few tries, I made it.
You should define two selectors in separated xml file: listitem_background.xml and listitem_selector.xml.
The first one is used to the background of the list item, it will make the effect when the item is pressed and in normal state.
The second one is used to the selector of the list, it will get rid of the default selector of the list view by setting all the state to transparent.
The default selector effect is defined in the first xml file: listitem_background.xml.
First you need a xml file to define some drawable color: color_drawable.xml, in res/values directory:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The color of the normal state. -->
<drawable name="listitem_normal">#E671B8</drawable>
<!-- The two color below show when the item is pressed, you should change that to the color you want. -->
<drawable name="listitem_pressed">#e7eeab</drawable>
<drawable name="listitem_selected">#e7eeab</drawable>
</resources>
Then, listitem_background.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/listitem_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/listitem_selected" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/listitem_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="#drawable/listitem_normal"/>
</selector>
and, listitem_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="#color/android:transparent" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#color/android:transparent" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#color/android:transparent"/>
</selector>
set listitem_background to listitem:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/listitem_background" >
...
</RelativeLayout>
set listitem_selector to listview:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/listitem_selector" />
Seeing as this is getting a bit of attention again, I will post the solution I found (which I had previously mentioned in a comment):
I found android:drawSelectorOnTop="true" in the ListView solved the problem.
Just use of this in ListView to match the color combination
android:cacheColorHint="#e7eeab"
I'm trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.
i followed the instructions on this post, the answer talking about shapes.
Now i have 3 xml on drawables folders:
normal.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
</shape>
pressed.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
and finally, bg.xml file
<?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/pressed" />
<item android:state_focused="true" android:drawable="#drawable/pressed" />
<item android:state_selected="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/normal" />
</selector>
I am accessing this in the following way:
Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
view.setBackgroundDrawable(d);
The "normal" state its fine, with the color set at "normal.xml", but no way with the other ones, I press my view and nothing happens, it's not changing color in any way...
I can't see what i'm doing wrong...
Thank you
Your view needs to be clickable in order to get the state pressed when you click on it.
Use :
view.setClickable(true);
or in the layout xml :
android:clickable="true"