I need perfect circle button in my app. Which scales automatically accordingly to the screen size(height of the layout it is in.)
My xml file : circle1try.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<corners
android:radius="1000dp"
/>
<solid
android:color="#ff00b3ff"
/>
</shape>
the place where I use it in my activity layout :
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="30dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button1Key"
android:typeface="monospace"
android:textSize="20dp"
android:background="@drawable/circle1try"
android:textColor="#ffffffff"
android:text="tap here"
android:layout_gravity="center"/>
</LinearLayout>
The entire LinearLayout
lies in another vertical LinearLayout
and I use layout_weight
to make sure the space occupied by the layouts have similar proportions on all screens.
This code snippet from the java file related to the button
roundButton=(Button)findViewById(R.id.button1Key);
int btnSize=roundButton.getLayoutParams().height;
roundButton.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
and in my app depending on the user actions, I also need to change the color for which I use the following snippet in the same class.
bgShape = (GradientDrawable)roundButton.getBackground();
bgShape.setColor(Color.BLACK);
However the button I get using this process is not a complete circle. It is close to a circle but wide along the x axis. So how do I get a perfectly round button whose color can be easily changed ?