how to show that the radio button is selected permanently - android

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.

Related

Android Auto-select check box

Can some one tell me how can i auto select a check box when a function completes. I check a condition, After the function has completed i need to automatically view that check box is selected.
Thanks in Advance.
You can check the CheckBox automatically from xml
<CheckBox
android:id="#+id/checkbox1"
.
.
.
android:checked="true"
/>
or set java code like
CheckBox cbCheckbox = (CheckBox)findViewById(R.id.checkbox1);
cbCheckbox .setChecked(true);

Is it possible to make a checkbox/radio button uncheckable?

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!

Keep the text inside a button for the next run of the application

I have a button that changes the text inside in it when clicked.
<Button android:id="#+id/timelinebtn"
android:text="Button1"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="5dp" />
I have some other user clickable buttons in my app, and depending on what they click, the text inside this button (at first "Button1") changes to some other text with the code:
button1.setText("Changed the text to something other.");
My question is how can I keep the last text that the button was showing for the next run of the program.
Once again to make it a bit more clear - when I close the program, how the button can save the text inside it until the next run of the application.
You will need to persist the state somewhere, perhaps the fastest way would be using the shared preferences.
You can read this, it describes all ways of storing data in android.

How can we custmize Check Box in Android?

I want to display Check box like Radio button though is it possible to give our own shape to the check box
if possible than help me please thanks
Yes. It is possible. Use this code
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#android:drawable/btn_radio"/>
you need to set your own shape or image for both of it's state and have to handle it in the code....

Radio Button does not toggle its state

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);

Categories

Resources