Android Customized Button; Changing Text Color

change button text color when pressed

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.

Android customized button; changing text color

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"

How to change android button text color globally in theme


 <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#yourcolor</item>
<item name="android:buttonStyle">@style/ButtonColor</item>
<item name="colorButtonNormal">@color/buttonColor</item>
</style>



<style name="ButtonColor" parent="@android:style/Widget.Button">
<item name="android:textColor">@color/yourcolor</item>
</style>

android:textColor This should help you change the text color globally.

Android : change button text and background color

Here is an example of a drawable that will be white by default, black when pressed:

<?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="#1E669B"/>
<stroke
android:width="2dp"
android:color="#1B5E91"/>
<corners
android:radius="6dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>

</item>
<item>
<shape>
<gradient
android:angle="270"
android:endColor="#1E669B"
android:startColor="#1E669B"/>
<stroke
android:width="4dp"
android:color="#1B5E91"/>
<corners
android:radius="7dp"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
</item>
</selector>

How do I change the text color of a Button?

try this:

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

or

button.setTextColor(0xff0000); //SET CUSTOM COLOR 

or

button.setTextColor(Color.parseColor("#ff0000")); 

and in xml :

<Button android:id="@+id/mybtn" 
android:text="text textx "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ff0000" /> <-- SET TEXT COLOR HERE -->

How to change the color of a button?

You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.

XML:

<Button
android:background="@android:color/white"
android:textColor="@android:color/black"
/>

You can also use hex values ex.

android:background="@android:color/white"

Coding:

//btn represents your button object

btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);

how to change the button text color on AlertDialog globally using style?

With the MaterialComponents theme and the MaterialAlertDialogBuilder you can define globally the style using the materialAlertDialogTheme attribute in your app theme.

Something like:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">@style/My_MaterialAlertDialog</item>
</style>

Then you can define a custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<!-- Style for neutral button -->
<item name="buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item>
</style>

with the button style defined by:

  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">@color/primaryDarkColor</item>
</style>

<style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/primaryDarkColor</item>
</style>

<style name="NueutralButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
....
</style>

With the version 1.1.0 of the library you can also simply override the default color using the materialThemeOverlay in the custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialThemeOverlay">@style/DialogButtonOverlay</item>
</style>

<style name="DialogButtonOverlay">
<item name="colorPrimary">@color/...</item>
</style>

How can I change default dialog button text color in android 5

You can try to create the AlertDialog object first, and then use it to set up to change the color of the button and then show it. (Note that on builder object instead of calling show() we call create() to get the AlertDialog object:

//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage);
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int i) {
...
}

}).create();

//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT);
}
});

dialog.show()

The reason you have to do it on onShow() and cannot just get that button after you create your dialog is that the button would not have been created yet.

I changed AlertDialog.BUTTON_POSITIVE to AlertDialog.BUTTON_NEGATIVE to reflect the change in your question. Although it is odd that "OK" button would be a negative button. Usually it is the positive button.

How to change android AppCompatButton's text color on pressed

To do this, you just need to replace the textColor you specified with a selector:

Something like this:

<?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/white" />
<item android:state_focused="true" android:color="@color/grey" /> <!-- (for accessibility) -->
<item android:color="@color/black" />
</selector>

and then assign it like this:
android:textColor="@drawable/button_foreground_selector"

<android.support.v7.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="@style/CustomAppCompatButtonStyle"
android:text="Hi, Neo"/>

Not clicked button:

Not clicked

Clicked button:

Clicked

Style I used:

<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/red</item>
<item name="colorControlActivated">@color/blue</item>
<item name="android:colorButtonNormal">@color/green</item>
<item name="android:textColorPrimary">@color/gray</item>
<item name="android:textColor">@drawable/button_foreground_selector</item>
</style>


Related Topics



Leave a reply



Submit