I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?
Yes, you can do it like that:
layout/main_layout.xml:
.....
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bonjour !"
android:textColor="#color/button_text_color"
/>
.....
color/button_text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#c0c0c0" android:state_pressed="true"/>
<item android:color="#ffffff"/>
</selector>
See the section called State List in this bit of documentation...Drawable Resources.
You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state. You then define a selector which switches the drawable resources in the different states.
EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.
I like the solution proposed by Konstantin Burov in the other issue: Android customized button; changing text color
You can actually manage more states than just pressed and normal. But it should solve the problem.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#000000" />
<!-- Default color -->
<item android:color="#ffffff" />
</selector>
Then you can use that selector drawable in your button changing the text color attribute like below. Note that the selector in the example below is named "button_text_color"
android:textColor="#drawable/button_text_color"
Using the same drawable approach you can also solve the background color of the button. Just remember that in the selector instead of using the "android:color" attribute you need to use the "android:drawable" attribute like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#ffffff" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#000000" />
<!-- Default color -->
<item android:drawable="#ffffff" />
</selector>
And then in the button itself do, note that this time the selector name is "button_background"
android:background="#drawable/button_background"
You have to do it in your code. Try this:
mBtn = ((Button) findViewById( R.id.button1 ));
mBtn.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
mBtn.setTextColor( Color.RED );
}
});
Declare:
private Button mBtn;
You must set #drawable xml resource in textColor attributte
Here is example: Android customized button; changing text color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#FFFFFF" />
<item
android:state_pressed="true"
android:color="#000000" />
</selector>
Related
I am creating a application that has couple of edittext's that chang color when the edittext has focus or not. When the view has the focus the color should be blue, when normal it should have white color, something like this:
I've tried creating two nine patches and setting the background according to this xml but seems its not working. Can anyone tell me how can I achieve this ?
Xml selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_focused="true"
android:drawable="#drawable/spelling_blue" />
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spelling_white" />
<item android:drawable="#drawable/spelling_white" />
</selector>
Thanks
I don't think you need to use android:state_window_focused.
Try this:
<?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/spelling_blue" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/spelling_blue" /> <!-- focused -->
<item
android:drawable="#drawable/spelling_white" /> <!-- default -->
</selector>
Hi i'm trying to change the color of toggle button's text through xml.
I have referred links but its only changing the background color of toggle button but not its text.
I tried with this approach :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffff" />
<item android:state_checked="false" android:color="#000000" />
</selector>
but only the background is changing.
Note : I don't want to do it in code since there are 21 toggle buttons and setting listeners for each of them is not good.
You shouldn't set the parent of a widget style to be a theme. Instead, you'll want to set it to be the default widget style that you want to modify (e.g. #android:style/Widget.Holo.Button.Toggle).
In your case, however, you don't need to use a style:
res/color/toggle_text.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffff" />
<item android:color="#000000" />
</selector>
res/layout/your_layout.xml:
...
<ToggleButton
android:id="#+id/toggleButton"
...
android:textColor="#color/toggle_text" />
I have some buttons with transparent background in my app. Now, I want to customize them to maintain that transparent background, but when they are pressed, the backgroud should become green.
I know that are lots of topics about custom buttons here on SO, I have readed several of them, also lots of tutorials from google. And although it may seem an easy task, I'm not getting it to work.
This is an example code of my buttons:
<Button
android:id="#+id/accept_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/button_state"
android:text="#string/btnaccept"
android:textStyle="bold" />
And this is the selector xml file buton_state.xml where I've defined the background color change for diferent button states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button focused and pressed-->
<item android:state_pressed="true"
android:state_focused="true" >
<shape>
<solid
android:color="#color/LightGreen" />
</shape>
</item>
<!-- Button Default-->
<item android:state_pressed="false"
android:state_focused="false" >
<shape>
<solid
android:color="#android:color/transparent" />
</shape>
</item>
</selector>
This file is in res/drawable folder.
In my app, the button gets correctly a default transparent background, but this background color doesn't change to green when focused nor pressed.
try this may be help you,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/LightGreen" /> <!--pressed -->
<item android:drawable="#android:color/transparent" /> <!-- Normal -->
</selector>
Use Selector
<?xml version="1.0" encoding="utf-8"?>
<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>
and then
<Button
android:id="#+id/button1"
android:background="#drawable/Selector File Name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
I want to change the background colour of my StaggeredGridView item when pressed, but currently I am getting an ugly gingerbread orange by default, as shown
here. I tried setting the gridview items background as a selector, but when I did that, if I click one item, all the items' background colours are changed.
<!-- My selector -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_blue_light"
android:state_pressed="true" />
<item android:drawable="#android:color/transparent" />
</selector>
And this was in my StaggeredGridView, but it didn't help:
<!-- In StaggeredGridView -->
android:listSelector="#drawable/selector"
By the way, I am using this StaggeredGridView Library. Thanks in advance!
at the implementation of staggeredGridViews onClick event:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for(int i=0;i<mGridView.getChildCount();i++) {
mGridView.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.yellow));
// set all items to yellow
}
mGridView.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.blue));
//set the selected items color to blue
}
hope this helps
The solution was to stay away from any #color or #drawable that refers
to a color inside listSelector. I created two 3x3 pixel .png files.
Each saved with the gamma layer. In my case it's two of the same color
each mixed down in Gimp with a different transparency on the color
layer. So when you select an item you get an overlay with 25% color,
and when you press it you get a png with 50% color. I put them in my
drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/bg_list_item_highlighted" /> <!-- #drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/bg_list_item_pressed" /> <!-- #drawable/tab_press -->
</selector>
then
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
you should change a line in that library and than make your own selector
1) in the library there is a class called: StaggeredGridView
there is a method in that class called: useDefaultSelector()
inside that comment the setSelector line and add this line:
setSelector(getResources().getDrawable(R.drawable.selector));
2)make an xml file inside drawable folder in the library as follow: selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/yourColor1" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/yourColor2" />
<item android:state_focused="true" android:drawable="#color/yourColor3" />
</selector>
In my seTabColor() I'm setting the color of the title text to Gray. I want to change it to white when pressed. How can I do it?
public void setTabColor(TabHost tabHost) {
for(int i = 0; i<tabHost.getTabWidget().getChildCount(); i++) {
// tabHost.getTabWidget().getChildAt(i).setBackgroundResource(r[i]);
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK);
TextView t = (TextView) getTabWidget().getChildAt(i).findViewById(android.R.id.title);
t.setTextSize(9 * getResources().getDisplayMetrics().density);
// tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 58;
// tabHost.getTabWidget().getChildAt(i).().height = 58;
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.GRAY);
}
I wanna do something like: tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())...
But im not sure how to use that to conditionally change the text color.
Try this answer, which in particular shows:
<item name="android:textColor">#android:color/tab_indicator_text</item>
You can override that default textColor by creating your own color selector (create the res/color/ directory in your project, and create a new file in there, called tab_indicator_text.xml), and changing the value above to match your own color selector (#color/tab_indicator_text). The contents of the tab_indicator_text.xml file will be a selector list, like the one mentioned in this answer:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/white" />
<item android:state_pressed="true" android:color="#color/white" />
<item android:color="#bfbfbf" />
</selector>
First, look into defining your UI in XML if that's possible.
Take a look at State List Drawable Resource. You can define what image to use when a view is pressed, highlighted, etc. After defined, you can then use your XML file like any other resource.
Example:
XML file saved at res/drawable/button.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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html just go there you waill come to know what u want to do.
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);