Thursday, 22 August 2019

ANDROID PROGRAMMING : Radio Button

ANDROID PROGRAMMING 
Radio Button Usage :




D:\QUIZAPP\app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
 android:layout_height="match_parent"     
android:orientation="horizontal"    >


<Space 
 android:layout_width="match_parent"     
android:layout_height="100sp" />


    <TextView 
 android:id="@+id/tvRg" 
 android:layout_width="match_parent"         
android:layout_height="wrap_content"         
android:gravity="center"         
android:text="You will get exited when you see ______________ " 
 android:textAppearance="?android:attr/textAppearanceMedium" />
 
 <Space 
 android:layout_width="match_parent"         
android:layout_height="match_parent" />

 <RadioGroup 
 android:layout_width="match_parent"         
android:layout_height="wrap_content"         
android:layout_below="@+id/tvRg" 
 android:layout_centerHorizontal="true" 
 android:orientation="horizontal"         
android:showDividers="beginning|middle|end" 
 android:layout_marginTop="10dp" 
 android:id="@+id/radioGroup" >


        <RadioButton 
 android:id="@+id/rb1"             
android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="MOON" 
 android:checked="false" />

        <RadioButton 
 android:id="@+id/rb2" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="SUN"             
android:checked="true" />

        <RadioButton             
android:id="@+id/rb3"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:text="CLOUDS"             
android:checked="false" />

        <RadioButton 
 android:id="@+id/rb4"             
android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="STARS" 
 android:checked="false" />

    </RadioGroup>

    <Button 
 android:id="@+id/btnSubmit" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
android:layout_alignEnd="@+id/tv1"         
android:layout_alignParentTop="true"         
android:layout_marginTop="143dp" 
 android:text="SUBMIT" />
    <Space         
android:layout_width="match_parent" 
 android:layout_height="100sp" /><![CDATA[

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">



    ]]>

    <TextView         
android:id="@+id/tv1"         
android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
android:layout_alignParentBottom="true" 
 android:layout_alignParentStart="true" 
 android:layout_marginBottom="189dp" 
 android:layout_marginStart="103dp" 
 android:text="TextView" />


</RelativeLayout>
------------------------------------------------------------------------------
D:\QUIZAPP\app\src\main\java\com\ldc\quizapp\MainActivity.java
 
package com.ldc.quizapp;
import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    // These are the global variables    RadioGroup radioGroup;
    RadioButton selectedRadioButton;
    Button buttonSubmit;
    TextView t1;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // layout instances        buttonSubmit = (Button) findViewById(R.id.btnSubmit);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        t1=(TextView) findViewById(R.id.tv1);

        /*            Submit Button        */        buttonSubmit.setOnClickListener(new View.OnClickListener() {

            @Override            public void onClick(View v) {
                // get the selected RadioButton of the group                selectedRadioButton  = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
                //get RadioButton text                String choice = selectedRadioButton.getText().toString();
                // display it as Toast to the user                Toast.makeText(MainActivity.this, "Selected Radio Button is:" +choice , Toast.LENGTH_LONG).show();
                if (choice.equalsIgnoreCase("MOON"))
                {
                t1.setText("You have Gracefull Eyes");
                }
                if (choice.equalsIgnoreCase("SUN"))
                {
                    t1.setText("You Love Flowers");
                }
                if (choice.equalsIgnoreCase("CLOUDS"))
                {
                    t1.setText("You Enjoy  Raining ");
                }
                if (choice.equalsIgnoreCase("STARS"))
                {
                    t1.setText("You Look Beatiful ");
                }


            }
        });
    }
}

No comments:

Post a Comment