Thursday, 5 April 2018

android radio button example


(1)write  code for   activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   
android:layout_height="match_parent" android:fitsSystemWindows="true"
   
tools:context="com.xaviercollege.ecommerce.practical6.Main3Activity">

    <
android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
       
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <
android.support.v7.widget.Toolbar android:id="@+id/toolbar"
           
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
           
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </
android.support.design.widget.AppBarLayout>

    <
include layout="@layout/content_main3" />

    <
android.support.design.widget.FloatingActionButton android:id="@+id/fab"
       
android:layout_width="wrap_content" android:layout_height="wrap_content"
       
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
       
android:src="@android:drawable/ic_dialog_email" />

</
android.support.design.widget.CoordinatorLayout>

(2)write code for content_main.xml file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent"

    android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main3"

    tools:context="com.xaviercollege.ecommerce.practical6.Main3Activity">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView" />



    <RadioGroup

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@+id/textView"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginLeft="36dp"

        android:layout_marginStart="36dp"

        android:layout_marginTop="64dp">



        <RadioButton

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Cash On Delivery"

            android:id="@+id/radioButton2"

            android:checked="false" />



        <RadioButton

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Credit Card"

            android:id="@+id/radioButton1"

            android:checked="false" />



        <RadioButton

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Debit Card"

            android:id="@+id/radioButton"

            android:checked="false" />

    </RadioGroup>

</RelativeLayout>

(3)write  code for MainAcitivity3.java file:

package com.xaviercollege.ecommerce.practical6;



import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.RadioButton;

import android.widget.TextView;

import android.widget.Toast;



public class Main3Activity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main3);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar.make(view, "Replace with your own action", 
Snackbar.LENGTH_LONG)

                        .setAction("Action", null).show();

            }

        });



        RadioButton radioFivestar=(RadioButton)findViewById(R.id.radioButton1);

        RadioButton radioThreestar=(RadioButton)findViewById(R.id.radioButton2);

        RadioButton radio_One=(RadioButton)findViewById((R.id.radioButton));

        radioFivestar.setOnClickListener(radioListener);

        radio_One.setOnClickListener(radioListener);

        radioThreestar.setOnClickListener(radioListener);



    }



    public View.OnClickListener radioListener=new View.OnClickListener(){

        public void onClick(View view){

            TextView selectedhotel=(TextView)findViewById(R.id.textView);

            RadioButton rb=(RadioButton)view;

            selectedhotel.setText("your order"+rb.getText());

            Toast.makeText(getApplicationContext(),rb.getText(),Toast.LENGTH_LONG).show();

        }

    };



}



android checkbox example


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  :-

Recent Posts

Powered by Blogger.

Pages

About Me

My Photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

 

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

Back To Top