postheadericon Android Button onClick Example that implements onClickListener

This example explain the how we can listen onClick event  of multiple Android Button by implementing  onClickListener.




MainActivity.java

package com.bipin.rupadiya.button_onclick;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnEnglish = (Button) findViewById(R.id.btnEnglish);
final Button btnHindi = (Button) findViewById(R.id.btnHindi);
final Button btnGujarati = (Button) findViewById(R.id.btnGujarati);
btnEnglish.setOnClickListener(this);
btnHindi.setOnClickListener(this);
btnGujarati.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String msg = "You have Selected ";
if(v.getId()==R.id.btnEnglish)
{
msg=msg+" English";
}
if(v.getId()==R.id.btnGujarati)
{
msg=msg+"Guarati";
}
if(v.getId()==R.id.btnHindi)
{
msg=msg+"Hindi";
}
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}



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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnHindi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnEnglish"
        android:layout_alignRight="@+id/btnEnglish"
        android:layout_below="@+id/btnEnglish"
        android:layout_marginTop="56dp"
        android:text="Hindi" />

    <Button
        android:id="@+id/btnGujarati"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnHindi"
        android:layout_below="@+id/btnHindi"
        android:layout_marginTop="54dp"
        android:text="Gujarati" />

    <Button
        android:id="@+id/btnEnglish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp"
        android:text="English" />

</RelativeLayout>






0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.