Android Radio Button - android

I have a radio button group in Android which looks something like:
Choose Color:
Red
Blue
Orange
Green
I need to get selected radio button and also its value.
I have 4 radiobuttons in this manner within radiogroup rg
rb1a=(RadioButton)findViewById(R.id.rb1a);
rb1b=(RadioButton)findViewById(R.id.rb1b);
rb1c=(RadioButton)findViewById(R.id.rb1c);
rb1d=(RadioButton)findViewById(R.id.rb1d);
tv1=(TextView)findViewById(R.id.tv1);
next1=(Button)findViewById(R.id.next1);
rg=(RadioGroup)findViewById(R.id.rg);
// I have error after this line.please help
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
}
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}
});

you can test the radion button with the isChecked() function.
for ex:
if(radio1_red.isChecked())
{
txtView.setText("Red button is checked");
}
Have a look at this Example .
You can also refer this Page - Form Stuff given in android-sdk pages.
Do this for getting selected radio button and also its value:
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};

like you can see on the android documentation
http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
the OnCheckedChangeListener has only the method
onCheckedChanged(RadioGroup group, int checkedId) and doesn't contain the method
public void onCheckedChanged(CompoundButton arg0, boolean arg1) -> remove it and try again.
You find an example here:
http://developer.android.com/guide/tutorials/views/hello-formstuff.html
regards

Related

how to check and uncheck single radio button

I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) radioButton.setChecked(false);
else radioButton.setChecked(true);
}
});
I have made a solution for it using set selected attribute of the radio button.
final RadioButton radioButton = findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?
You are generating a endless loop. That will cause stackoverflow error.
Because you are changing radio button checked status inside onCheckedChanged.
If you need changing check status then do it in click of a button.
Don't change check status inside onCheckedChanged
Perhaps you need to change status of RadioButton for some condition. For that you will do following.
public class MainActivity extends AppCompatActivity {
RadioButton radioButton = null;
CompoundButton.OnCheckedChangeListener listener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO: 8/6/2018 your logics
}
};
radioButton.setOnCheckedChangeListener(listener);
changeStatus(radioButton, true);
}
private void changeStatus(RadioButton radioButton, boolean status){
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(status);
radioButton.setOnCheckedChangeListener(listener);
}
}
When you need to change status then call
changeStatus(radioButton, true);

How to set editText visible/invisible?

In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE); however it just ruines the format of the xml.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
Try this :
rCM.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});
I've made a sample and it works for me just take a look at this code :
I've declared those variables as global
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate() I've got this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton for the use that you want, I recommend you to use CheckBox cause you can only click ONE and this you have to check if the first RadioButton is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
By the way if you want to use this code :
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView when you start the Activity and you'll have to put any RadioButton (if you want) android:checked="true" or false if you want to be showed or not the TextView, but with this code you won't get the click event on the RadioButtons.

Android RadioGroup OnCheckedChangeListener

I have an Activity which implements both Checkbox.OnCheckedChangeListener and RadioGroup.OnCheckedChangeListener The groups are both dynamically generated and passed the same Activity as the listener.
However, when I click on a RadioGroup, the RadioGroup.OnCheckChangeListener is completely ignored but here's where it gets weird. It triggers CheckBox.OnCheckChangeListener! I tried casting the activity to the RadioGroup listener before passing it, but RadioGroup cannot even accept it, only the CheckBox listener gets through.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(subquestion.getQuestionType().equals("MULTIPLE_CHOICE_MULTI_ANSWER")) {
String selected = buttonView.getText().toString();
if (subquestion.getAnswer().getDataList() != null) {
Answer answer = subquestion.getAnswer();
ArrayList<String> checked = answer.getDataList();
if (checked.contains(selected)) {
checked.remove(selected);
Log.d("Checkbox" + buttonView.getId(), "unchecked");
} else {
checked.add(selected);
Log.d("Checkbox" + buttonView.getId(), "checked");
}
} else {
ArrayList<String> checked = new ArrayList<String>();
checked.add(selected);
subquestion.getAnswer().setDataList(checked);
Log.d("Checkbox" + buttonView.getId(), "checked");
}
}
if(subquestion.getQuestionType().equals("MULTIPLE_CHOICE_SINGLE_ANSWER")) {
RadioGroup group = buttonView; //CAN'T BE DONE
String selected = group.getFocusedChild().getId() + "";
subquestion.getAnswer().setData(selected);
Log.d("Radiobutton " + selected + group.getFocusedChild().getId(), "selected");
}
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//This does NOTHING, can't even be passed to the RadioGroup as a listener
}
How do I get the RadioGroup from the buttonView? I generate everything in code and it's different every time so there are no static id's to determine what's clicked. I pass an id to the radiobuttons to let me know which is which but I can't get to it like this.
It works perfectly for the checkboxes, it's just that Android seems to throw the wrong event and uses the wrong event handler for RadioGroup.
Thank you for reading!
Call this method for RadioGroup.OnCheckChangeListener and pass RadioGroup as argument
public void onRadioChangeListener(RadioGroup radioGroup){
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// write your code here
}
});
}

How to give radio button functions in a alert dialog?

So I have made a alert dialog box with 3 options. So, if the first option is selected and something specific is entered into a edit text field it changes a text view into something specific. How do I go about giving the radio button a function?
Check this
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.firstRadioBtn: //id of first radio button
//code goes here if first radio button is clicked
break;
case R.id.secondRadioBtn://id of second radio button
//code goes here if second radio button is clicked
break;
case R.id.thirdRadioBtn://id of third radio button
//code goes here if third radio button is clicked
break;
}
}
});
Hope it helps.
I think this is what you want....
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// your code here
}
});
here inside the code block you will do something specific or whatever.... ;)
you must use
RadioGroup radioGroup = (RadioGroup) dialog.findViewById(R.id.yourRadioGroup);
if this is a part of your dialog....
Hope this helps
you can get it in a much easier way like this
builder.setSingleChoiceItems(items, checkeditem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});

How to disable edittext after click on radio button?

Hi i have Two radio button(subject and chapter) and two edittext(SubjectName,ChapterName).
My question is that when i click on subject radio button then chapter edittext will
disable. I don't understand how to do this.
Pls give me some reference.
try this :
inRadiobutton onCheckedChanged()
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(radio_1.getId()==checkedId)
{
////for Disable :
EditText edt=(EditText)findViewById(R.id.editboxtxt);
edt.setEnabled(false);
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(false);
edt.clearFocus();
////for Enable :
//EditText edt=(EditText)findViewById(R.id.editboxtxt);
//edt.setEnabled(true);
//edt.setInputType(InputType.TYPE_CLASS_NUMBER);
//edt.setFocusableInTouchMode(true);
//edt.requestFocus();
}
subject and chapter
Privet EditText edt,edt1;
edt=(EditText)findViewById(R.id.editbox_subject);
edt1=(EditText)findViewById(R.id.editbox_chapter);
edt.setVisibility(8);
edt1.setVisibility(8);
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(radio_1.getId()==checkedId)
{
edt.setVisibility(1);
}
else{
edt1.setVisibility(1);
}
}
you try this one method

Categories

Resources