i am trying to change the color of text inside a button.
So for example i have a button which is filled with white and the text is blue. When i click on the button i want these two colors to swap (text inside the button becomes white and the button blue).
I have tried something like this:
<item style="#style/ButtonText_Menue_Clicked" android:drawable="#drawable/button_menue_default" android:state_focused="true"></item>
<item style="#style/ButtonText_Menue" android:drawable="#drawable/button_menue_default" ></item>
but it actually does nothing.
Is there any way to do what i want, or do i have to do some stuff in the onclik event (but then there is the problem how to set the colors back when the "click is gone" )
Create a selector resource in res/color for the text color and a button selector in res/drawable as below...
text_color.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#800000" />
<item
android:state_pressed="false"
android:color="#4C5" />
</selector>
button_color.xml
<?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="#4C5"/>
</shape>
</item>
<item
android:state_pressed="false">
<shape android:shape="rectangle" >
<solid android:color="#800000"/>
</shape>
</item>
</selector>
Add the button to the layout
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#color/text_color"
android:background="#drawable/button_color"/>
Related
I am trying to have a button changing color (background and text) when I click on it. Like, by default the button has a grey background and white text color, when you click on it, the background become white and text grey. When you reclick on it, it come back to grey background and white text color.
I tried this:
androidx.appcompat.widget.AppCompatButton
style="#style/AppTheme.Button"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:minWidth="60dp"
android:padding="11dp"
android:textColor="#color/button_text_color"
android:layout_margin="7dp"/>
The AppThemeButton is :
<style name=AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:textSize">16sp</item>
<item name="android:letterSpacing">0.06</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#drawable/corner_shape</item>
<item name="android:textAllCaps">false</item>
</style>
then my corner_shape is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<corners android:radius="15dp" />
<solid android:color="#color/brand04" />
</shape>
</item>
<item android:state_enabled="true">
<shape>
<corners android:radius="15dp" />
<solid android:color="#color/brand01" />
</shape>
</item>
</selector>
and the button_text_color as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/brand05" android:state_enabled="true"/>
<item android:color="#color/white"/>
</selector>
I was trying to play with enable and disable which was a terrible idea as when I set the button to enable=false I cannot reverse back as I cannot act using the click listener..
Any idea. I am kind of looking of for a toogle function for the color.
Thanks
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.
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 am trying to create clickable textbox that highlights like a listview item when pressed. I have my own background image that i want to use so i cannot use
android:background="?android:attr/selectableItemBackground"
Is there a way i can achieve the blue "glow" affect when i click on the textbox while using my own background image for the textbox? Currently, my textview looks like:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
style="#style/ListSeparatorTextViewStyle"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:focusable="true"/>
UPDATE:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:background="?android:attr/selectableItemBackground"
android:state_pressed="true" />
<item android:drawable="#drawable/list_section_divider_holo_custom"></item>
</selector>
you can create a drawable for background of textview same below
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" ><!-- this drawable display when you press textView -->
<shape android:shape="rectangle" >
<gradient android:startColor="#fa7905"
android:endColor="#fcc96b"
android:angle="90"
></gradient>
</shape>
</item>
<item android:drawable="your drawable">
<!-- this drawable show when you dont press textview -->
</item>
</selector>
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"