Creating And Handling Custom Button

The project describes how to implement Custom Button and display a message when button is pressed.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a Project CustomButton
2.) Open and insert following in main.xml:


android:orientation="vertical" android:layout_gravity="center_horizontal"
android:layout_width="wrap_content" android:layout_height="fill_parent">

<button> android:layout_height="wrap_content" android:focusable="true"
android:text="Click Me" android:background="@drawable/btn_blue" android:onClick="@drawable/btn_green"/>



3.) Open and insert following in strings.xml:


Hello World, CustomButton!

You have Clicked Me



4.) Insert some button image into your drawable folder.

5.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. CustomButton. Enter following information:

Project name: CustomButton
Build Target: Android 2.3.3
Application name: CustomButton
Package name: org.example.CustomButton
Create Activity: CustomButton

2.) On Clicking Finish CustomButton code structure is generated with the necessary Android Packages being imported along with CustomButton class will look like following:


package org.example.CustomButton;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class CustomButton extends Activity implements OnClickListener {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
View Button = findViewById(;
public void onClick(View v)
switch (v.getId())

private void openMessageWindow()
new AlertDialog.Builder(this)
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,
int i) {



Output – The final output: