2

I am having following xml file.I want to make the shape of button circle with some background color.but my each button will contain different background color.For one button i can do it by taking xml in drawable and define shape and color for button.but by this method i need to create separate xml file for each button for shape and color.but i want to do it in only one xml file which contain shape and different color so that i can reduce xml files in drawable

<?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"
                    android:orientation="vertical" >

        <Button android:id="@+id/button"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                     />
        <Button android:id="@+id/button1"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                     />
        <Button android:id="@+id/button2"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                     />
        </LinearLayout>
ham-sandwich
  • 3,975
  • 10
  • 34
  • 46
Dhiraj
  • 870
  • 2
  • 12
  • 25

1 Answers1

1

Define shape roundbutton.xml like following:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring">
        <solid android:color="#FFFFFF"/>

    </shape>

Then assign shape to your button

 <Button
            android:id="@+id/round1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/roundbutton"
            android:gravity="center" />

After that you can use java to change color

 round1.setBackgroundColor(Color.BLACK);

And you need to import android.graphics.Color; not: import android.R.color;

Or hex code (not 3-byte) 0xFF000000 where the first byte sets the alpha.

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32