Thursday, 5 April 2018

android radio button example

06:38


(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();

        }

    };



}



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