How to assign rounding on the button and random colors at the same time?

Asked

Viewed 253 times

2

Hey, guys, I’m having a little doubt I already did a little snooping on the internet and I haven’t found it yet maybe it’s not possible, but come on..

For example I have a button on this button I play random colors on it, I have a List Colors = new Arraylist(); and in this list I keep the colors in Hexadecimal, so far without problems I use the Collections.shuffle() and I take a color randomly from the list and on the button I use the bt1.setBackgroundColor(), my biggest doubt is I want my application Buttons to have rounded corners, I know I have to use something like this

cantosredondodos.xml

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

but if I put this xml as background there in the xml button

<Button
    android:text="Button"
    android:textAllCaps="false"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:background="@drawable/cantosredondodos"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

the backgroud I defined earlier in the code will overwrite the xml part I want to do Radius, would you have a solution? to do everything in the application code? I even thought about making several xmls but I believe it wouldn’t look too cool, as I have more than 10 Buttons wouldn’t be a very viable thing...

1 answer

3


You can set the border programmatically using shape.setStroke(3, borderColor), where the 3 represents the edge and borderColor represents the color of the border using the GradientDrawable. See this function below, which I pass as parameter to view, which would be its button, the backgroundColor and the borderColor. Behold:

public static void customView(View v, int backgroundColor, int borderColor){
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
    shape.setColor(backgroundColor);
    shape.setStroke(3, borderColor);
    v.setBackgroundDrawable(shape);
}

You can customize according to your ideas.

To set a color randomly you can use the class Random. Behold:

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));  
  • opa thanks, here in my case I gave a small adapted : ic void customView(int backgroundColor, Button value){ Shape = new Gradientdrawable(); Shape.setShape(Gradientdrawable.RECTANGLE); Shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 }); Shape.setColor(backgroundColor); //Shape.setStroke(3, borderColor); value.setBackgroundDrawable(Shape); } the colors I have already added to the list in all 18 colors, and in my case I already use the Collections.shuffle not to repeat the colors, but thanks solved my problem

  • 1

    @Cool Rogersmarques... I put it like this for you to adapt, because it’s in a very generic way. abs. good luck

Browser other questions tagged

You are not signed in. Login or sign up in order to post.