Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a circular imageButton in android? [duplicate]

Guys i have created a imageButton in my layout file and set a circular png image as its background.but when i am running my application, its displaying me a square button with my given image placed in the middle of it.

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sliderr" />
like image 643
Sritam Jagadev Avatar asked Jan 21 '15 12:01

Sritam Jagadev


People also ask

How do I make an image a circle button?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/actiivity_main. xml. In the above code, we have taken the button with a round_button background.


2 Answers

add android:background="@null"

like image 160
klimat Avatar answered Sep 19 '22 13:09

klimat


Make a new shape XML in res/drawable/round_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <gradient 
        android:startColor="#FFFF0000" 
        android:endColor="#80FF00FF"
        android:angle="270" />
</shape>

Use this shape as the button's background:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/round_button"
    android:src="@drawable/sliderr" />

You may also want to add android:scaleType="fitCenter" to your button to make the image the same size as the button, and android:adjustViewBounds="true" if your image has unequal height/width.

like image 35
Akash Avatar answered Sep 21 '22 13:09

Akash