Android:Change Button Text and Background Color

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 to change text background color of a button in Android?

Sample Image

created this with the help of this,i have correctly positioned the textview on button to acheive this kind of aout put:

<Button
android:id="@+id/button1"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="380dp"
/>

<TextView
android:id="@+id/textView1"
android:layout_width="130dp"
android:layout_height="70dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="420dp"
android:textSize="40dp"
android:text="Text"
android:gravity="center"
android:textColor="#ffff00"
android:background="#000000"/>

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="#FFFFFF"

Coding:

//btn represents your button object

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

Change Button color and its Text Color in android

What about this?

@Override
public void onClick(View v) {
...
if (v instanceof Button) {
((Button) v).setBackgroundColor(Color.WHITE);
((Button) v).setTextColor(Color.BLACK);
}
}

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 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"

Android button background color

If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property

android:backgroundTint="@android:color/holo_green_light"


Related Topics



Leave a reply



Submit