Check box is the state button to check that state is checked or nor. It is like a radio button in radio button in the same class we can choose only one state but by the use of Check box we can take more then one options in the same class.
In xml file we take the foue check boxes and give the different values to each check box. We give the name of courses to each check box.Courses are Android, PHP, Dot Net and java.
In java file we give the amount of all courses and if user select more then one course then the amount will be added. After clicking the button user will get total amount that will be show on the screen for a short period of time because we show total amount in toast.
No need to change in the manifest file. Because we are not adding any additional class and no any information for the permission. So need to change in the manifest file.
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" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Android" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:text="PHP" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:text="Dot Net" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox3"
android:text="Java" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox4"
android:layout_marginTop="100dp"
android:text="Select Course" />
</RelativeLayout>
MainActivity.java
package com.example.checkbox;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
CheckBox pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
android=(CheckBox)findViewById(R.id.checkBox1);
php=(CheckBox)findViewById(R.id.checkBox2);
dotnet=(CheckBox)findViewById(R.id.checkBox3);
java=(CheckBox)findViewById(R.id.checkBox4);
buttonSelect=(Button)findViewById(R.id.button1);
buttonOrder.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(android.isChecked()){
result.append("\nAndroid 9000Rs");
totalamount+=9000;
}
if(php.isChecked()){
result.append("\nPHP 5000Rs");
totalamount+=5000;
}
if(dotnet.isChecked()){
result.append("\nBurger 4000Rs");
totalamount+=4000;
}
if(java.isChecked()){
result.append("\nJava 6000Rs");
totalamount+=6000;
}
result.append("\nTotal: "+totalamount+"Rs");
Toast.makeText(getApplicationContext(), result.toString(), 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;
}
}
In xml file we take the foue check boxes and give the different values to each check box. We give the name of courses to each check box.Courses are Android, PHP, Dot Net and java.
In java file we give the amount of all courses and if user select more then one course then the amount will be added. After clicking the button user will get total amount that will be show on the screen for a short period of time because we show total amount in toast.
No need to change in the manifest file. Because we are not adding any additional class and no any information for the permission. So need to change in the manifest file.
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" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Android" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:text="PHP" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:text="Dot Net" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox3"
android:text="Java" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox4"
android:layout_marginTop="100dp"
android:text="Select Course" />
</RelativeLayout>
MainActivity.java
package com.example.checkbox;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
CheckBox pizza,coffe,burger;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
android=(CheckBox)findViewById(R.id.checkBox1);
php=(CheckBox)findViewById(R.id.checkBox2);
dotnet=(CheckBox)findViewById(R.id.checkBox3);
java=(CheckBox)findViewById(R.id.checkBox4);
buttonSelect=(Button)findViewById(R.id.button1);
buttonOrder.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(android.isChecked()){
result.append("\nAndroid 9000Rs");
totalamount+=9000;
}
if(php.isChecked()){
result.append("\nPHP 5000Rs");
totalamount+=5000;
}
if(dotnet.isChecked()){
result.append("\nBurger 4000Rs");
totalamount+=4000;
}
if(java.isChecked()){
result.append("\nJava 6000Rs");
totalamount+=6000;
}
result.append("\nTotal: "+totalamount+"Rs");
Toast.makeText(getApplicationContext(), result.toString(), 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;
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.checkbox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.checkbox.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment