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:gravity="center_vertical|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!
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.java. CustomButton class will look like following:

 

package org.example.CustomButton;

import android.app.Activity;
import android.app.AlertDialog;
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Button = findViewById(R.id.next_button);
Button.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.next_button:

openMessageWindow();
break;
}
}
private void openMessageWindow()
{
new AlertDialog.Builder(this)
.setTitle(null)
.setItems(R.array.buttonarray,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,
int i) {
}
})
.show();
}
}

 

 

Output – The final output: