Android how to set background color on a Button

2

I am trying to change the background color of a Button. I'm in Kotlin on SDK 21 on emulator.

A View and a Button are declared in the layout XML file

<View
    android:id="@+id/myview"
    android:layout_width="64dp"
    android:layout_height="32dp"
    />
<Button
    android:id="@+id/showButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12dp"
    android:text="test"
    />

The API to set the color doesn't seem to work:

    showButton.setBackgroundColor(0xff60a0e0.toInt()) <-- doesnt work

What works is:

    myview.setBackgroundColor(0xff60a0e0.toInt()) <-- works, exact background color
    showButton.setTextColor(0xff000050.toInt()) <-- works, exact text color

After trying further it seems that I can only set the alpha channel of the button, not the color:

    setBackgroundColor( 0xff000000.toInt())  <-- works, opaque
    setBackgroundColor( 0x00000000.toInt())  <-- works, transparent

Also same thing with:

        showButton.setBackgroundColor(Color.GREEN) <-- doesnt work, button is opaque but not green
        showButton.setBackgroundColor(Color.TRANSPARENT) <-- works, button is transparent

Any idea? Did I miss something in the other answers or the documentation?

Here is complete layout, it used to inflate a fragment, if that matters:



    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
           <View
               android:id="@+id/myview"
               android:layout_width="64dp"
               android:layout_height="32dp"
              />
           <Button
               android:id="@+id/showButton"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="12dp"
               android:text="test"
               />
        </LinearLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dictionaryEntryRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layoutManager="LinearLayoutManager"
            />
       </LinearLayout>

android
kotlin
android-button
material-components
material-components-android
asked on Stack Overflow Sep 25, 2019 by user3144772 • edited Oct 12, 2019 by Gabriele Mariotti

4 Answers

1

mButton.setBackgroundColor(ContextCompat.getColor(mContext, R.color.xxx));

answered on Stack Overflow Sep 25, 2019 by 戴文锦 • edited Sep 25, 2019 by 戴文锦
1

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 add this attribute to set background color android:background="#000"

 <Button
            android:id="@+id/showButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fgkdjgdjsf"
            android:background="#000"
            />

Coding:

showButton.setBackgroundColor(resources.getColor(R.color.colorPrimary))
        showButton.setBackgroundColor(Color.BLACK)
answered on Stack Overflow Sep 25, 2019 by Android Geek
1

In your layout, you are using

 <Button
           android:id="@+id/showButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="12dp"
           android:text="test"
           />

if you want to set textSize in Button, you should use

android:textSize="12dp" 

and for background set in button your layout should be like :-

 <Button
    android:id="@+id/showButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:text="test"
    android:background="#ff60a0e0"/>

OR You can also set color in colors.xml as :-

<color name="button_background">#ff60a0e0</color>

and then your button tag in your layout will be as

 <Button
           android:id="@+id/showButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="12dp"
           android:text="test"
           android:background="@color/button_background"/>

Dynamic you can set color as

showButton.setBackgroundColor(ContextCompat.getColor(context!!, R.color.button_background))
answered on Stack Overflow Sep 25, 2019 by Alok Mishra • edited Sep 25, 2019 by Alok Mishra
0

Since you are using a Theme.MaterialComponents.Light.DarkActionBar theme, check the doc and just use the MaterialButton with app:backgroundTint attribute:

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/color_selector"
    android:textColor="#FFF"
    android:text="BUTTON"
    />

where color_selector can be a color or a selector. Something like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="@color/..."/>
</selector>

enter image description here

answered on Stack Overflow Sep 26, 2019 by Gabriele Mariotti

User contributions licensed under CC BY-SA 3.0