Created
April 8, 2021 15:00
-
-
Save codinginflow/13a0eab9e17361e53e899b98a9271c30 to your computer and use it in GitHub Desktop.
Radio Buttons & Radio Group Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="com.codinginflow.radiobuttonexample.MainActivity"> | |
<RadioGroup | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:id="@+id/radioGroup"> | |
<RadioButton | |
android:id="@+id/radio_one" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="One" | |
android:onClick="checkButton" | |
android:checked="true"/> | |
<RadioButton | |
android:id="@+id/radio_two" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Two" | |
android:onClick="checkButton"/> | |
<RadioButton | |
android:id="@+id/radio_three" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Three" | |
android:onClick="checkButton"/> | |
</RadioGroup> | |
<TextView | |
android:id="@+id/text_view_selected" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/radioGroup" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="27dp" | |
android:text="Your selection: " | |
android:textSize="20sp" /> | |
<Button | |
android:id="@+id/button_apply" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/text_view_selected" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="37dp" | |
android:text="apply" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.radiobuttonexample; | |
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; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
RadioGroup radioGroup; | |
RadioButton radioButton; | |
TextView textView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
radioGroup = findViewById(R.id.radioGroup); | |
textView = findViewById(R.id.text_view_selected); | |
Button buttonApply = findViewById(R.id.button_apply); | |
buttonApply.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
int radioId = radioGroup.getCheckedRadioButtonId(); | |
radioButton = findViewById(radioId); | |
textView.setText("Your choice: " + radioButton.getText()); | |
} | |
}); | |
} | |
public void checkButton(View v) { | |
int radioId = radioGroup.getCheckedRadioButtonId(); | |
radioButton = findViewById(radioId); | |
Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(), | |
Toast.LENGTH_SHORT).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man, when i clone from git using https the project wont run. All the class names and imports are red. Is this because the gradle file is not included or am i doing something wrong? I am new to AS. Thanks.