Screenshots:
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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Developed by Shrikant" />
<Button
android:id="@+id/fatbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Fat" />
<Button
android:id="@+id/catbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fatbtn"
android:layout_below="@+id/fatbtn"
android:layout_marginTop="22dp"
android:text="Cat" />
<Button
android:id="@+id/sadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/catbtn"
android:layout_below="@+id/catbtn"
android:layout_marginTop="27dp"
android:text="Sad" />
<Button
android:id="@+id/ratbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sadbtn"
android:layout_below="@+id/sadbtn"
android:layout_marginTop="16dp"
android:text="Rat" />
<Button
android:id="@+id/matbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratbtn"
android:layout_below="@+id/ratbtn"
android:layout_marginTop="18dp"
android:text="Mat" />
</RelativeLayout>
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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Developed by Shrikant" />
<Button
android:id="@+id/fatbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Fat" />
<Button
android:id="@+id/catbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fatbtn"
android:layout_below="@+id/fatbtn"
android:layout_marginTop="22dp"
android:text="Cat" />
<Button
android:id="@+id/sadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/catbtn"
android:layout_below="@+id/catbtn"
android:layout_marginTop="27dp"
android:text="Sad" />
<Button
android:id="@+id/ratbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sadbtn"
android:layout_below="@+id/sadbtn"
android:layout_marginTop="16dp"
android:text="Rat" />
<Button
android:id="@+id/matbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratbtn"
android:layout_below="@+id/ratbtn"
android:layout_marginTop="18dp"
android:text="Mat" />
</RelativeLayout>
MainActivity.java:
package com.example.workingwithmultiplebutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btnrat,btncat,btnsad,btnmat,btnfat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnrat=(Button)findViewById(R.id.ratbtn);
btncat=(Button)findViewById(R.id.catbtn);
btnsad=(Button)findViewById(R.id.sadbtn);
btnmat=(Button)findViewById(R.id.matbtn);
btnfat=(Button)findViewById(R.id.fatbtn);
btnrat.setOnClickListener(this);
btncat.setOnClickListener(this);
btnsad.setOnClickListener(this);
btnmat.setOnClickListener(this);
btnfat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ratbtn:
Toast.makeText(getApplicationContext(), "I am Rat", Toast.LENGTH_SHORT).show();
break;
case R.id.catbtn:
Toast.makeText(getApplicationContext(), "I am Cat", Toast.LENGTH_SHORT).show();
break;
case R.id.sadbtn:
Toast.makeText(getApplicationContext(), "I am Sad", Toast.LENGTH_SHORT).show();
break;
case R.id.matbtn:
Toast.makeText(getApplicationContext(), "I am Mat", Toast.LENGTH_SHORT).show();
break;
case R.id.fatbtn:
Toast.makeText(getApplicationContext(), "I am Fat", Toast.LENGTH_SHORT).show();
break;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btnrat,btncat,btnsad,btnmat,btnfat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnrat=(Button)findViewById(R.id.ratbtn);
btncat=(Button)findViewById(R.id.catbtn);
btnsad=(Button)findViewById(R.id.sadbtn);
btnmat=(Button)findViewById(R.id.matbtn);
btnfat=(Button)findViewById(R.id.fatbtn);
btnrat.setOnClickListener(this);
btncat.setOnClickListener(this);
btnsad.setOnClickListener(this);
btnmat.setOnClickListener(this);
btnfat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ratbtn:
Toast.makeText(getApplicationContext(), "I am Rat", Toast.LENGTH_SHORT).show();
break;
case R.id.catbtn:
Toast.makeText(getApplicationContext(), "I am Cat", Toast.LENGTH_SHORT).show();
break;
case R.id.sadbtn:
Toast.makeText(getApplicationContext(), "I am Sad", Toast.LENGTH_SHORT).show();
break;
case R.id.matbtn:
Toast.makeText(getApplicationContext(), "I am Mat", Toast.LENGTH_SHORT).show();
break;
case R.id.fatbtn:
Toast.makeText(getApplicationContext(), "I am Fat", Toast.LENGTH_SHORT).show();
break;
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.