Change button color without changing shape in android

When I speak

 button.setBackgroundColor(Color.BLUE);

The shape of the button changes to the default rectangle. I want to change the color of the button without affecting its original shape. Please help me.

+5
source share
7 answers

Whenever you change the default background on your button, the shape will change because the default shape is a rectangle and the default background is a shape with rounded corners. If you use any other background, this effect of the rounded corner is lost.

You can achieve the same effect with any color or shape if you use a shape suitable for drawing as a background.
Here's how you can achieve this:

  • Create a form.
  • .

:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid
        android:color="#f8f8f8"/>
    <corners
        android:radius="4dp"/>
    <padding 
        android:left="10dp"
        android:right="10dp"
        android:top="5dp"
        android:bottom="5dp"/>
    <stroke 
        android:width="1dp"
        android:color="#aeaeae"/>
</shape>


, xml

<?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="#ff0000" />
            <stroke 
                android:width="1dp"
                android:color="#ff0000" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item >
        <shape >
            <gradient
                android:startColor="#ff2727"
                android:endColor="#890000"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#620000" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>        
    </item>
</selector>

xml . , state_pressed, , , .
, .

+6

, , :

myButton = (ImageButton)myView.findViewById(R.id.my_button);
Drawable roundDrawable = getResources().getDrawable(R.drawable.round_button);
roundDrawable.setColorFilter(Color.BLUE, Mode.SRC_ATOP);

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    myButton.setBackgroundDrawable(roundDrawable);
} else {
    myButton.setBackground(roundDrawable);
}

XML "round_button", :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#4db6ac"/>
</shape>
+6

. android:backgroundTint XML . , .

XML :

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me!"
        android:textColor="#FFF"
        android:backgroundTint="#2196f3" />
+1

, android paint , , , ()

0

layout.xml

Android: = "*****"

0

- /. , , .

First you need to define the button in xml:

       <Button
            android:id="@+id/myButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonshape"
            android:text="Change Colour but keep shape, please" />

You will see above it buttonshape, therefore in the folder drawable:

(This is a rectangle, gray, with a dark gray border 1dp)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#D3D3D3" />
    <corners android:radius="3dp" />

    <stroke
        android:width="1dp"
        android:color="#aeaeae" />
</shape>

Now a new xmlfile in the folder drawable, buttonpressed.xmlfor pressing. The color will be green, everything else will be the same.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    android:shape="rectangle">
    <solid android:color="#2AB40E" />
    <corners android:radius="3dp" />

    <stroke
        android:width="1dp"
        android:color="#aeaeae" />
</shape>

Now, at the touch of a button in our business:

 private void myButton() {

        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

//              keep the slightly rounded shape, when the button is pressed
                myButton.setBackgroundResource(R.drawable.buttonpressed);

etc..etc...
0
source

All Articles