Android selector with textColor [duplicate] - android

This question already has answers here:
Android customized button; changing text color
(5 answers)
Closed 6 years ago.
I have created a selector:
btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/btn_bg_pressed" android:state_pressed="true" />
<!-- focused -->
<item android:drawable="#drawable/btn_bg_focused" android:state_focused="true" />
<!-- default -->
<item android:drawable="#drawable/btn_bg_default" />
</selector>
btn_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/btn_pressed"/>
</shape>
btn_bg_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/reality_fix_yellow"/>
</shape>
btn_bg_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="#color/btn_default_border_color" />
</shape>
I want to change the text color the button when the focus changes. I've tried adding android:color="color_code" and android:textColor="color_code" but failed.
Please help me on How to change the text color of a button with selector?

Define this way :
Create button_selector.xml file in res/color directory
<?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 in Button property define this way.
<Button
android:TextColor="#color/button_selector"
/>

There's a trick where you can reuse your background drawable resource to change color for your text.
In your layout.xml, declare your button background and textColor in the following way:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_bg"
android:textColor="#drawable/btn_bg"
android:text="Hello" />
Now, in your drawable/btn_bg.xml, define android:color="#color/your_color_code" as the item attribute:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:color="#color/md_green_500" android:drawable="#drawable/btn_bg_pressed" android:state_pressed="true" />
<!-- focused -->
<item android:color="#color/md_red_500" android:drawable="#drawable/btn_bg_focused" android:state_focused="true" />
<!-- default -->
<item android:color="#color/md_blue_500" android:drawable="#drawable/btn_bg_default" />
</selector>
You should be able to see background and text color change for your button.

You can acheive this on your own TextView class that extends the Android TextView class and override the onTouchEvent(MotionEvent event)
You can then modify the instances text color based on the MotionEvent passed.
For example:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Change color
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Change it back
}
return super.onTouchEvent(event);
}

Related

Disabling a Button with an icon

I have a simple Button, which has a drawable set as icon:
<Button
android:id="#+id/bOk"
android:drawableStart="#drawable/icon_ok"
android:text="#string/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
when I disable the button, either in XML layout file: android:enabled="false"
or programmaticaly: bOk.setEnabled(false);
The button gets disabled, it is 'grayed out', but the icon remains as it was ine the enabled state.
How can I get a look, that the icon is also 'grayed out'?
Create a new grayed icon and add both inside a selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_ok" android:state_enabled="true" />
<item android:drawable="#drawable/icon_ok_disabled" android:state_enabled="false" />
</selector>
Use inside button like: android:drawableStart="#drawable/selector"
For TextColor,
Create another selector inside res/color/mycustomtextcolor.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#666" android:state_enabled="false" />
<item android:color="#000" android:state_enabled="true"/>
</selector>
Inside your widget call using: android:textColor="#color/mycustomtextcolor"
Or
inside your style add another item using: <item name="android:textColor">#color/mycustomtextcolor</item>
Like a regular Button, a Button that has an image background is not grayed when disabled.
You have to use another image it appears grayed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/button_gray" /> ***button_gray is a Drawable image***
<item android:state_pressed="true"
android:drawable="#drawable/button_gray" />
<item android:drawable="#drawable/button_red" /> ***button_red is a Drawable image***
</selector>
You can tint your drawable on enabled and disabled state using a selector with bitmaps:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/disableColorWithAlpha" />
</item>
<item android:state_enabled="true">
<bitmap android:src="#drawable/icon_ok" android:tint="#color/colorAccent" />
</item>
</selector>

Changing background color through styles

I have an image button and I want to change the color of a button when pressed. First I show it with a transparent as a default state color as below. But it doesn't show highlighted when pressed. So I decided to use other color to highlight when pressed using state drawable. But how to keep background color through styles wise. Means by default it should be transparent and when pressed it has to show other color.
present code:
<ImageButton
android:id="#+id/imgbutton"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_launcher"
android:background="#android:color/transparent"
/>
Need to be coded:
<ImageButton
android:id="#+id/imgbutton"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_launcher"
android:background="#drawable/btnselector"
/>
So what should be the btnselector.xml here?
put it inside res/color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/green_text_color"/> <!-- pressed -->
<item android:state_focused="true" android:color="#color/green_text_color"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
create a new shape, inside res/drawable
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/yourxmlfile" />
</shape>
and put it as background
or a res/drawable selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/green_botton_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/green_botton_highlited"
android:state_focused="true" />
<item android:drawable="#drawable/green_botton" />
</selector>

how to change background image of button when clicked/focused?

