Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Button with rounded corners in android [duplicate]

enter image description here

I'm trying to create a button which looks as in the image above. Initially my idea was to create a 9 patch and set it as the button background. But since, this is a plain button, i think we can somehow draw this without using any images.

The button background color is #0c0c0c The border color is #1a1a1a The text color is #cccccc

I found a similar question on SO but that creates a gradient -
Android - border for button

like image 310
coderplus Avatar asked Oct 07 '12 18:10

coderplus


2 Answers

The Android Developer's Guide has a detailed guide on this: Shape Drawbables.

You could also simply remove the gradient element from the link you provided:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#1a1a1a" />
</shape>
like image 79
Sam Avatar answered Oct 12 '22 23:10

Sam


   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginBottom="56dp"
    android:text="Button" 
    android:textColor="#FF0F13"
    android:background="@drawable/bbkg"/>//create bbkg.xml in drawable folder

bbkg.xml//button background

   <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" 
    android:drawable="@drawable/pressed" />
  <item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
  </selector>

normal.xml //button background normal state

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#10EB0A"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>   

pressed.xml //button background pressed state

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle"> 
<solid android:color="#FF1A47"/>    
<stroke android:width="3dp"
        android:color="#0FECFF"/>

<corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
</shape>  
like image 26
Raghunandan Avatar answered Oct 12 '22 23:10

Raghunandan