Tuesday, 25 August 2015

OnclickProperties Using Multiple Button

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"
   
tools:context=".MainActivity">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button1"
       
android:id="@+id/button"
       
android:onClick="show1"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="45dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button2"
       
android:id="@+id/button2"
       
android:layout_below="@+id/button"
        android:onClick="show2"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="75dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Button3"
       
android:id="@+id/button3"
       
android:onClick="show3"
       
android:layout_marginTop="53dp"
       
android:layout_below="@+id/button2"
       
android:layout_alignLeft="@+id/button2"
       
android:layout_alignStart="@+id/button2" />
</
RelativeLayout>


MainActivity.java
package com.anil.android.multiplebuttonusingonclickproperties;



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.Toast;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    //calling the method names 

    public void show1(View v){
Toast.makeText(getApplicationContext(),"Button1 is Working",5000).show();

    }

    public void show2(View v){
Toast.makeText(getApplicationContext(),"Button2 is Working",5000).show();

    }
public void show3(View v){

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

    }
}
-------------------

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









No comments:

Post a Comment