I want to change the background image of a button when clicked or focused.
This is my code:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
Is this code right? Does it calls a button on its event?
you can implement in a xml file for this as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="#drawable/your_imagename_while_pressed" />
<item android:drawable="#drawable/image_name_while_notpressed" /> //means normal
</selector>
now save this xml file in drawable folder and name it suppos abc.xml and set it as follows
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
Hope it will help you. :)
Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.
button_background_selector.xml
<?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="#drawable/your_hover_image" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/your_hover_image" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/your_hover_image"/>
<item android:drawable="#drawable/your_simple_image" />
</selector>
Now set the above file in button's background.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/grey_text"
android:background="#drawable/button_background_selector"/>
Sorry this is wrong.
For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.
For example: button_selector.xml (define this file inside the drawable folder)
<?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>
<!-- IF you want image instead of color then write
android:drawable="#drawable/your_image" inside the <item> tag -->
And apply it as:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/button_selector.xml" />
use this code
create xml file in drawable folder name:button
<?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/buutton_pressed" />
<item
android:drawable="#drawable/button_image" />
</selector>
and in button xml file
android:background="#drawable/button"
To change the button background we can follow 2 methods
In the button OnClick, just add this code:
public void onClick(View v) {
if(v == buttonName) {
buttonName.setBackgroundDrawable
(getResources().getDrawable(R.drawable.imageName_selected));
}
}
2.Create button_background.xml in the drawable folder.(using xml)
res -> drawable -> button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/tabs_selected" /> <!-- selected-->
<item android:state_pressed="true"
android:drawable="#drawable/tabs_selected" /> <!-- pressed-->
<item android:drawable="#drawable/tabs_selected"/>
</selector>
Now set the above file in button's background file.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_background"/>
(or)
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.abc);
2nd method is better for setting the background fd button
You just need to set background and give previous.xml file in background of button in your layout file.
<Button
android:id="#+id/button1"
android:background="#drawable/previous"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
and done.Edit Following is previous.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/onclick" android:state_selected="true"></item>
<item android:drawable="#drawable/onclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/normal"></item>
You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:
<?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="#81ba73" />
<corners android:radius="6dp" />
</shape>
<ripple android:color="#c62828"/>
</item>
<item android:state_enabled="false">
<shape>
<solid android:color="#788e73" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#add8a3" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).
Create a file in drawable play_pause.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/pause" />
<item android:state_selected="false"
android:drawable="#drawable/play" />
<!-- default -->
</selector>
In xml file add this below code
<ImageView
android:id="#+id/iv_play"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#drawable/pause_button"
android:gravity="center"
android:scaleType="fitXY" />
In java file add this below code
iv_play = (ImageView) findViewById(R.id.iv_play);
iv_play.setSelected(false);
and also add this
iv_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iv_play.setSelected(!iv_play.isSelected());
if (iv_play.isSelected()) {
((GifDrawable) gif_1.getDrawable()).start();
((GifDrawable) gif_2.getDrawable()).start();
} else {
iv_play.setSelected(false);
((GifDrawable) gif_1.getDrawable()).stop();
((GifDrawable) gif_2.getDrawable()).stop();
}
}
});
use <androidx.appcompat.widget.AppCompatButton /> instead of 'Button'

How to change the background color of the toggle button on Android

I tried to change the background color of the toggle button using XML file as white color, but the toggle button is totally damaged. It looks like all the button was covered with white.
There is no indication of ON or OFF on the toggle button when I have changed the color of the toggle button to white. Is there is another way to change the background which will not damage the indication of the toggle button?
<ToggleButton android:id="#+id/togglebutton"
android:layout_width="100px"
android:layout_height="46px"
android:background="#ffffff"
android:layout_above ="#+id/save"
android:textOn="DAY"
android:textOff="NIGHT" />
This is how my XML code look for the toggle button.
Yes, there is a way to change the background as you wish, but you have to use a selector like this as background:
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/some_image" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/some_other_image" />
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/some_image1" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/other_image" />
</selector>
For #Drawable, etc. (you can use a color or make a gradient. Check this for more information about gradients.
Follow this way to make your ToogleButton have background color red when On and green when OFF
First, create tooglebutton_selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/togglebutton_on"
android:state_checked="true" />
<item android:drawable="#drawable/togglebutton_off"
android:state_checked="false"
/>
</selector>
Second, create togglebutton_on.xml in 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="#ff0000" /> // red color
</shape>
Third, create togglebutton_off.xml in 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="#00FF00" /> // green color
</shape>
Finally, in your ToggleButton
<ToggleButton
android:id="#+id/btnMon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tooglebutton_selector" //set background of ToggleButton to tooglebutton_selector
/>
When you decompile your SystemUI.apk, you should go to the following file: SystemUI/res/values/colors.xml
Then change the following line:
#ff000000
#ffffffff
#80000000
#ffadc1d6
#ffffffff
#ffe6e6e6

Android LinearLayout with color resource: What am I doing wrong?

I followed this tutorial to create a color state list for a particular Android view. I just want it to highlight when clicked so the user knows why the screen just changed.
When the view is rendered, I get the following error:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable
My color XML (in res/color/viewcolor.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="#ff33ffff"/> <!-- pressed -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
My layout XML (in res/layout/myview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="#color/viewcolor">
<!--crap in the layout-->
</LinearLayout>
What did I miss?
I remember that I worked around this error by using state drawable instead of state color. For some reason layout background just doesn't work with stateful colors. So try creating a stateful drawable (for example list of shape drawables with different colors) and use it as background.
res/drawable/pressed.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff33ffff" />
</shape>
res/drawable/normal.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
</shape>
res/drawable/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/pressed" />
<item android:drawable="#drawable/normal" />
</selector>
Then use background.xml drawable as background :)
Instead of using shapes in your drawable, you can use the android:drawable attribute which accepts a color resource (e.g. #color/black).
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="#drawable/myDrawable">
<!-- other views in layout-->
</LinearLayout>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- focused -->
<item android:state_focused="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- focused and pressed-->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- pressed -->
<item android:state_pressed="true" android:drawable="#color/YOUR_COLOR_HERE" />
<!-- default -->
<item android:drawable="#color/YOUR_COLOR_HERE" />
</selector>
In my_drawable.xml you need to make sure that the colors you specify are defined in res/values/colors.xml, or this won't work.
If you want to use an image instead of a color change from a color resource to a drawable resource.
Example:
android:drawable="#color/YOUR_COLOR_HERE"
android:drawable="#drawable/YOUR_IMAGE_HERE"

Categories

Resources