-1

I want to create a custom Button in my app, like the image below: enter image description here

This is a Button from an app where when I press it, another Activity starts.

How can I create a Button like this?
I'll appreciate any help.

Editted:

button_back.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

<gradient
    android:angle="90"
    android:startColor="#4080FE"
    android:centerColor="#4080FE"
    android:endColor="#4080FE"
    android:type="linear" />

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



<size
    android:width="40dp"
    android:height="40dp"
    />

and in layout :

<Button
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:drawableLeft="@drawable/btn"
    android:background="@drawable/button_back" 

    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="94dp" />

and this is the output: enter image description here

I don't want those red circles to be shown; I want my button be the same as first picture without any boredr.

Thank you for your helping.

Last Edition:

I used imageButton instead of button and it is correct solution for my case.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Samaneh Rezaei
  • 73
  • 2
  • 11

5 Answers5

2

Try this way to get the shape for your view

create go_btn.xml

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

    <gradient
        android:angle="90"
        android:startColor="#4080FE"
        android:centerColor="#4080FE"
        android:endColor="#4080FE"
        android:type="linear" />

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



    <size
        android:width="40dp"
        android:height="40dp"
        />


</shape>

and then add it to your layout

<ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:padding="10dp"
        android:src="@drawable/autoorder"
        android:background="@drawable/go_btn"
        android:layout_gravity="center"
android:layout_marginTop="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calender"
        android:layout_gravity="center"
        />

OUTPUT

enter image description here

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
1

You can create Similar like this using TextView

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:drawableTop="@drawable/checked"
        android:gravity="center"
        android:text="Calander" />
</RelativeLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

I think you better use a TextView and set drawableTop for it, like below

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/calendar"
    android:textSize="@dimen/textSizeSmall_SP"
    android:textColor="@color/textColor"
    android:gravity="center"
    android:text="Calender"
    android:paddingLeft="@dimen/smallMargin_SP"
    android:paddingRight="@dimen/smallMargin_SP"
    android:background="@drawable/green_border_no_corners"/>

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anil
  • 1,605
  • 1
  • 14
  • 24
0

There's a lot of different ways:

Create a new LayerList with round corners and your Drawable like this: https://stackoverflow.com/a/21002306/5778152

Then in your XML file, create the Button and use the LayerList you've created.

Or you can create a LinearLayout, use a shape with round corners for the background and inside it, add an ImageView to set your icon

Community
  • 1
  • 1
Nicolas Cortell
  • 659
  • 4
  • 16
0

Create an xml resource file and add this resource to button background attribute:-)