Create Android Application:
- File >> New >> Android Application
- Enter Project name: CheckBoxApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
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 ActionBarActivity
{
CheckBox pizza, biryani, burger;
Button btnOrder;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListerToButton();
}
private void addListerToButton() {
pizza =
(CheckBox) findViewById(R.id.checkBox1);
biryani =
(CheckBox) findViewById(R.id.checkBox2);
burger =
(CheckBox) findViewById(R.id.checkBox3);
btnOrder =
(Button) findViewById(R.id.btnOrder);
btnOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int amount = 0;
StringBuilder sb = new StringBuilder();
sb.append("Select
Above Items");
if(pizza.isChecked()){
sb.append("\n
Pizza Prize:150");
amount +=
100;
}
if(biryani.isChecked()){
sb.append("\n
Birayni Prize: 200");
amount +=
200;
}
if(burger.isChecked()){
sb.append("\n
Burger Prize: 180");
amount +=
180;
}
sb.append("\nTotal
Amount:"+amount+"Rs");
Toast.makeText(getApplicationContext(),
sb.toString(), Toast.LENGTH_SHORT).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.main, menu);
return true;
}
}
2.Simple Copy and Past the below code: actvitiy_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="com.ambilpursunil.newapp.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:layout_marginLeft="66dp"
android:layout_marginTop="106dp"
android:text="Pizza"
/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="14dp"
android:text="Burger"
/>
<Button
android:id="@+id/btnOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:layout_marginTop="25dp"
android:text="Order
Now" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignLeft="@+id/checkBox1"
android:layout_marginBottom="39dp"
android:text="Biryani"
/>
</RelativeLayout>
3.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
When our application launch it will show Three Check boxes Just Check one or all and click on the ORDER Button, You will see a Toast Message with Prices of the all items
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... RadioGroup In Android:
No comments:
Post a Comment