remove listitem blue selection - android

I added the following selector to my listview in my drawerlayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item android:state_focused="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- pressed -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
</selector>
colorPrimaryDark and colorPrimary are just color values
This work amost fine. But if i press and release an listitem on API 19 or lower, the item gets blue. How is that possible?

If you are using AppCompat as a theme you should reference the colors from theme like this
<item android:drawable="?attr/colorPrimary"

I found the problem.
android:choiceMode="singleChoice"
This appeared to be the problem.

android:cacheColorHint="#00000000"
add this attribute for your listview

Related

XMl integer selector Android resource file

I have a requirement to vary the border box on a TextInputLayout, I have a theme that I have applied to all my TextInputLayouts and I want to modify boxStrokeWidth based on state
<style name="InputTextStyle" parent="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:textColor">#color/secondary_button_text_selector</item>
<item name="boxStrokeColor">#color/text_field_border_color</item>
<item name="boxStrokeWidth">#Integer/my_int_selector </item>
</style>
is there a way of creating a selector that returns a dimension integer?
something like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:dimen="1dp" android:state_pressed="true" /> <!-- pressed -->
<item android:dimen="4dp" android:state_focused="true" /> <!-- focused -->
<item android:dimen="1dp" android:state_enabled="false" /> <!-- focused -->
<item android:dimen="1dp" /> <!-- default -->
</selector>

How to change background color on Item in actionBar after it is pressed and keep the background?

I would like to to change background color on Item in actionBar after it is pressed and keep the background.
Now I get to change the background color when I press the item on ActionBar but it when I release the item the background color came back as before.
Keep the color as in the image after I release the item:
menu.xml
<item android:id="#+id/object_classification" android:orderInCategory="95" android:title="#string/title" android:actionLayout="#style/AppTheme" app:showAsAction="always" />
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarItemBackground">#drawable/ab_item_background</item>
<item name="actionBarItemBackground">#drawable/ab_item_background</item>
</style>
ab_item_background.xml (in drawable)
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<!-- focused/pressed: color=holo_orange_light -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#android:color/holo_orange_light" />
<!-- pressed: color=holo_green_light -->
<item
android:state_pressed="true"
android:drawable="#android:color/holo_green_light" />
<!-- normal: color=transparent -->
<item
android:drawable="#android:color/transparent" />
</selector>
Thanks a lot

How to change the font color of actionBar Tab to distinguish between selected and unselected?

In android actionBar Tabs, when you select a tab, that particular tab gets the blue-Underline-Highlight, and the remaining doesn't. Which is fine.
But my project wants the particular TEXT-COLOR of SELECTED tab to be of say BLUE color and all the other UNSELECTED tab's TEXT-COLOR to be of say GREY color.
How could I acheive variation in Text-Color ?
As far as I could go I made the text-color as BLUE.. but its blue color text for SELECTED and UNSELECTED TABS also.
Following is the code am using at the moment ( created in theme.xml in res/value/ )
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarTabStyle">#style/MyActionBarTabs</item>
<item name="android:textColor">#00A7F4</item>
<item name="android:actionBarTabTextStyle">#style/custom_actionbar_tab_text_style</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">#style/MyActionBarTabs</item>
<item name="android:textColor">#00A7F4</item>
<item name="android:actionBarTabTextStyle">#style/custom_actionbar_tab_text_style</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="#style/Widget.AppCompat.Light.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">#drawable/actionbar_tab_indicator</item>
<item name="android:textColor">#00A7F4</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_tab_indicator</item>
<item name="android:textColor">#00A7F4</item>
</style>
<style name="custom_actionbar_tab_text_style">
<item name="android:textAllCaps">false</item>
</style>
Use color state list
Create res/color/filename.xml
Example:
<?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>
and then use it
<item name="android:textColor">#color/filename</item>
Learn more : http://developer.android.com/guide/topics/resources/color-list-resource.html
Try This
<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 -->
or in code
<item name="android:textColor">#color/filename</item>
If i combine all lines together than answer would be following code. Create new folder under res folder( res/color/filename.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="#FFFFFF" /> <!-- pressed -->
<item android:state_focused="true" android:color="#FFFFFF" /> <!-- focused -->
<item android:state_selected="true" android:color="#FFFFFF" /> <!-- selected -->
<item android:color="#B3FFFFFF" /> <!-- default -->

Android Button background color when clicked

Can anyone help me with changing the background color of my button so that it can change color when clicked and change back to normal color after clicked. I am not interested in using images and have looked, everyone keep posting half the codes so I still don't understand fully. If possible, please provide a link where I can see step by step instructions, These half posted comments does not help me at all because I am totally new to android and Java.
You need to make special drawable for that with selector element.
<?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_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and in the button
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
More info here
<item
android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<item
android:state_pressed="true"
android:drawable="#drawable/item selected"/>
<item
android:state_focused="true"
android:drawable="#drawable/item selected"/>
<item
android:drawable="#android:color/transparent"/>
Put this xml in a drawable file and set the resource.

How to get selection appearance like listview for textview in android

I have a textview and some text is present inside of it. Now if i tap on textview, i want section appearance like listview. when i press on the text, it should show me selection appearance, how to do?
use selector Link
<?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_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

Categories

Resources