I have two EditText boxes and a RadioGroup which contains two radio buttons. When I start entering input in any of edittext boxes, All the radiobuttons has to be checked off (if any one of buttons has checked already) automatically and another edittext also should be cleared. And If i check any of two radio buttons then all edittext boxes should be cleared(if they have any inputted data). Here is what i have done so far.
Inside oncreate() :
et1 = (EditText)findViewById(R.id.editText1);
et1.setOnFocusChangeListener(this);
et1.addTextChangedListener(this);
et2 = (EditText)findViewById(R.id.editText2);
et2.setOnFocusChangeListener(this);
et2.addTextChangedListener(this);
radioButton1 = (RadioButton)findViewById(R.id.rb1);
radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {
et1.setText("");
et2.setText("");
}
});
And ...
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.editText1:
whoHasFocus = 1;
break;
case R.id.editText2 :
whoHasFocus = 2;
break;
}
}
And
#Override
public void afterTextChanged(Editable edt) {
if(whoHasFocus == 1){
String enteredName1 = edt.toString().trim();
if(enteredName1.length() == 1){
if(et2.getText().toString().trim().length() >= 1)
et2.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}else if(whoHasFocus == 2){
String enteredName2 = edt.toString().trim();
if(enteredName2.length() == 1){
if(et1.getText().toString().trim().length() >= 1)
et1.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}
}
When i check any radio button and start typing in edittext, radiobutton is getting unchecked but nothing is entering into edittext for the first time. If i enter next letter then it is entering the second letter and working fine then onwards. But First letter is not being entered into endittext when a radiobutton is checked. please help me. Where iam going wrong?
Solved
radioGroup.setOnCheckedChangeListener(null);
radioButton1.setChecked(false);
radioButton2.setChecked(false);
radioGroup.setOnCheckedChangeListener(this);
And
et1.removeTextChangedListener(MyActivity.this);
et2.removeTextChangedListener(MyActivity.this);
et1.setText("");
et2.setText("");
et1.addTextChangedListener(MyActivity.this);
et2.addTextChangedListener(MyActivity.this);
Related
I have set the click event listener on a number of CheckBox and RadioButton views. Both of these view types have an isChecked() method. I can get the state of either one by casting to its type for every single switch case, but can I get the state just in a general sense for either of them before I enter the switch cases?
#Override
public void onClick(View view) {
boolean checkState;
switch (view.getId()) {
case R.id.radio_button_1:
checkState = ((RadioButton) view).isChecked();
// ...
break;
case R.id.check_box_1:
checkState = ((CheckBox) view).isChecked();
// ...
break;
// more check boxes and ratio buttons ...
}
}
This question has a similar title to this one, but there was too much code there for me to easily understand if they were asking the same thing.
While I was in the process of writing this question, I found an answer, which I will post below. However, if there is a better answer, I will accept it.
Consider one RadioButton and one CheckBox
RadioButton button = (RadioButton) findViewById(R.id.radio_button);
button.setOnCheckedChangeListener(listener);
CheckBox box = (CheckBox) findViewById(R.id.checkbox);
box.setOnCheckedChangeListener(listener);
Both have a single listener
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView instanceof CheckBox ){
Toast.makeText(getApplicationContext(), "ChechBox " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "RadionButton " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
}
}
};
If there are many still we can use a single listener as follows
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView instanceof CheckBox ){
final ChechBox b = (ChechBox)buttonView;
switch(b.getId()){
case R.id.first_checkbox:
. . . . . . . .
}
}else {
final RadioButton r = (RadioButton)buttonView;
switch(r.getId()){
case R.id.first_radio
. . . . . . . . . . .
}
}
}
};
You need to attach all of the views to this listener
Direct Answer
As #MikeM answered in the comments, you can use instanceof to check once for CompoundButton or Checkable and cast to that.
#Override
public void onClick(View view) {
boolean checkState = false;
if (view instanceof Checkable) {
checkState = ((Checkable) view).isChecked();
}
switch (view.getId()) {
// ...
}
}
Better Answer
However, as #MikeM also mentioned, you should be using an OnCheckedChangeListener rather than an OnClickListener. There are two similar checked changed listeners:
CompoundButton.OnCheckedChangeListener
RadioGroup.OnCheckedChangeListener
The CompoundButton listener can be used for a CheckBox or a RadioButton. However, if all the radio buttons in a RadioGroup had this set, the listener would get called twice, once for the button getting turned off and once for the button getting turned on. Thus, it is better to use the RadioGroup listener for radio buttons.
So add the RadioGroup.OnCheckedChangeListener to the RadioGroup and the CompoundButton.OnCheckedChangeListener to each CheckBox.
// set RadioGroup.OnCheckedChangeListener
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(radioGroupListener);
// set `CompoundButton.OnCheckedChangeListener`
CheckBox cb1 = (CheckBox) view.findViewById(R.id.check_box_1);
cb1.setOnCheckedChangeListener(checkBoxListener);
CheckBox cb2 = (CheckBox) view.findViewById(R.id.check_box_2);
cb2.setOnCheckedChangeListener(checkBoxListener);
// ...
where the listeners are implemented as follows:
Radio Buttons
The isChecked() value is true for whatever RadioButton id is returned by the RadioGroup listener.
RadioGroup.OnCheckedChangeListener radioGroupListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
// checkedId is the id of the newly checked radio button
switch (checkedId) {
case R.id.radio_button_1:
// ...
break;
case R.id..radio_button_2:
// ...
break;
// ...
}
}
};
Check Boxes
The isChecked value is directly passed in to the CompoundButton listener.
CompoundButton.OnCheckedChangeListener checkBoxListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.checkbox_1:
// ...
break;
case R.id.checkbox_2:
// ...
break;
}
}
};
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
}
});
I have a radio button group in Android.
I receive events when the items are selected. Normal so far.
But I don't get the event if the user clicks on an already selected item.
Is there a way to know (to receive an event) when the users hits a radiobutton either if it is selected or not?
Thanks a lot.
i don't understand why you would get an event when clicked on an already checked radio button,
but if you want to unselect a radio button by a click on it if it is already selected!!
check this code it can help you:
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.rg);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{ //The first codition check if we have clicked on an already selected radioButton
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}
Hope this wil help
By setting an OnClickListener() on your buttons...
I am having a problem with a radio button that is in a radio group.
I have an app that is a quiz application and I ask 5 questions.
When run the application in my Android emulator all the questions have no problem but only on the 3rd question. When I click with my mouse on the radio button it seems toggle to a checked state but then it unchecks right away. Has anyone seen this kind of behavior?!
I setup this Radio group and dynamically add 4 radio buttons in a radio group and then use an OnCheckedChangeListener() event to capture the change.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<=3;i++)
{
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
if (btn.isPressed() && questNo < 6)
{
if (corrAns[questNo-1].equals(btn.getText()) && flag==true)
{
Log.e(LOG_TAG,"onCheckedChanged: correct answer = btn Text");
score++;
flag = false;
checked = true;
}
else if(checked==true)
{
Log.e(LOG_TAG,"onCheckedChanged: correct answer != btn Text");
score--;
flag = true;
checked=false;
}
}
}
Log.e(LOG_TAG, "Score:"+ Integer.toString(score));
}
});
I have noticed that it happens randomly on different questions and only on first radiobutton that is selected but if you select another one after then the functionality returns to normal. Any ideas?
I had the same bug. I'm also using dynamic radioGroups and buttons. This is working for me:
private OnCheckedChangeListener rblLikert_Listener = new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedIndex = group.getCheckedRadioButtonId();
if(selectedIndex != -1)
{
m_likertValue = radioButtonValue;
int buttonId = group.getCheckedRadioButtonId();
Logger.i("button id: " + String.valueOf(buttonId));
RadioButton selectedButton = (RadioButton)findViewById(buttonId);
selectedButton.toggle();
Logger.i(" is checked: " + String.valueOf(selectedButton.isChecked()));
}
}
};
I am trying to use radio buttons but they aren't working. I've got the toast in there at the moment purely for debugging, and it never appears. There seems to be various ways to use them so perhaps I'm just using a poor method. Any advice on what I've done wrong would be amazing.
final RadioButton rbSDR = (RadioButton) findViewById(R.id.rbSDR);
final RadioButton rbMM = (RadioButton) findViewById(R.id.rbMM);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
//int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
switch (id) {
case -1:
rbMM.setChecked(false);
rbSDR.setChecked(false);
break;
case R.id.rbSDR:
rbMM.setChecked(false);
break;
case R.id.rbMM:
rbSDR.setChecked(false);
break;
default:
break;
}
});
EDIT:
Apparently the issue was that I had a linear layout WITHIN the radio button group. Now to figure out how to put the buttons side by side...
the id will always return -1 only. Try the following edited code,
final RadioButton rbSDR = (RadioButton) findViewById(R.id.rbSDR);
rbSDR.setId(R.id.rbSDR);
final RadioButton rbMM = (RadioButton) findViewById(R.id.rbMM);
rbMM.setId(R.id.rbMM);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
//int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
switch (id) {
case -1:
rbMM.setChecked(false);
rbSDR.setChecked(false);
break;
case R.id.rbSDR:
rbMM.setChecked(false);
break;
case R.id.rbMM:
rbSDR.setChecked(false);
break;
default:
break;
}
});
set the listener to radio buttons instead of radio group
Instead of using
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
Use
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
To put the radio buttons side-by-side, add this to the XML properties:
android:orientation="horizontal"