Thursday, 5 April 2018

android checkbox example

06:34


Andorid check box example on Order_Coffee:

(1)res/layout/Coffe.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/coffee">

<
TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="25dp"
android:textColor="@color/title"
android:textStyle="bold"/>

<
EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:hint="@string/hint"
android:textColorHint="#000000"/>

<
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<
TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/toppings"
android:textStyle="bold"/>

<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/extra"
android:textStyle="bold"/>
</
LinearLayout>

<
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<
CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cb1" />

<
CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:text="@string/cb3" />
</
LinearLayout>

<
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<
CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cb2" />

<
CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cb4" />
</
LinearLayout>

<
TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/quantity1"
android:textStyle="bold"
android:textColor="@color/quantity"
android:textSize="18dp"/>
<
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal">

<
Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:onClick="decrement"
android:text="@string/negative"
android:textColor="@color/negative"
android:background="#000000"/>

<
TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="@string/zero"
android:textColor="@color/zero"
android:textSize="16sp" />

<
Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:onClick="increment"
android:text="@string/positive"
android:textColor="@color/positive"
android:background="#000000"/>

</
LinearLayout>

<
TextView
android:id="@+id/order_summary"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:hint="@string/order"
android:textColor="@color/order"/>

<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:text="@string/price"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="16sp" />

<
TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:text="0.00"
android:textColor="@color/cost"
android:textSize="16sp" />

<
Button
android:id="@+id/Order_button"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:onClick="ORDER"
android:text="@string/Order"
android:textColor="@color/order"
android:background="#000000"/>

</
LinearLayout>

(2)MainActivity.java
package com.example.rohan.order_coffee;



import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;



import android.view.View;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

int quantity=0;



    @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.coffee);

    }

public void decrement(View view){

if(quantity>0){

            quantity--;

        }

        displayquantity(quantity);

    }



public void increment(View view){

        quantity++;

        displayquantity(quantity);

    }

private void displayquantity(int quantity){

        TextView quantity1=(TextView) findViewById(R.id.quantity);

        quantity1.setText(""+quantity);

    }



public void ORDER(View view){

        EditText name =(EditText)findViewById(R.id.name);

        String names =name.getText().toString();

        CheckBox checkBox1 =(CheckBox)findViewById(R.id.cb1);

        Boolean check =checkBox1.isChecked();

        CheckBox checkBox2 =(CheckBox)findViewById(R.id.cb2);

        Boolean checkb =checkBox2.isChecked();

int price = calcuclateprice(check,checkb);

        String finalmessage =createfinalmessage(names,check,checkb,price);

        displayMessage(finalmessage);

    }

private String createfinalmessage(String names, Boolean check, Boolean checkb, int price) {

        String message ="Customer Name: "+names+"\n"+"Coffee with Wipped Cream ordered: "+check+"\n"+"Coffee with Chocolate ordered: "+checkb+"\n"+"Total Price: "+price;

return message;



    }

private int calcuclateprice(Boolean check,Boolean checkb){

int price=15;

if(check)

        {

            price=price*1;



        }

if (checkb)

        {

            price=price*2;



        }

return quantity*price;

    }

private void displayMessage(String finalmessage){

        TextView Message=(TextView)findViewById(R.id.price);

        Message.setText(""+finalmessage);

    }

}

(3)res/values/strings.xml
<resources>

<string name="app_name">Order_Coffee</string>

<string name="title">Order Your Coffee Here...!!</string>

<string name="hint">Enter Your Name</string>

<string name="toppings">TOPPINGS</string>

<string name="extra">Extras</string>

<string name="quantity1">Quantity</string>

<string name="cb1">Whipped Cream</string>

<string name="cb2">Chocolate</string>

<string name="cb3">Caffe Americano</string>

<string name="cb4">Caffe Cubano</string>

<string name="quantity">Quantity</string>

<string name="negative">-</string>

<string name="zero">0</string>

<string name="positive">+</string>

<string name="order">YOUR ORDER</string>

<string name="price">PRICE</string>

<string name="Order">ORDER</string>



</resources>

(4)res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="colorPrimary">#3F51B5</color>

<color name="colorPrimaryDark">#303F9F</color>

<color name="colorAccent">#FF4081</color>

<color name="title">#000000</color>

<color name="quantity">#000000</color>

<color name="negative">#ffffff</color>

<color name="zero">#000000</color>

<color name="positive">#ffffff</color>

<color name="order">#ffffff</color>

<color name="price">#000000</color>

<color name="cost">#000000</color>

</resources>

OP  :-

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING AT VISSICOMP BY OM SIR. All rights resevered. Designed by Templateism

Back To Top