Skip to main content

How to create Radio Buttons in Android studio - Java

MainActivity.java

package com.example.youtuberadio;

import androidx.appcompat.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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
RadioGroup radioGroup;
TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup=findViewById(R.id.rGroup);
        textView=findViewById(R.id.view_selected);

        //variable for the button
       Button buttonOk =findViewById(R.id.btn_ok);
       buttonOk.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               int radioId=radioGroup.getCheckedRadioButtonId();
               radioButton=findViewById(radioId);

               textView.setText("You selected: "+radioButton.getText());
           }
       });
    }

    public void checkOk(View v){
        int radioId=radioGroup.getCheckedRadioButtonId();
        radioButton=findViewById(radioId);

        Toast.makeText(this,"You have clicked on: "+ radioButton.getText(),Toast.LENGTH_SHORT).show();
    }
}

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rGroup"
    android:layout_centerInParent="true">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="Male"
        android:onClick="checkOk"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="Female"
        android:onClick="checkOk"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/other"
        android:text="Other"
        android:onClick="checkOk"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rather"
        android:text="Rather Not Say"
        android:onClick="checkOk"
        android:checked="true"/>
</RadioGroup>

    <TextView
        android:id="@+id/view_selected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rGroup"
        android:layout_marginStart="141dp"
        android:layout_marginLeft="141dp"
        android:layout_marginTop="-4dp"
        android:text="Your choice"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_selected"
        android:layout_marginStart="145dp"
        android:layout_marginLeft="145dp"
        android:layout_marginTop="0dp"
        android:text="OK" />
</RelativeLayout>

Comments

Popular posts from this blog

ACTIVATING MS OFFICE WITHOUT KEY

Step1 Open cmd as admin Step2 copy office directory Step3  Go to office directory, type cd on cmd then paste the office directory you copied e.g., for a 64-bit machine it will look like this. cd C:\Program Files (x86)\Microsoft Office\Office16 Step4 Type in the following commands into cmd then hit enter after every command: cscript ospp.vbs /inpkey:XQNVK-8JYDB-WJ9W3-YJ8YR-WFG99 cscript ospp.vbs /unpkey:BTDRB >nul cscript ospp.vbs /unpkey:KHGM9 >nul cscript ospp.vbs /unpkey:CPQVG >nul cscript ospp.vbs /sethst:kms8.msguides.com cscript ospp.vbs /setprt:1688 cscript ospp.vbs /act METHOD 2 Copy script https://gist.github.com/amitbd1508/fe64e926005c85d424e8e79a943b3b60 @echo off title Activate Microsoft Office 2019 (ALL versions) for FREE - MSGuides.com&cls&echo =====================================================================================&echo #Project: Activating Microsoft software products for FREE without additional software&echo =====================...
 HOW TO USE MESSAGE BOX TO DISPLAY OUTPUT FROM A SELECTION CASE IN VB Public Class Form1     Dim mark As Double     Private Sub txtTest_TextChanged(sender As Object, e As EventArgs) Handles txtTest.TextChanged     End Sub     Private Sub btnok_Click(sender As Object, e As EventArgs) Handles btnok.Click         mark = Double.Parse(txtTest.Text)         Select Case (mark)             Case 0 To 49                 MsgBox("Fail!", MsgBoxStyle.Information, "COMMENT")             Case 50 To 64                 MsgBox("Pass", MsgBoxStyle.Information, "COMMENT")             Case 65 To 74                 MsgBox("Credit", MsgBoxStyle.Information, "COMMENT")             Case 75 To...
The computer scientist responsible for copy,  cut and paste passes away.  The advent of the personal computer wasn’t just about making these powerful machines available to everyone, it was also about making them accessible and usable, even for those lacking a computer science degree. Larry Tesler, who passed away on Monday, might not be a household name like Steve Jobs or Bill Gates, but his contributions to making computers and mobile devices easier to use are the highlight of a long career influencing modern computing. Born in 1945 in New York, Tesler went on to study computer science at Stanford University, and after graduation he dabbled in artificial intelligence research (long before it became a deeply concerning tool) and became involved in the anti-war and anti-corporate monopoly movements, with companies like IBM as one of his deserving targets. In 1973 Tesler took a job at the Xerox Palo Alto Research Center (PARC) where he worked until 1980. Xerox PARC is famou...