Aniket
Aniket

Reputation: 2214

Button background color

Hi, I am working on custom keyboard tool in android it is somewhat same as our default android device keyboard. Below shows the format of my keyboard.

enter image description here

So, here i am setting backgorund color to button but it is looking very clumsy as shown below,

enter image description here

i need to set background color as red same as shown in above screenshot which is default color of button.It changes the dimension of button when i set background color.

Code i have used to set background color is,

int mColor = 0xFFFF0000;
b.setBackgroundColor(mColor);
v.setBackgroundColor(mColor);

So where i am going wrong.

Upvotes: 0

Views: 389

Answers (2)

Boe-Dev
Boe-Dev

Reputation: 1595

enter code hereYour normal button got a design from the system. If you change the color the design will be changed to standard.

You should create you own style for a button with a shape

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270"
        android:endColor="#aaaaaa"
        android:startColor="#555555" />

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

    <corners
        android:bottomLeftRadius="10sp"
        android:bottomRightRadius="10sp"
        android:topLeftRadius="10sp"
        android:topRightRadius="10sp" />

</shape>

create a xml file in your drawable and insert the code above.

When did you want to change the color of your button? If you want to change the color by a click, you should use a selector, like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/shapeButtonRed" android:state_focused="true"/>
    <item android:drawable="@drawable/shapeButtonRed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shapeButton"/>

</selector>

now you have to give you button the android:backgroud"@drawable/selector" the it will change the color by click

Upvotes: 2

Voicu
Voicu

Reputation: 17860

You can use a color filter to achieve the red coloring of the two buttons:

int mColor = 0xFFFF0000;
b.getBackground().setColorFilter(mColor, PorterDuff.Mode.MULTIPLY);
v.getBackground().setColorFilter(mColor, PorterDuff.Mode.MULTIPLY);

while using this import statement:

import android.graphics.PorterDuff;

This will preserve the default design of the button.

Upvotes: 1

Related Questions