I want to set the visibility of a RadioButton to INVISIBLE or GONE. For some reason this isn't working.
RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setVisibility(View.GONE);
or
myRadioButton.setVisibility(View.INVISIBLE);
No error is returned, it just doesn't do anything.
However I have tried this on the RadioGroup
RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.INVISIBLE);
And it works fine to hide the whole group. Is there a way to just hide one of the RadioButtons? I have a group of 3 which are answers to questions but in some cases I want to hide the last one.
you can hide the particular radio button this way
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.INVISIBLE);
or if you use View Gone menas radio button hide with sapce
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.GONE);
In that case do not hide the radio group
RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.Visible);
Hi use like this.
RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myRadioButton.setVisibility(View.INVISIBLE);
}
});
OR
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:visibility="invisible"/>
This code is working for me.Hope this will help you.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TableLayout bilgiAlani=(TableLayout)findViewById(R.id.bilgiAlani);
final RadioButton secim1 = (RadioButton) findViewById(R.id.butonSecim1);
final TextView bilgiMesaji=(TextView)findViewById(R.id.bilgiMesaji);
secim1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (secim1.isChecked()) {
Toast.makeText(getApplicationContext(), "secildi",
Toast.LENGTH_SHORT).show();
bilgiAlani.setVisibility(View.VISIBLE);
bilgiMesaji.setText("birinci seicmbirinci seicmbirinci seicmbirinci seicmbirinci\n seicmbirinci seicm" +
"birinci seicmbirinci seicmbirinci seicmbirinci seicm" +
"birinci seicmbirinci seicm ");
}
else if(!secim1.isChecked())
{
Toast.makeText(getApplicationContext(), "Secmekten Vazgecildi",
Toast.LENGTH_SHORT).show();
bilgiAlani.setVisibility(View.GONE);
bilgiMesaji.setText("birinci secilmedi");
}
}
});
Related
I have some code that generates in the activity some radiobuttons:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
What I want to do is be able to verify which ones (radiobuttons) are checked when I click a button:
public void onAddAnswer(View v){
position++;
delete();
drawRAnswers(position);
}
For now, what this button does is only load the next set of radiobuttons in the view and delete the current ones, but I don't check which ones are selected. Do you know how I could do this in this onAddAnswer method? I would like to put the text of each selected radiobutton in a list.
Thanks.
I managed to figure this one out myself. Here is what I have done:
public class Activity extends AppCompatActivity {
RadioGroup currentGroup;
...
public void onAddAnswer(View v){
if (currentGroup.getCheckedRadioButtonId()==-1){
Toast.makeText(getApplicationContext(), "Veuillez sélectionner au moins une réponse",
Toast.LENGTH_LONG).show();
} else {
int selectedId = currentGroup.getCheckedRadioButtonId();
RadioButton selectedRadio = (RadioButton)findViewById(selectedId);
Toast.makeText(getApplicationContext(), selectedRadio.getText().toString()+" is selected",
Toast.LENGTH_SHORT).show();
position++;
delete();
updateData(position);
}
}
...
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
currentGroup = l1;
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
}
So basically what I am doing is have a RadioGroup for every time I draw radiobuttons, match it with the currentGroup and check if I have (or not) any radiobuttons selected. If yes, then I find out which one.
There is a question having 4 options(Radio buttons). I want to add the option of selected radio button into an array. ONLY the option of the selected radio button.
The Radio Group is having 4 radio buttons.
You should wrap all the radiobuttons in a RadioGroup and then set its OnCheckedChangeListener:
final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = group.findViewById(checkedId);
// add radioButton to Your array
}
});
Note that this code will add to the array the selected radio button every time it gets selected. If you want to add it on a button click, you need to do it this way:
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int checkedId = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(checkedId);
// add radioButton to Your array
}
})
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.
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...