0

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"/>
Fustigador
  • 6,339
  • 12
  • 59
  • 115

2 Answers2

2

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.

Simran Sharma
  • 852
  • 7
  • 16
  • It is inside a runOnUiThread because that code its in another thread, in fact it in the onResponse method of OkHttpClient. Regarding your answer, I know it SHOULD work, but for some strange reason I keep getting my buttons in the primaryColor defined in colors.xml... – Fustigador Jan 21 '21 at 12:11
  • Also, I want them gone because I want a video to play, and when the video has ended, THEN make them visible – Fustigador Jan 21 '21 at 12:12
  • As I said in my post if your code demands that, then go for it. I have added how to set the background in the code itself. Check the edit and inform me if that worked or not. – Simran Sharma Jan 21 '21 at 12:13
  • Its not working, and I really don't know why...because that code looks perfectly fine. I keep getting my buttons in the primaryColor. Also, it should be entrarButton.setBackground(getDrawable(R.drawable.gradient)); – Fustigador Jan 21 '21 at 12:19
  • Why don't you use `Log` or `System.out.println()` statements to check if the compiler is executing the code of the line properly or not. – Simran Sharma Jan 21 '21 at 12:21
  • The buttons are visible after the video plays, so Im pretty sure the code is reaching that point... – Fustigador Jan 21 '21 at 12:25
  • Yeah, the log is showing. Tried also to delete the visibility thing out of the XML and the result is the same. Gonna try to go to another activity after the video plays instead of setting the visibility to VISIBLE, and draw there the buttons. – Fustigador Jan 21 '21 at 12:43
  • Nothing. Still is not working...Even in the Design tab of the layout, by setting the background to the gradient.xml file, it shows the primaryColor instead of the gradient. – Fustigador Jan 21 '21 at 12:55
  • Ok. If you do get the solution, then please inform me as well. Even I am curious about how will this get solved. – Simran Sharma Jan 21 '21 at 12:58
  • Just solved it. I needed to add the line app:backgroundTint="@null" in order to get it working. Looks like the tint is overriding the background you put there. Someone in google should fix that. Anyway, thank you very much for your help, Im marking your answer as correct. – Fustigador Jan 21 '21 at 13:02
  • Oh, so it was the `backgroundTint`. I will remember it for future instances. Thank you for making me learn something new. Till then I will edit the answer. – Simran Sharma Jan 21 '21 at 13:07
0

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!