Listview Custom Selector issue - android

i have a simple custom selector for my listview but i get an xml error any ideas why?
Errors:
unbound prefix (line 3)
not well formatted (invalid token) (line 5)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android ="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false"
<color android:color = "#FF8800" />
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#E77A26"
android:angle="270" />
</shape>
</item>
<item android:state_selected="true" android:state_pressed="false"
<color android:color = "#FF8800" />
</selector>

As Nadir B suggested, you didn't close your tag properly, unfortunately he suggested a wrong solution.
Item must wrap the color element(<item ...> ... <color... /> </item>) and not be a self closing element (eg <item ... />).
You have made this mistake twice in the example you have given. Ensure you notice both changes.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android ="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false"
<color android:color = "#FF8800" />
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#E77A26"
android:angle="270" />
</shape>
</item>
<item android:state_selected="true" android:state_pressed="false"
<color android:color = "#FF8800" />
</selector>
Should be
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android ="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false">
<color android:color = "#FF8800" />
</item>
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#E77A26"
android:angle="270" />
</shape>
</item>
<item android:state_selected="true" android:state_pressed="false">
<color android:color = "#FF8800" />
</item>
</selector>

your not closing your xml tag
the problem is here
<item android:state_selected="true" android:state_pressed="false"
remove tab before
<?xml version="1.0" encoding="utf-8"?>
do like this
EDIT remove the space in xmlns like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false"/>
<color android:color = "#FF8800" />
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#E77A26"
android:angle="270" />
</shape>
</item>
<item android:state_selected="true" android:state_pressed="false"/>
<color android:color = "#FF8800" />
</selector>

Related

Button with background and shape doesn't have a press effect

I can't figure out why my buttons doesn't have the press effect.
i guess my styles is the problem.
The button is displayed inside a fragment hosted by AppCombatActivity
v21 style.xml is
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
style.xml is
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your customizations go here -->
<item name="buttonStyle">#style/LoginButton</item>
<!-- Override the color of UI controls -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorPrimary">#color/my_blue</item>
<item name="colorPrimaryDark">#color/my_blue</item>
<item name="colorAccent">#color/my_purple</item>
</style>
<style name="LoginButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textAllCaps">false</item>
<item name="colorControlHighlight">#color/my_purple</item>
<item name="android:textColor">#color/white</item>
</style>
button code:
<Button
android:theme="#style/LoginButton"
android:background="#drawable/roundedbutton_do_this"
android:backgroundTint="#color/dark_blue"
android:id="#+id/SelectDateButton"
android:layout_width="match_parent"
android:layout_height="52dp"
android:gravity="center"
android:text="#string/ChooseDateTime_SelectDate"
android:layout_margin="12dp"
android:singleLine="false"
android:layout_gravity="center_vertical" />
background code:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
minSdkVersion:16
targetSdkVersion:27
Now i don't see any effect on the button when i press it. test it on android 6 and 7
EDIT: i have try this but the buttons dont have shape (rounded corners)
<?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="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#color/green" />
<item>
<shape android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
</item>
</selector>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="#dimen/_3sdp"
app:cardBackgroundColor="#android:color/white">
You can use selector as background drawable, refer to this answer.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:drawable="#color/green" />
</selector>
I have manage to create a press effect with rounded corners that fades on click this way:
selector.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/colorbutton" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/colorbutton" />
</selector>
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
colorbutton.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
and on button i set:
<Button
android:background="#drawable/selector" />
thanks #Omkar for the help.

how to add a selector to a shape?

How to add beloved different xml file in one file.
first file is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/blue" android:state_selected="true"/>
<item android:drawable="#color/transparent"/>
</selector>
second file is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff"/>
<stroke android:width="0dp"
android:color="#ff000000"/>
<corners android:radius="25px"/>
</shape>
try this -
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/blue" android:state_pressed="true"/>
<item android:drawable="#drawable/second" android:state_pressed="false"/>
</selector>
you can do this way:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" >
<shape>
<solid android:color="#xxxxxx" />
....
</shape>
</item>
<item android:drawable="#color/transparent"/>
</selector>

How to add a selector to a View's border

I have an edittextview with a black colored border. How can I change this border color depending on the state, using a selector.
I tried this:
<EditText
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/black_rounded_borders"
/>
black_rounded_borders.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<stroke
android:width="2dp"
android:color="#drawable/selector_black_border" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
selector_black_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:color="#FF000000"/>
</selector>
EDIT:
I changed selector_black_border.xml to color folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_selected="true" android:color="#FF269ff5"/>
<item android:state_focused="false" android:color="#FF000000"/>
<item android:state_pressed="false" android:color="#FF000000"/>
<item android:state_selected="false" android:color="#FF000000"/>
</selector>
<stroke
android:width="2dp"
android:color="#color/selector_black_border" />
But then it always remain in blue color("#FF269ff5") even when not focused/pressed.
Thank You.
you have to put the selector inside color/ and
<stroke
android:width="2dp"
android:color="#color/nameofcolor">

Change external shape color in XML

So, if I have a shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="10dp"/>
</shape>
And I want to have that shape in a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/someDrawable" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/someOtherDrawable" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/someOtherOTHERDrawable"/>
</selector>
How could I use my shape in my selector AND set the color of it? Is that even possible in to do in the XML? Would I have to use an <include /> tag like this:
<item >
<include layout="#drawable/myShape"
android:color="#987" />
</item>

Problem setting ListView selector color when pressed

I have the following ListView, and have followed the answer from this SO Question w/ no luck:
Android ListView Selector Color
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ARListView"
android:background="#drawable/transparent_background"
android:cacheColorHint="#00000000"
android:listSelector="#drawable/ar_selector"
android:drawSelectorOnTop="true">
Here is my ar_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/solid_red" />
<item android:state_focused="true"
android:drawable="#android:color/transparent" />
</selector>
When I press down on a ListViewItem nothing appears.
Sheehan Alam, use this code:-
list_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/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
gradient_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
gradient_bg_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#18d7e5"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>

Categories

Resources