Page 1 of 1

Custom UI Components in Android

#1 H3R3T1C   User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Posted 21 September 2011 - 03:41 PM

In Android as in any other versions of Java we are able to create our own custom UI components and in this tutorial I will be showing you two different ways of creating custom UI components!

To begin we first need to create a new Android application. Create your new app with the following settings:
Posted Image

Part 1: Modify existing components the code way!

In part one we will be modifying an existing UI component. The component we will modifying is EditText by adding text lines to it. This will be final result of part one:
Posted Image

Ok to start off we need to create a new class called LinedEditText in src -> com.h3r3t1c.tutorial.customwdg

Fill in LinedEditText with the following code (Explanation of code to follow):
package com.h3r3t1c.tutorial.customwdg;

import android.content.Context;
import android.widget.EditText;

public class LinedEditText extends EditText{

	private Paint bkg;
	private Rect mRect;
	private Paint lineColor;

	public LinedEditText(Context context) {
		mRect = new Rect();
		bkg = null;
		lineColor = new Paint();
	 * @param context Application context
	 * @param bkg background color of the LinedTextView
	 * @param lColor Line color of the LinedTextView
	public LinedEditText(Context context,Paint bkg, Paint lColor) {
		mRect = new Rect();
		this.bkg = bkg;
		lineColor = lColor;
    protected void onDraw(Canvas canvas) {

        int count = getLineCount();

        Rect r = mRect;
        for (int i = 0; i < count; i++) {

            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, lineColor);



As you can see I have two constructors for this class, the basic and the one where you can set custom colors. To create a basic LinedEditText in your onCreate(...) use the following code:
LinedEditText te = new LinedEditText(this);

The constructor for setting the custom colors can be used like so:
Paint bkg = new Paint();
Paint line = new Paint();
LinedEditText te = new LinedEditText(this,bkg,line);

The result of using the above code is this:
Posted Image

The onDraw(...) method is where all the magic happens for creating the lines!
As you can see from the code we are using a Canvas to draw on.
The purpose of Rect r = mRect; and int baseline = getLineBounds(i, r); is so we can get the bounds of the drawn text in the Edit text so we can draw our lines to be the same length as the text and to have the line right under the text. The purpose of super.onDraw(canvas); is so that we can draw the rest of the stuff that is normally drawn by EditText!

That's it! That is all there is to modifying existing UI components with code!

Part Two: Modifying existing UI components with XML!

To start off we first need to create a new folder called drawable under res. After that we need to add three image to that folder:
Posted Image
Posted Image
Posted Image

The buttons came from this site:

Now in the drawable folder create a file called custom_button.xml
Code for custom_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:state_focused="true" 
    	  android:drawable="@drawable/button1click" />
    <item android:state_focused="true" 
    	  android:drawable="@drawable/button1up" />
    <item android:state_focused="false" 
	  android:drawable="@drawable/button1up" />
	  <item android:state_enabled="false" 
	  android:drawable="@drawable/button1off" />
    <item android:drawable="@drawable/button1click" />

What the above code does is it tells Android what images to apply to the button under different states.

Now go to your and replace the code in your onCreate() method with the following code:
    public void onCreate(Bundle savedInstanceState) {
        Button b = new Button(this);
        Button b2 = new Button(this);
        LinearLayout l = new LinearLayout(this);
        l.addView(b );

This is what our result should look like:
Posted Image

That's all there basically is to this part. The same can be done to other existing UI components!

If we wanted to create our own UI components from scratch all we would have to do is extend View and just override methods onDraw(...) to display our custom UI component!

Full source:

Is This A Good Question/Topic? 0
  • +

Replies To: Custom UI Components in Android

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Posted 21 September 2011 - 05:22 PM

The first example extending the EditText is nice, but the second isn't a custom component, it's just a state list with drawables.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1