How to add underline to selected side menu item in SlidingMenu? - android

I want to put an underline to my selected side menu item.Is there any way to put only an underline to the item name instead of highlighting the whole item?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item android:drawable="your drawable when pressed" android:state_pressed="true"/>
<item android:drawable="your drawable when selected" android:state_selected="true"/>
<item android:drawable="your drawable when activated" android:state_activated="true"/>
</selector>
The above code is a simple selector from where you can make the one that you need and you also need to add android:choiceMode="singleChoice" to your listview and also add the selector listView.setSelector(R.drawable.your_selector); or you can add in xml like android:listSelector="#drawable/your_selector"
A better selector for your need would be
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<layer-list>
<item>
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<item android:bottom="3dp">
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
</layer-list>
</item>
</selector>
Hope it helps a bit.

Related

Android Button with bottom margin and state_pressed

Is there a way to create Android Button that have bottom margin when state_pressed is false, and that have rounded corners without bottom margin when state_pressed is true?
I have this, but it's without bottom margin when state_pressed is false:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
android:shape="rectangle">
<solid android:color="#B5B5B5" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape
android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</selector>
EDIT: Added pictures
android:state_pressed="false"
android:state_pressed="true"
<selector xmlns:android="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" android:state_pressed="false"/>
</selector>
Add this code your drawable folder and use separate button drawables for the different button state

Creating a transparent selector for button and shape

I tried to create a selector for my button, using the the following XML codes...
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" android:state_enabled="true"></item>
<item android:drawable="#drawable/rectangular_grey" android:state_pressed="true"></item>
</selector>
this is the rectangular gray shape I have defined...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/grey"/>
<stroke android:color="#android:color/transparent"/>
<corners android:radius="1dp"/>
</shape>
and this is the rectangular transparent shape I have defined...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent"/>
<stroke android:color="#color/grey" android:width="#dimen/_1sdp"/>
<corners android:radius="1dp"/>
</shape>
When I run it into my device, it shows only transparent button but it doesn't change color when i press...
The same approach works when I try to use with other color than transparent, please help...
The order of items in a selector is important. It always picks the first one that applies. So you should swap those items.
In your case, the button is always enabled, no matter what pressed state is, so it always picks the first item.
You need to use selector in right way :
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" ></item>
<item android:drawable="#drawable/rectangular_grey" android:state_pressed="true"></item>
or
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:drawable="#drawable/rectangular_transparent" android:state_pressed="true"></item>
<item android:drawable="#drawable/rectangular_grey"></item>

Android Ripple Effect Overridden by Selected State

After having been looking for a while I've not been able to find an answer to this...
I have a recycler view with items which when selected have a red background and white text (beforehand the background is white and text is black). To do this I am using a selector.
I have recently tried to add a ripple effect to this, but unless I long click on the item the background of the item goes straight to red without the ripple. I am assuming this is because the selector state state_selected overrides the ripple on sate_pressed?
Does anyone know if there is a way around this? Here is the selector code I use:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_red_dark" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/ripple"
android:state_pressed="true"/>
<item
android:drawable="#android:color/holo_red_dark"
android:state_selected="true"/>
<item android:drawable="#android:color/white"/>
</selector>
</item>
</ripple>
Thanks in advance!
To create a selector background that has a ripple effect and shows selected status I do the following:
Start by defining your highlight color, with some transparency:
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="selector_color">#660000ff</color>
</resources>
You probably want to have compatibility pre-lollipop. Put a typical old-school selector inside drawable folder:
drawable/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selector_color" android:state_pressed="true"/>
<item android:drawable="#color/selector_color" android:state_selected="true"/>
<item android:drawable="#android:color/transparent"/>
</selector>
And then add the following layer drawable inside drawable-v21 folder:
drawable-v21/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_selected="true"
android:drawable="#color/selector_color" />
<item android:drawable="#android:color/transparent" />
</selector>
</item>
<item>
<ripple android:color="#color/selector_color">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
</item>
</layer-list>
Now you can use #drawable/selector_background for your selector.
So I have another case in which I had to use selector as well as layer list for that
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<!-- ripple color -->
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/c_unread_notifications_item" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item>
<!-- ripple color -->
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/grey_very_light" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
</item>
</ripple>
</item>
</selector>
This worked, for your need what you can do it just replace the item under ripple with your item shape if you don't have any layering.
Hope this helps
It will be better if you wrap your recyclerview item view in FrameLayout and set android:background="?selectableItemBackground" of FrameLayout and the child layout of FrameLayout background="#drawable/background"
background.xml
<item android:drawable="#color/red" android:state_selected="true"/>
<item android:drawable="#color/red" android:state_focused="true"/>
<item android:drawable="#color/red" android:state_pressed="true"/>
<item android:drawable="#color/white"/>
And then child layout must has attribute android:duplicateParentState="true"

How to easily change the default color of a ListView

I'm new to Android, and trying to do some design on my first application, I changed the background ! and Now I want to change the color of the listView ( Blue ), Onclick, and when sliding up/Down.
Is it possible to change on the XML file, or I have to do programmatically.
here is a screenshot to make things clear :
I put as background for the list view this XML :
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/Orange"/>
</shape>
</item>
I get this :
So this is not what i'm looking for, I want only to change the default blue color
create one selector file,
/res/drawable/list_selectr.xml
<?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="#color/list_item_pressed"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#color/list_item_normal" />
</shape>
</item>
</selector>
And apply this xml as the background to the root tag of listview items xml.
Such as,
<RelativeLayout android:background="#drawable/list_selector .../>"
You can also set list background attribute of ListView in xml
android:background="#your_color"
Finally I found a solution for this, it's more simple that the solutions I found :
Just add this to your ListView in your xml file : android:listSelector="Yourcolor"
if you want the color for only "on focus" use a separated xml file :
android:listSelector="Yourbackground"
XML Example :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/Orange"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#color/Transparent" />
</shape>
</item>
</selector>

Android borders color with selector listview

I had this working perfectly, although I need to change my code to listSelector, and now I don't know how to do it in order to maintain the border on left side.
In all listview lines I had one border:
border.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#E30E0E"/>
</shape>
</item>
<item android:left="5dip">
<shape android:shape="rectangle">
<solid android:color="#E3E3E3"/>
</shape>
</item>
</layer-list>
This sets the left side of each line, a border with color. But, as you can see, it has a default Solid color for background. With this code, whenever the user click on the item of the listview he doesn't understand if he has already clicked or not, because it has this solid color which doesn't change onState().
To make this working, I needed to create a listSelector with background gradients on state select plus state pressed.
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/listview_gradient_bg"/>
<item android:state_pressed="true"
android:drawable="#drawable/listview_gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/listview_gradient_bg_hover" />
</selector>
So, my question is: there's any way to "link" this two different piece of code?
Try to put this
<item android:left="5dip">
<shape android:shape="rectangle">
<solid android:color="#E3E3E3"/>
</shape>
</item>
into the listview_gradient_bg and listview_gradient_bg_hover drawables, then link the selector to each line in the adapter.

Categories

Resources