different text colour for Toggle button - android

Is there a way to have different text colours for a toggle button? If i use the android:textColor attribute, it will be applied to both states of the toggle button. Can we have one colour for On and another for Off?
Here's an example ToggleButton element :
<ToggleButton
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="200dp"
android:textOn="On"
android:textOff="Off"
android:onClick="onToggleClicked"
android:layout_centerInParent="true"
android:textSize="100sp"
android:textColor="#color/yellow"
/>

create a directory called color under the res directory.
In there create an xml file like, toggle_text_selector.xml
This is what the file could look like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"/>
<item android:color="#android:color/black" android:state_checked="false"/>
</selector>
Then on your ToggleButton set android:textColor="#color/toggle_text_selector"
and you're done

Related

Is there any way to create android:text state selector for button?

A beginner here.
We create state selector for android:background and android:color etc so why can't we create it for android:text?
I have a button and I want to change the text when I press button using selector, is it possible?
Button text in Normal state--> hello
Button text in Pressed state--> Hey
Here is my button and the drawable selector, please tell me how to create selector for android:text
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
Following is button_effect.xml file in drawable directory
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
Thank you so much for your support...

Change Color onClick and background on Focus

i have a ListView in my Navigation Drawer and want it looks like the Playstore App or Youtube App.
Means onClick changing FontColor for Example to red and when i just hold it the background should turn grey.
Do i have to use 2 onitemclicklistener?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/DrawerListIcon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/DrawerListFontColor"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:paddingLeft="0dp"/>
</RelativeLayout>
Tried with Item and Selector. First thing Font Color don't change when i pressed the Button. Second the background of the Relative Layout turns blue insead of Grey.
drawer_list_font.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListFontColorPressed"
/>
<item android:color="#color/DrawerListFontColor"/></selector>
drawer_list_background.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListBackgroundFocused"
/>
<item
android:color="#color/DrawerBackground"
/></selector>
I had to use android:drawable instead of background on the relative Layout. On the Textview Android Stuio accepted android:color.
This is changed by changing view from ur java file at every click event u can change the view. U have to make multiple view ie xml files for that. Which are dynamically changed or u can also send parameters to a function and create an xml at runtime but that will be too deep for u
No need for listeners at all.
A better approach here is to create drawable XML's with definitions for every state ("pressed", "focused", etc).
Check out Color State List resources
For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"android:color="#80000" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ff0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ff0000" />
<item android:color="#color/DrawerListFontColor" />
</selector>

Checkbox without the square android

I would like to simply have the "tick" without the square. Also only as ImageView would be fine. Anyone knows the name of the resource in Android?
Just set selector to drawable left to Checkbox in whatever shape you desire.
Do like this.
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableLeft="#drawable/checkImageSelector" />
android:button="#null" will remove the default image of square with tick and drawableLeft will place your image in place of that.
checkImageSelector.xml will be like this.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check" android:state_selected="true"/>
<item android:drawable="#drawable/unchecked"/>
</selector>
You need two images for checked and unchecked state.
Create a selector with same resources :
checkbox_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/checked_image" android:state_checked="true"/>
<item android:drawable="#drawable/unchecked_image"/>
</selector>
Then set this selector as button :
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
Activity:
CheckBox.setButtonDrawable((int)getResources().getColor(R.color.transparent));
Fragment:
CheckBox.setButtonDrawable((int)getActivity.getResources().getColor(R.color.transparent));

Button Press,Text Color

I need to change Button Color(normal/pressed) using Selector
res/color/test_color_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/button_focused"/>
<item android:state_pressed="false" android:color="#color/button_font"/>
<item android:color="#color/button_font"/>
</selector>
in code
class MyButton (its extended from class Button) and I used following code to set text color
//at Constructor
this.setTextColor(getResources().getColor(R.color.text_color_button));
But my problem is ,Button alwas shows default color
You need to create a ColorStateList object
XmlResourceParser parser = getResources().getXml(R.color.test_color_button);
ColorStateList colorStateList = ColorStateList.createFromXml(getResources(), parser);
this.setTextColor(colorStateList);
try to set the selector to the background like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_bg" />
Why changing colors,lets do a complete new attractive thing.... I am sure you will like it
Create an xml file in your drawable folder named as button_bg.xml like the following
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="#drawable/button" />
</selector>
where button_pressed and button are the images which you want to show on button pressed and normal state of button
Set the background of the button as button_bg.xml like the following
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="70dp"
android:layout_marginLeft="360dp"
android:layout_marginTop="520dp"
android:background="#drawable/button_bg" <!--like this-->
android:text="Login"
android:textColor="#ffffff"
android:textSize="30dp" />
and enjoy!

Custom button which will change text color, background color and drawable on click

I have a Button XML file in /drawable/ which changes states of the button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/img_normal"
android:state_focused="true" />
<item android:drawable="#drawable/img_normal"/>
</selector>
And this Ok if I will change background only on click event.
But now, I have a button with left drawable, and on click I have to change left drawable, text color and button background.
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My button"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/btn_leftDrawable"
android:background="#android:color/transparent"
/>
Can I do this inside this XML and how? I don't want to make spaghetti code by doing these extra tasks inside Java code in setOnClickListener method.
Create three different xml selector and add them to the corresponding attributes..
Sample:
TextColor:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/click"/>
<item android:color="#color/normal"/>
Background:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/buttonBackgroundClick"/>
<item android:color="#android:color/transparent"/>
Add them to your button:
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My button"
android:textColor="#drawable/textColor"
android:drawableLeft="#drawable/btn_leftDrawable"
android:background="#drawable/background"
/>

Categories

Resources