Saturday 23 February 2013

Working with Multiple Buttons

Demo:





1)main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Button4" />

</LinearLayout>

2)Activity: 

package com.venky.multiplebuttonsdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MultipleButtonsActivity extends Activity implements
            OnClickListener {
      /** Called when the activity is first created. */

      Button btn1, btn2, btn3, btn4;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btn1 = (Button) findViewById(R.id.button1);
            btn2 = (Button) findViewById(R.id.button2);
            btn3 = (Button) findViewById(R.id.button3);
            btn4 = (Button) findViewById(R.id.button4);

            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
      }

      @Override
      public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
            case R.id.button1:

                  Toast.makeText(getApplicationContext(), "Button1 is Clicked",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.button2:

                  Toast.makeText(getApplicationContext(), "Button2 is Clicked",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.button3:

                  Toast.makeText(getApplicationContext(), "Button3 is Clicked",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.button4:

                  Toast.makeText(getApplicationContext(), "Button4 is Clicked",
                              Toast.LENGTH_SHORT).show();
                  break;
            }
      }
}


3)Download this Project Click Here 

No comments:

Post a Comment