In this tutorial, I will show you a customized checkbox. See output below.
Next is add this xml named custom_checkbox_design.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/checked" />
<item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>
Next is your main_layout.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobbies:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckBox
android:id="@+id/chk_chc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_title"
android:button="@drawable/custom_checkbox_design"
android:text="Basketball" />
<CheckBox
android:id="@+id/chk_chc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/chk_chc1"
android:layout_below="@+id/chk_chc1"
android:button="@drawable/custom_checkbox_design"
android:text="Playing Computer" />
<CheckBox
android:id="@+id/chk_chc3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/chk_chc2"
android:layout_below="@+id/chk_chc2"
android:button="@drawable/custom_checkbox_design"
android:text="Reading Books" />
<Button
android:id="@+id/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chk_chc3"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Check" />
</RelativeLayout>
And lastly is your MainActivity.java
package com.client.apps.views.checkbox;
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.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Toast msg;
Button check;
CheckBox chk1, chk2, chk3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check = (Button)findViewById(R.id.btn_check);
chk1 = (CheckBox)findViewById(R.id.chk_chc1);
chk2 = (CheckBox)findViewById(R.id.chk_chc2);
chk3 = (CheckBox)findViewById(R.id.chk_chc3);
check.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_check:
if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, PLaying Computer, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"Playing Computers", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==false && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, Playing Computers", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"Playing Computers, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else
{
msg = Toast.makeText(MainActivity.this,
"Nothing Selected", Toast.LENGTH_SHORT);
msg.show();
}
break;
}
}
}
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.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Toast msg;
Button check;
CheckBox chk1, chk2, chk3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check = (Button)findViewById(R.id.btn_check);
chk1 = (CheckBox)findViewById(R.id.chk_chc1);
chk2 = (CheckBox)findViewById(R.id.chk_chc2);
chk3 = (CheckBox)findViewById(R.id.chk_chc3);
check.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_check:
if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, PLaying Computer, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"Playing Computers", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==false && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==false)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, Playing Computers", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"Playing Computers, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==true)
{
msg = Toast.makeText(MainActivity.this,
"BasketBall, Reading Books", Toast.LENGTH_SHORT);
msg.show();
}
else
{
msg = Toast.makeText(MainActivity.this,
"Nothing Selected", Toast.LENGTH_SHORT);
msg.show();
}
break;
}
}
}
No comments:
Post a Comment