Tuesday, 25 August 2015

Multiple Button Using This in Android


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
    
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>


    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button1"
       
android:id="@+id/button1"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true"
/>

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button2"
       
android:id="@+id/button2"
       
android:layout_below="@+id/button1"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="50dp"
/>

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button3"
       
android:id="@+id/button3"
       
android:layout_centerVertical="true"
       
android:layout_centerHorizontal="true"
/>

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button4"
       
android:id="@+id/button4"
       
android:layout_below="@+id/button3"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="41dp"
/>
</
RelativeLayout


MainActivity.Java
----------------------------
package com.anil.android.multiplebuttonusingthis;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button b1,b2,b3,b4;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.button1);

        b2=(Button)findViewById(R.id.button2);

        b3=(Button)findViewById(R.id.button3);

        b4=(Button)findViewById(R.id.button4);

        

        b1.setOnClickListener(this);

        b2.setOnClickListener(this);

        b3.setOnClickListener(this);

        b4.setOnClickListener(this);

        



    }



    @Override

    public void onClick(View v) {

        switch (v.getId()){

         case R.id.button1:

  Toast.makeText(getApplicationContext(),"Button1 is Working",5000).show();

            break;

         case R.id.button2:

    Toast.makeText(getApplicationContext(),"Button2 is Working",5000).show();

                break;

          case R.id.button3:

     Toast.makeText(getApplicationContext(),"Button3 is Working",5000).show();

                break;

         case R.id.button4:

      Toast.makeText(getApplicationContext(),"Button4 is Working",5000).show();

                break;

        }

    }

}
 

Output is:
------------


























No comments:

Post a Comment