Android Getting Value from Selected Radiobutton

Android getting value from selected radiobutton

Tested and working. Check this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();

}

public void addListenerOnButton() {

radioGroup = (RadioGroup) findViewById(R.id.radio);
btnDisplay = (Button) findViewById(R.id.btnDisplay);

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();

}

});

}
}

xml

<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />

</RadioGroup>

Android getting value from selected radiobutton

Tested and working. Check this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();

}

public void addListenerOnButton() {

radioGroup = (RadioGroup) findViewById(R.id.radio);
btnDisplay = (Button) findViewById(R.id.btnDisplay);

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();

}

});

}
}

xml

<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />

</RadioGroup>

Getting the value from selected RadioButton within a fragment

the app crashes because you tring to get radioButtion from the view that handling the click and not your layout view

radioButton = v.findViewById(radioID);

you have to use another view name to set a difference between them

buttonConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Records the gender from the selected radio button.
int radioID = radioGroup.getCheckedRadioButtonId();
radioButton = v.findViewById(radioID);
if (radioButton.getText().equals("Male")) {
gender = (float) .58;
} else {
gender = (float) .49;
}
......

Getting selected radio button value from radiogroup in fragment

Make rootView as global variable because your method 'addListenerOnButton()' getting null view.

as below:

public class Booking extends Fragment {

public Context _context = getActivity();
private String JSON_URL;
private SharedPrefManager sharedPrefManager;

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button getQuotes;
private View rootView;

public Booking() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_booking, container, false);

sharedPrefManager = new SharedPrefManager(getActivity());

return rootView;
}


public void onViewCreated(View rootView, Bundle savedInstanceState){
super.onViewCreated(rootView, savedInstanceState);
final View v = rootView;

// Spinner element
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);

radioGroup = (RadioGroup) rootView.findViewById(R.id.radio);
getQuotes = (Button) rootView.findViewById(R.id.getQuotes);

// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("4 Night / 5 Days");
categories.add("5 Night / 6 Days");
categories.add("6 Night / 7 Days");
categories.add("7 Night / 8 Days");

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, categories);

// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);


// Spinner click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {

Log.d("tag", "String item="+position);

if(position == 0) {
JSON_URL = "http://kaushika.tigrimigri.com/gcmMulticast2/getPackages.php";
}

getJSON(JSON_URL, v);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


String text = spinner.getSelectedItem().toString();
sharedPrefManager.addSpinner(text);

addListenerOnButton();

}

public void addListenerOnButton(){

getQuotes.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
Log.d("tag","selectId" + selectedId);

// find the radiobutton by returned id
radioButton = (RadioButton) rootView.findViewById(selectedId);
Log.d("tag","radioButton" + radioButton);
Log.d("tag","radioButton.getText()" + radioButton.getText());

//String radiovalue = ((RadioButton) rootView.findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString();

//sharedPrefManager.addRadio(radioButton.getText().toString());

Toast.makeText(getActivity().getApplication(), radioButton.getText(), Toast.LENGTH_LONG).show();

}

});
}

I did not try this but I hope this works.

How to get text of the checked id from radio group using view binding?

You could do this:

EDIT:

int id = binding.radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = binding.getRoot().findViewById(id);
//Then get the radio button's text
Toast.makeText(MyAndroidAppActivity.this,
radioButton.getText(), Toast.LENGTH_SHORT).show();

Android get value of the selected radio button

You need to get the radio button at that index, then get the value of the text of that button. Try this code below.

if(rg1.getCheckedRadioButtonId()!=-1){
int id= rg1.getCheckedRadioButtonId();
View radioButton = rg1.findViewById(id);
int radioId = radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
String selection = (String) btn.getText();
}

How to get default selected radio button from radio group?


if(gender.getCheckedRadioButtonId()==-1)
{
Toast.makeText(mContext, "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
Toast.makeText(mContext, selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}

How to get the selected index of a RadioGroup in Android

You should be able to do something like this:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

If the RadioGroup contains other Views (like a TextView) then the indexOfChild() method will return wrong index.

To get the selected RadioButton text on the RadioGroup:

 RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
String selectedtext = r.getText().toString();

Set RadioGroup selected RadioButton by value of String

rg - your radioGroup

for (rbPosition in 0 until rg.childCount) {
val rb = rg.getChildAt(rbPosition) as RadioButton
if (rb.text == yourText) {
//do stuff for example rb.isChecked = true
}
}

How to check which radio button of a radio group is selected? [ANDROID]

This is working perfectly:

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

int radioButtonID = radioGroup.getCheckedRadioButtonId();

RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);

String selectedText = (String) radioButton.getText();


Related Topics



Leave a reply



Submit