Wednesday, August 29, 2012

Using shape to define round drawable in Android

Using shape to define round drawable

To define our shape as a drawable, create XML(s) under /res/drawable/ folder.

/res/drawable/redroundrect.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
 <solid
  android:color="#FFFF0000"/>
 <corners
  android:radius="15dp" />
 <padding
  android:left="20dp"
  android:top="20dp"
  android:right="20dp"
  android:bottom="2dp" />
</shape>


/res/drawable/greenroundrect.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
 <solid
  android:color="#FF00FF00"/>
 <corners
  android:radius="15dp" />
 <padding
  android:left="20dp"
  android:top="20dp"
  android:right="20dp"
  android:bottom="2dp" />
</shape>


/res/drawable/blueroundrect.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
 <solid
  android:color="#FF0000FF"/>
 <corners
  android:radius="15dp" />
 <padding
  android:left="20dp"
  android:top="20dp"
  android:right="20dp"
  android:bottom="2dp" />
</shape>
Using it inside main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="RED in XML"
    android:background="@drawable/redroundrect" />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="GREEN in XML"
    android:background="@drawable/greenroundrect" />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="BLUE in XML"
    android:background="@drawable/blueroundrect" />
<TextView 
    android:id="@+id/javared"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="RED using Java"/>
<TextView 
    android:id="@+id/javagreen"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="GREEN using Java"/>
<TextView 
    android:id="@+id/javablue"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="BLUE using Java"/>
</LinearLayout>



In Java code


package com.AndroidTestResources;
 
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.TextView;
 
public class AndroidTestResourcesActivity extends Activity {
  
 TextView javaRed, javaGreen, javaBlue;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        javaRed = (TextView)findViewById(R.id.javared);
        javaGreen = (TextView)findViewById(R.id.javagreen);
        javaBlue = (TextView)findViewById(R.id.javablue);
         
        Drawable redRoundRect
        = getResources().getDrawable(R.drawable.redroundrect);
        Drawable greenRoundRect
        = getResources().getDrawable(R.drawable.greenroundrect);
        Drawable blueRoundRect
        = getResources().getDrawable(R.drawable.blueroundrect);
         
        javaRed.setBackgroundDrawable(redRoundRect);
        javaGreen.setBackgroundDrawable(greenRoundRect);
        javaBlue.setBackgroundDrawable(blueRoundRect);
    }
}

No comments:

Post a Comment