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!
Related
I am using selector for imageView to change image on press event.
but it is not changing image when I press on it.
Here is selector,
<?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/fed_1" />
<item
android:drawable="#drawable/fed" />
</selector>
And this is my imageview,
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:src="#drawable/sel_fed" />
Also tried using background for image view but it's not working.
you should add android:clickable="true". Your imageView will be like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:clickable="true"
android:src="#drawable/sel_fed" />
change your src to background and add android:clickable="true" to your ImageView
You can try this selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/fed_1" android:state_selected="true"></item>
<item android:drawable="#drawable/fed_1" android:state_pressed="true"></item>
<item android:drawable="#drawable/fed"></item>
</selector>
Change src to background in your image view.
android:background="#drawable/sel_fed"
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>
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));
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
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"
/>