Check Box Android Tutorial
You can select check box and show toast message selected check box.
First Create main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ChechboxActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:text="A" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:text="C" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="32dp"
android:text="B" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox3"
android:layout_alignBottom="@+id/checkBox3"
android:layout_alignLeft="@+id/checkBox2"
android:text="D" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox4"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="Button" />
</RelativeLayout>
After Create Java file
package pack.chech;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class ChechboxActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
CheckBox c,c1,c2,c3;
RadioButton r;
RadioGroup rg;
//TextView t,t1;
//EditText e,e1;
//RatingBar r;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
c = (CheckBox)findViewById(R.id.checkBox1);
c1 = (CheckBox)findViewById(R.id.checkBox2);
// c2 = (CheckBox)findViewById(R.id.checkBox3);
//c3 = (CheckBox)findViewById(R.id.checkBox4);
/* t = (TextView)findViewById(R.id.textView1);
t1 = (TextView)findViewById(R.id.textView2);
e = (EditText)findViewById(R.id.editText1);
e1 = (EditText)findViewById(R.id.editText2);*/
rg = (RadioGroup)findViewById(R.id.radioGroup1);
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = rg.getCheckedRadioButtonId();
r = (RadioButton)findViewById(id);
Toast.makeText(this, r.getText().toString(), 2000).show();
if(c.isChecked())
{
Toast.makeText(this, c.getText().toString(), 2000).show();
}
if(c1.isChecked())
{
Toast.makeText(this, c1.getText().toString(), 2000).show();
}
}
}
OUTPUT :
Share my article ......
You can select check box and show toast message selected check box.
First Create main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ChechboxActivity" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:text="A" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:text="C" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="32dp"
android:text="B" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox3"
android:layout_alignBottom="@+id/checkBox3"
android:layout_alignLeft="@+id/checkBox2"
android:text="D" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox4"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="Button" />
</RelativeLayout>
After Create Java file
package pack.chech;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class ChechboxActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
CheckBox c,c1,c2,c3;
RadioButton r;
RadioGroup rg;
//TextView t,t1;
//EditText e,e1;
//RatingBar r;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
c = (CheckBox)findViewById(R.id.checkBox1);
c1 = (CheckBox)findViewById(R.id.checkBox2);
// c2 = (CheckBox)findViewById(R.id.checkBox3);
//c3 = (CheckBox)findViewById(R.id.checkBox4);
/* t = (TextView)findViewById(R.id.textView1);
t1 = (TextView)findViewById(R.id.textView2);
e = (EditText)findViewById(R.id.editText1);
e1 = (EditText)findViewById(R.id.editText2);*/
rg = (RadioGroup)findViewById(R.id.radioGroup1);
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = rg.getCheckedRadioButtonId();
r = (RadioButton)findViewById(id);
Toast.makeText(this, r.getText().toString(), 2000).show();
if(c.isChecked())
{
Toast.makeText(this, c.getText().toString(), 2000).show();
}
if(c1.isChecked())
{
Toast.makeText(this, c1.getText().toString(), 2000).show();
}
}
}
OUTPUT :
Share my article ......
No comments :
Post a Comment