Fustigador
Fustigador

Reputation: 6459

My Button is not taking the background color

I have two buttons, and I want to add a gradient effect to them.

    colors = new int[]{Color.parseColor("#F0E7AD"), Color.parseColor("#A68813")};
    gd = new GradientDrawable(
    GradientDrawable.Orientation.TOP_BOTTOM, colors);
    entrarButton=findViewById(R.id.entrar);
    loginButton=findViewById(R.id.login);

These buttons have visibility GONE by default. Later in the app:

runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                entrarButton.setVisibility(View.VISIBLE);
                                entrarButton.setBackground(gd);
                            }
                        });

                        if(showLogin){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    loginButton.setVisibility(View.VISIBLE);
                                    loginButton.setBackground(gd);
                                }
                            });

                        }

This way, they are taking the primaryColor dfined in colors.xml. Why aren't my buttons taking the background I want?

EDIT: (Buttons in XML)

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/entrar"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    android:text="ENTRAR"
    android:textColor="@color/black"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/login"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/entrar"
    android:layout_marginTop="20dp"
    android:visibility="gone"
    android:textColor="@color/black"
    android:text="LOGIN"/>

Upvotes: 0

Views: 76

Answers (2)

Simran Sharma
Simran Sharma

Reputation: 892

First, create a gradient file to set as background. We can create it by right-clicking on the drawable folder --> New --> Drawable resource file.

Suppose we have named it as button_background.xml The content of this file can be like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:type="linear"
        android:angle="0"
        android:startColor="#0085FF"
        android:endColor="#00E0FF" />
</shape>

This answer in the link explains the best how to properly create a gradient and use the attributes such as android:angle. https://stackoverflow.com/a/45134682/12555686

Then in the layout file, you can simply put this file as the background.

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/login"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/entrar"
    android:layout_marginTop="20dp"
    app:backgroundTint="@null"
    android:background="@drawable/button_background"
    android:textColor="@color/black"
    android:text="LOGIN"/>

If you don't want to set the background in the XML file and want to do it programmatically, then you can do that as follows:

entrarButton.setVisibility(View.VISIBLE);
entrarButton.setBackground(R.drawable.button_background);

And your gradient background for the button is set.

And if you want the background color to change according to states like state_pressed, state_selected or state_enabled. Then create another drawable-resource file and keep that file as the background. Link your gradient file inside the newly created background file and fill in the contents as the above-mentioned answer (just replace the color with your gradient file). https://stackoverflow.com/a/65826409/12555686

Also, I couldn't really understand why do your buttons have the attribute android:visibility="gone". That will just set the visibility to gone. And why are you changing stuff about the buttons in runOnUiThread(new Runnable() { }). Because you can do that directly without runOnUiThread(new Runnable() { }).But if that is what your code demands then absolutely fine. Refer to the steps above.

And you're done. I hope this works. If any errors or corrections then please contact me by commenting.

Upvotes: 2

Emon Hossain Munna
Emon Hossain Munna

Reputation: 755

Go For Background XML like that one!

 <?xml version="1.0" encoding="utf-8"?>  

 <selector
 xmlns:android="http://schemas.android.com/apk/res/android"> 
     <!--When Button is not enabled -->
     <item android:state_enabled="false" android:color="#b6b7b5" /> 
     <!--When Button is in pressed state -->
     <item android:state_pressed="true" android:color="#22a540" /> 
     <!--When Button is in selected state -->
     <item android:state_selected="true" android:color="#fabcff" /> 
     <!--Default Background Color -->
     <item android:color="@android:color/white" />     
</selector>

Set Button Click Listener and then change the background with respect to the button state! Hope It Would Solve Your Issue!

Upvotes: 0

Related Questions