Android customized button; changing text color - android

I made a button that changes the background drawable on different states, this way:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_location_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="#drawable/btn_location_pressed"/> <!-- focused -->
<item android:drawable="#drawable/btn_location"/> <!-- default -->
</selector>
The problem here is that I'm also trying to change the textColor as I do with the drawable but I'm not being able to. I already tried android:textColor and android:color but the first doesn't work whilst the seconds changes my background.
The next code is part of my layout. Regarding to the text color it only works for the normal state text color, thus not changing it to the white one while pressed
<Button android:id="#+id/location_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:background="#drawable/location"
android:textSize="15sp"
android:textColor="#color/location_color"
android:textColorHighlight="#FFFFFF"
/>
Has anybody got a clue?

Create a stateful color for your button, just like you did for background, for example:
<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>
Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:
android:textColor="#drawable/button_text_color"

Another way to do it is in your class:
import android.graphics.Color; // add to top of class
Button btn = (Button)findViewById(R.id.btn);
// set button text colour to be blue
btn.setTextColor(Color.parseColor("blue"));
// set button text colour to be red
btn.setTextColor(Color.parseColor("#FF0000"));
// set button text color to be a color from your resources (could be strings.xml)
btn.setTextColor(getResources().getColor(R.color.yourColor));
// set button background colour to be green
btn.setBackgroundColor(Color.GREEN);

ok very simple first go to
1. res-valuse and open colors.xml
2.copy 1 of the defined text their for example
#FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this
<color name="White">#FFFFFF</color>
then inside your button add this line
b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));
ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2

Changing text color of button
Because this method is now deprecated
button.setTextColor(getResources().getColor(R.color.your_color));
I use the following:
button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));

Use getColorStateList like this
setTextColor(resources.getColorStateList(R.color.button_states_color))
instead of getColor
setTextColor(resources.getColor(R.color.button_states_color))

Related

How to change Android button click color

I have one button. I have added background image to my button using background attribute. Now when I click on button, I am not getting the default orange color. How can I get that color.
One more query, In the above scenario how can I change the default orange color to some other color.
Try this http://developer.android.com/guide/topics/ui/controls/button.html#Style, change state of your button
You have to make your button custom to do that changes for that create an xml file named custom_btn in yoyr drawable folder and paste the code in it as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/tick_btn" /> <!-- pressed -->
<item android:drawable="#drawable/untick_btn" /> <!-- default -->
</selector>
and in the button you have to add android:button="#drawable/custom_btn"
// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="hover color or drawable" android:state_pressed="true"></item>
<item android:drawable="hover color or drawable" android:state_focused="true"></item>
<item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="hover color or drawable" android:state_enabled="false"></item>
</selector>
if you're just trying to see the "default orange color" then use an ImageButton and apply your drawable to the src (rather than the background). You will then see the native image background behind your image and it will continue to do whatever it was doing before.
I am not getting the default orange color. How can I get that color.
When you add a background to a button it no more remains a default raw button , It becomes a custom button as you have rendered the default behavior by adding some background to it. Now you need to add color to your custom button on your own because the OS deals with only raw buttons not custom.
How can I change the default orange color to some other color.
To change the button state after its pressed can be done in two ways
1) Either add a background image.
2) or Add a xml to the button.
This is a very nice tutorial on Customizing Android buttons.

How to change the color of a focused EditText when using "android:Theme.Holo.Light"?

I Am using "android:Theme.Holo.Light" in my application, it gives blue as the Default color.
I want to have a EditText when focused like below.
By default it comes like this when applying holo.light theme.
You can use Holo Color generator for custom colors.
http://android-holo-colors.com/
You don't need to download all controls. Only these you want.
May this help you:
You will have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder.
For example:
edittext_modified_states.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="#android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="#android:drawable/edittext_normal"
/> <!-- default -->
</selector>

Textview not not going back to orginal color after being unpressed

I have a text view that I am setting the text color as follows:
<TextViw
android:id = "#+id/tv"
android:clickable = "true"
android:textColor = "#color/clickable_text"
android:textSize="16sp"
/>
And the selecor is
<item android:state_pressed "true" android:color = "#FF0000" /> //which is red
<item android:color = "#00FF66" /> //which is Green
The textview starts Green which is as expected. If I click (say hold on it) the color changes to Red which is good. But when release the click (i.e. untouch it). then the color changes to yellow!. Which is the default color android whould change a clickable view to upon clicking. WHY!!!!.
Shouldn't it come back to green to as I expected? The problem is that this color stasys on evenif I move between activities and come back
Any help?
Thank you
You usually want to have a pressed, focused, and default state. And always put the default state last by the way. My guess is if you long press and then release, the button is not presssed (red), but is still focused. So change your focused to green as well as below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color = "#FF0000" /> <!-- pressed -->
<item android:state_focused="true"
android:color = "#00FF66" /> <!-- focused -->
<item android:color = "#00FF66" /> <!-- default -->
</selector>
I know this isn't the exact answer for your question, but the idea from this link might help you: Android how to make TextView text bold when pressed or focussed. I hope this helps you.

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you

How to change focus color of EditText in Android

How can I change the focus color (orange) on an EditText box?
The focus color is a small rim around the entire control and is bright
orange when the control has focus. How can I change the color of that
focus to a different color?
You'll have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder, and build a new StateListDrawable, for example something like the below:
edittext_modified_states.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="#android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="#android:drawable/edittext_normal"
/> <!-- default -->
</selector>
I don't know offhand the actual names for the default NinePatches for the EditText, so replace those as necessary, but the key here is to just use the #android:drawable images for the ones you haven't modified (or you can copy them over to your project's drawable folder), and then use your modified drawable for your focused state.
You can then set this StateListDrawable as the background for your TextView, like so:
<TextView
android:background="#drawable/edittext_modified_states"
You dont need to create xml drawables. It may be more simple in code.
Example in kotlin:
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
// colorLine, colorLineFocus is vars of ColorStateList
ViewCompat.setBackgroundTintList(editText, if (hasFocus) colorLineFocus else colorLine)
}
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:color="colorcode"
/> <!-- pressed -->
<item
android:state_focused="true"
android:color="colorcode"
/> <!-- focused -->
<item
android:color="colorcode"
/> <!-- default -->
</selector>

Categories

Resources