Monday, June 18, 2012

How To Create And Hadle Custom Button Into Your Application


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:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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:id="@+id/next_button" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:focusable="true"
                android:text="Click Me" android:background="@drawable/btn_blue"android:onClick="@drawable/btn_green"/>
</LinearLayout>
3.) Open and insert following in strings.xml:
<resources>
    <string name="hello">Hello World, CustomButton!</string>
    <string name="app_name">CustomButton</string>
    <array name="buttonarray">
        <item name="label">You have Clicked Me</item>
    </array>
</resources>
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:

No comments:

Post a Comment