2

I trying to make a circular image button like WhatsApp.

Enter image description here

I tried the below code, but I am only able to create a circular imageButton. The image did not show in the imageButton.

 <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="70dp"
        android:paddingBottom="140dp"
        android:src = "@drawable/camera"
        android:scaleType="fitXY"
        android:layout_height="70dp"
        android:background="@drawable/round"
        android:layout_gravity="center_horizontal" />

round.xml

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

    <corners android:radius="100dp" />

    <stroke
        android:width="5dp"
        android:color="#090" />

</shape>

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John
  • 684
  • 11
  • 35

8 Answers8

4

You added android:paddingBottom=140dp. Issue with that. Check this

<ImageButton
        android:id="@+id/imageButton"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/round"
        android:tint="@android:color/black"
        android:scaleType="fitXY"
        android:padding="5dp"
        android:src="@drawable/camera" />
Ankita Shah
  • 1,866
  • 16
  • 31
1

You just missed solid tag in your custom shape file.

<solid android:color="#090" /> 

Edit:

Never try to give margin or padding which has a larger value. Just like you are using android:paddingBottom="140dp". This is not a recommended way.

Piyush
  • 18,895
  • 5
  • 32
  • 63
1

For drawing a solid circle, try the code below:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="#090"/>

    <size
        android:width="100dp"
        android:height="100dp"/>
</shape>

Shape type oval is used for drawing circles.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
1

WhatsApp used a floating action button, and you can implement it in two steps.

Add a dependency to your build.gradle file:

dependencies {
    compile 'com.github.clans:fab:1.6.4'
}

Add com.github.clans.fab.FloatingActionButton to your layout XML file.

<com.github.clans.fab.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    android:src="@drawable/ic_message"
    fab:fab_colorNormal="@color/app_primary"
    fab:fab_colorPressed="@color/app_primary_pressed"
    fab:fab_colorRipple="@color/app_ripple"/>

Download image of camera by clicking here.

And you are done.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
1

Try this

<ImageButton
        android:id="@+id/imageButton"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/round"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher" />
Gulnaz Ghanchi
  • 485
  • 3
  • 14
1

Use a shape and fill with your desire color.

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

    <corners android:radius="100dp" />
    <solid android:color="#090" />  // solid for filling empty space

</shape>

Use code in ImageButton

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="70dp"
    android:paddingBottom="140dp"
    android:src = "@drawable/camera"
    android:scaleType="fitXY"
    android:layout_height="70dp"
    android:background="@drawable/round"
    android:layout_gravity="center_horizontal" />
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
1

Add Solid Tag

<solid android:color="#090" /> 

and replace android:paddingBottom="140dp" with android:padding="whateversuitsyou"

Tulsi
  • 719
  • 7
  • 15
0

Set width and height as below.

android:layout_width="match_parent"
android:layout_height="wrap_content"

This works for me. Try this...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131