info@thistimebd.com

Thursday 21st of November 07:29:03pm

Radio Button Android app Source Code

MainActivity.java

----------------


package com.tis.web.radiobutton;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    private RadioGroup radioGroup;

    private Button showButton;

    private RadioButton genderButton;

    private TextView resultTextView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        radioGroup = (RadioGroup) findViewById(R.id.radioGroupId);

        showButton=(Button) findViewById(R.id.showButtonId);

        resultTextView=(TextView) findViewById(R.id.resultTextViewId);


        showButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

              int SelectedId=  radioGroup.getCheckedRadioButtonId();

              genderButton=(RadioButton)findViewById(SelectedId);


              String value = genderButton.getText().toString();


              resultTextView.setText("You have selected : " +value);

            }

        });

    }

}

=====================================================================


string.xml

-----------

<resources>

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

    <string name="male_text">Male</string>

    <string name="female_text">Female</string>

    <string name="button_text">Show</string>

</resources>

===================================================================

------------------------------------------------------------------

activity_main.xml

----------------

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

<LinearLayout 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:background="#5c9c182e"

    android:padding="20dp"

    android:orientation="vertical"

    tools:context="com.tis.web.radiobutton.MainActivity">


    <RadioGroup

        android:id="@+id/radioGroupId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">


        <RadioButton

            android:id="@+id/maleTextId"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/male_text"

            android:textSize="20sp"

            android:layout_margin="10dp"

            />


        <RadioButton

            android:id="@+id/femaleTextId"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/female_text"

            android:textSize="20sp"

            />



    </RadioGroup>

    <Button

        android:layout_margin="20dp"

        android:id="@+id/showButtonId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/button_text"

        android:textSize="20sp"

        android:textStyle="bold"


        />


    <TextView

        android:id="@+id/resultTextViewId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="20sp"

        android:textStyle="bold"


        />




</LinearLayout>