Using shape to define round drawable

Using shape to define round drawable

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

/res/drawable/redroundrect.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<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
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<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
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<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);
}
}



Related: Define color drawable in XML



0 Response to "Using shape to define round drawable"

Posting Komentar