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:layout_marginTop="5dp" 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/button" android:layout_centerHorizontal="true" android:layout_marginTop="52dp" /> <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" /> </RelativeLayout>
MainActiivity.Java
-------------------
package com.anil.android.multiplebuttonusingnewonclicklistner; 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 { Button btn1,btn2,btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button); btn2=(Button)findViewById(R.id.button2); btn3=(Button)findViewById(R.id.button3); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"Button1 is Working",5000).show(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"Button2 is Working",5000).show(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"Button3 is Working",5000).show(); } }); } }
Output is :
------------
No comments:
Post a Comment