I have these RadioButtons set up within a RadioGroup:
<RadioGroup
android:id="#+id/radioGroupLeakTight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:text="#string/action_yes"
android:id="#+id/radioLeakTightYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:text="#string/action_no"
android:id="#+id/radioLeakTightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
With the code below I try to toggle one of the RadioButtons through code:
radioGroupLeakTight.check((currentTask.isLeakTight() ? R.id.radioLeakTightYes : R.id.radioLeakTightNo));
If I don't use the above code the RadioButtons work as intended: whenever one gets checked, the other one gets unchecked. If I use the code to pre-check one of the RadioButtons this functionality gets lost. Whenever I try to check a RadioButton the other one remains checked.
I've read solutions where I should implement the 'OnCheckedChangeListener' and toggle them myself. But I guess that's just covering up something else that doesn't work, no? I've also read people that had these issues, but they had the RadioGroup missing, which I clearly have.
What am I doing wrong? Is the only solution really implementing 'OnCheckedChangeListener' and toggling the other RadioButtons myself?
UPDATE 08/12/2016:
So I've continued my search and implemented 'OnCheckedChanged'. What I see now is that whenever I check one of the RadioButtons this event is never fired. The listener is set on the 2 RadioGroups.
I'm starting to suspect these RadioButtons lose their membership to the RadioGroup? Whenever I check them through code (after I set my listeners) the event is fired successfully.
Use clearCheck() for RadioGroups. Learn more.
If you need to check another RadioButton, you can do so after this.
Whenever I try to check a RadioButton, the other one remains checked.
RadioButton radioLeakTightYes = (RadioButton) findViewById(...);
RadioButton radioLeakTightNo = (RadioButton) findViewById(...);
radioGroupLeakTight.clearCheck();
if(currentTask.isLeakTight()){
radioGroupLeakTight.check(radioLeakTightYes.getId());
} else {
radioGroupLeakTight.check(radioLeakTightNo.getId());
}
Related
I have created a checkBox within my xml file. I am attempting to set an onClickListener on the text of the checkBox however I'm finding no real solution. In other words I want to be able to click the checkbox(make the check box selectable) as well as click the text to open a new activity with the Term of Agreements. Thank you.
Main.xml
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:checked="true"/>
This is the following code i attempted, however it will just cause it to crash.
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Button termsOfAgreement = (Button) checkBox2.getText();
....
termsOfAgreement.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Launch activity
}
});
getText() is not a button it is a string so you cannot cast it to a button.
you are better off not setting text for the checkbox and just using a regular textview next the to check box and putting a listener on the text.
then you have to manage the check box when the text is clicked. so you would have 2 click listeners, one for the checkbox and one for the textview
Seeing as none of the previous answers really gave you what you wanted, potentially you could just wrap both your CheckBox and your TextView within a larger view group (like LinearLayout).
You're implementation in xml would look as follows:
<LinearLayout
android:id="#+id/check_box_and_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#CC000000"
android:checked="true"/>
<TextView
android:id="#+id/termsOfAgreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:textSize="16sp"/>
</LinearLayout>
With this, you can now just set the OnClickListener to the LinearLayout instead of having one for both your CheckBox and your TextView. Notice that I set clickable to true under LinearLayout. This is an important thing to note because by default, clickable is false on a LinearLayout.
Implementation in your java code would be as follows:
LinearLayout myCheckbox = (LinearLayout)findViewById(R.id.check_box_and_text);
myCheckbox.setOnClickListener(yourListener);
As a quick final note, if you find that the alignment is wonky with your CheckBox and TextView, look into using the xml attributes layout_gravity and gravity to get them the way you want them.
Hope this helps! Good luck
Might be a bit of a cheat but how about putting a textview (with your text) next to a checkbox (without text) that way you can use the ontouch/onclick event of the textview to set the checkbox and open the activity, and pressing the checkbox will only check/uncheck it.
It appears there is no real solution to that. Only possibility is to set it aside to the checkBox. The purpose of this was to avoid any miss alignments between the textView and the checkBox however this is the best possible solution offered. Here is the code.
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox"
android:layout_margin="5dp"
android:textColor="#CC000000"
android:checked="true"/>
<TextView
android:id="#+id/termsOfAgreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:layout_toRightOf="#id/checkBox"
android:layout_alignTop="#id/checkBox"
android:textSize="16sp"/>
Then in the java code set an onClickListener for the TextView and a separate listener for the CheckBox itself.
onclick is just not the right listener, its:
onCheckedChanged
EDIT:
and to set the listener its:
setOnCheckedChangeListener
The program I am making has several views in which the user is trying to basically figure out a lot of boolean values. Sounds strange, but what I mean is that my device will connect to another device via bluetooth, and then read the status of that device. I basically display a checklist of the current status of that device. So depending on what that device is doing, different checkboxes are checked. Is there a way to make all of the checkboxes unselectable?
Even better, is there a way to hardcode this into the XML file?
Have you considered using android:clickable for an xml attribute for your checkboxes?
From the documentation:
Defines whether this view reacts to click events.
If it cannot react to click events, then it effectively becomes uncheckable.
<CheckBox
android:id="#+id/blahblah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false" />
and in your code you can call cbox.setChecked(boolean)
to expand on what crocboy and nicholas.hauschild said.
Setting this in you XML
android:enabled="false"
will make the button/checkbox be grayed-out, to show it is disabled
Another way to do this is to set it to:
android:clickable="false"
Here is the code to make a RadioButton that is red, that turns green when checked (in your code), but can not be checked by the user.
android:checked="false"
android:textColor="#d1d2d4"
android:buttonTint="#d1202d"
android:textColorHighlight="#248d51"
android:clickable="false"
AND here is the code to check it (this is using JSON: so it is checking if english=="yes")
String yes = "yes";
if(yourObject.english.equals(yes)) {
if (m_english.isChecked()) {/*do nothing */}
else m_english.toggle();
}
Check this out: http://developer.android.com/reference/android/widget/CheckBox.html
You can use checkbox.setEnabled(false) to make it unselectable.
Try this in your XML: android:enabled="false"
It's supposedly deprecated, but may work depending on your traget SDK.
Hope that helps!
i have the same problem as
how to show the radio button is selected is still ticked? but in android platform... can anybody help me please..
my codes are
<RadioButton
android:checked="false"
android:id="#+id/BBC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff6600"
android:text="BBC NEWS"
android:typeface="serif"
android:textStyle="bold"
android:layout_x="10px"
android:layout_y="100px">
</RadioButton>
Thanks in advance...
You could save the value (when changed) to SharedPreferences, read it each time the page is loaded, and set the checked state based on that.
You can use a static variable like is_ticked & set its value true/false when user tick it. and check value of this variable in onCreate() if(is_ticked) set radio button to checked else unchecked .
A question with similar content was asked yesterday:
I have radio button for the user to choose if he/she wants to keep his/her account logged in.
you can't solve this problem just with the layout.xml - you need to save the status of the radio button and load it again once the appropriate activity is started
You could use this:
mListView.setItemChecked(selectedPosition, true);//this will check(enable) the desired radio button.
selectedPosition, you can save using SharedPreferences.
I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="#color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="#drawable/male_icon_settings" android:id="#+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="#+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="#drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points
Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView is actually unnecessary. ListViews and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed Views for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the getView() method to make sure each button has the correct state each time the method is called.
You could also simply use a ScrollView populated with a few of your rows manually if you don't have enough data to warrant using a ListView. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences. SharedPreferences requires no setup, but I feel like an SQLite database is more suited to your needs.
I add a RadioButton in my layout.
It is unchecked to begin with. And when I click on it , it becomes checked (as shown in emulator). But when when i click on it again, it does not become unchecked again?
<RadioButton android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"/>
If you're looking for checkbox behavior with radio button appearance, you could pass in the xml style to a checkbox.
<CheckBox android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option1"
style="#android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton/>
This can be useful in some cases (ex. using radio buttons in a RecyclerView) but you should be careful because the user expects radio buttons to behave a certain way. If you're allowing the user to make multiple selections you should probably use a normal checkbox, as mentioned in the comments above.
Hope this helps!
If you are only using one radio box for checking on and off, maybe you should use checkbox or toggle button instead.
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Scroll down and see checkbox and toggle button.
When using radios you usually have more than one and choose between them. Like easy, medium, hard.
If you are having more than 1 radio buttons to work with then add "RadioGroups" as follows:
<RadioGroup android:id="#+id/group1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="#+id/radio1" android:text="madras"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:id="#+id/radio2" android:text="bombay"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
Have a look at this example , i am sure this will make your idea clear regarding Radio Buttons.
Also refer this Android Developer - Form Stuff page .
After searching a lot on SO, I came up with a not-so-good but decent workaround.
Declare a boolean variable for each RadioButton you have, initialize it with false, and change the state of the variable and the RadioButton at every click.
boolean isToggledRadio1 = false;
RadioButton radio1 = (RadioButton) findViewById(R.id.radiobutton1);
radio1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isToggledRadio1 = !isToggledRadio1; //Switch boolean value
RadioButton rb = (RadioButton)v;
rb.setChecked( isToggledRadio1 );
}
});
I know it's ideal to use a Checkbox, but if someone needs the radiobutton, then they need the radiobutton.
This is not an optimal solution, as it will basically toggle the button twice every time the user clicks the button (one is the default behaviour, the second time is inside your onclick function), so if you're using an OnCheckedChangeListener you will probably get two calls for the same click.
There's another workaround, which is to change the android:button in a checkbox to another drawable with an xml template, but it's a bit more complex, requires at least 2 more files for the states.
That is a conceptual question: Radio buttons allow you to choose between several options (represented by the other radio buttons). Typically, one radio button out of a group is always checked, even if in an initial state no buttons may be checked if you do not declare one as a default value. That means also that a single button does not allow to toggle its state unless other radio buttons are present in the same group - if there is only one option, you will have to choose it.
If you want a binary toggle, you will want to use a checkbox instead.
Solution is set checked[true/false] intermediate in Java code
ToggleButton t = (ToggleButton) findViewById(R.id.toggle_button);
t.setChecked(true);
// t.setChecked(false);