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.

2 comments:

  1. thanks for sharing the information.
    Indian Cyber Army’s most awaited internship is live now. Summer Internship 2018 on “ Ethical hacking” and book your seats before it runs out.Candidates have to get themselves registered to be a part of this Internship program. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization.As career in ethical hacking is most in demand.

    ReplyDelete
  2. thanks for sharing the more valuable information.
    Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
    Ethical hacking course

    ReplyDelete

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top