Don't Miss

Radio Button in android

By on January 10, 2018

Activity.java

 

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import static android.view.View.OnClickListener;

public class ExampleActivity extends AppCompatActivity {

private RadioGroup radiogroupCountry;
private RadioButton radcountrybtn;
private Button btnview;

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

radiogroupCountry=(RadioGroup)findViewById(R.id.radioGroupCountry);

btnview=(Button)findViewById(R.id.button3);

btnview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=radiogroupCountry.getCheckedRadioButtonId();
radcountrybtn=(RadioButton)findViewById(selectedId);
Toast.makeText(ExampleActivity.this,radcountrybtn.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”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>

<RadioGroup

android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/radioGroupCountry”>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”England” />

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”America”/>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Germany”/>
<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Farnace”/>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Itlay”/>

</RadioGroup>

<Button
android:id=”@+id/button3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/radioGroupCountry”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”21dp”
android:text=”View Checked” />

</RelativeLayout>

 

About CodingBazz

Leave a Reply

Your email address will not be published. Required fields are marked *