Skip to content Skip to sidebar Skip to footer

How To Change Text Background Color Of A Button In Android?

I have searched a lot but I haven't found what I want. What I’m planning to do is to change the background colour of text on a button. I’m not interested in changing the colour

Solution 1:

Use this to set the text color of Button :

setTextColor(getResources().getColor(R.color.white));

or

android:textColor="@color/white"

Use this to set the background of the button :

setBackgroundResource(R.drawable.button_img);

or

android:background="@drawable/button_img"

or

android:background="@color/blue"

Solution 2:

Maybe you should try to use RelativeLayout instead of Button. Something like this: `

<RelativeLayoutandroid:id="@+id/RelativeLayout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="10dp"android:clickable="true"android:background="#ffffff" ><TextViewandroid:id="@+id/TextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Text"android:textColor="#ffffff"android:background="#000000"android:textSize="16sp" /></RelativeLayout>`

Solution 3:

enter image description here

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

Solution 4:

your question is not correctly asked,but what i understood:

to set the background color of text means create shadow of text on button

<stylename="shadowed_button"><itemname="android:shadowColor">#444444</item><itemname="android:shadowDx">1</item><itemname="android:shadowDy">1</item><itemname="android:shadowRadius">9</item></style>

and setting shadow programmatically

button.setShadowLayer(9, 1, 1, Color.rgb(44, 44, 44));

to set the background color of button

<Buttonandroid:id="@+id/street_btn"android:layout_width="wrap_content"android:background="@drawable/layout_a" ><!-- your required color --></Button>

         or

button.setBackgroundColor(Color.BLUE);

and third thing is onbutton click change background,but you already mentioned that,you don't want that effect.

enter image description here

no you can not do this with only button widget,you have to create two contols:

    button with textView:
set textview background color
 or 
    Image with textView:
set textview background color
 orcreate [coumpound control][2]

Solution 5:

You can use selector for this :

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_focused="true"android:state_pressed="false"android:color="#ffffff" /><itemandroid:state_focused="true"android:state_pressed="true"android:color="#FF0000" /><itemandroid:state_focused="false"android:state_pressed="true"android:color="#FF0000" /><itemandroid: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"

All the best

Post a Comment for "How To Change Text Background Color Of A Button In Android?"