Android: Save State of Radio Buttons - android

Hi I'm trying to create an app for Android and in order to develop it i need to navigate through different pages and questions. For this task I have defined a radiogroup with some radiobuttons. What I want to obtain is each question answered radiobutton and when the user goes thorugh differentes pages the value can be retrieved. I have tried this code that consists of that if there is one selected radiobutton, there arent created new radiobuttons (radiobuttons checked false). However with this code, there is always a selected answer so there is always the same radiobutton selected. I will appreciate some help.
radBotA.setOnCheckedChangeListener(radioCheckChangeListener);
radBotB.setOnCheckedChangeListener(radioCheckChangeListener);
radBotC.setOnCheckedChangeListener(radioCheckChangeListener);
radBotD.setOnCheckedChangeListener(radioCheckChangeListener);
radBotA.setOnClickListener(radioClickListener);
radBotB.setOnClickListener(radioClickListener);
radBotC.setOnClickListener(radioClickListener);
radBotD.setOnClickListener(radioClickListener);
if (radBotA.isChecked()){
Answers[position]="A";
}
else if(radBotB.isChecked()){
Answers[position]="B"; }
else if(radBotB.isChecked()){
Answers[position]="C"; }
else if(radBotC.isChecked()){
Answers[position]="D"; }
else if(radBotD.isChecked()){
Answers[position]="D"; }
else {
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
}
bPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = position -1;
questions.Previous();
currentQuestion();
}
});
bNext.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
position = position +1;
questions.Next();
currentQuestion();
}
});
private void currentQuestion() {
if (position==0){
bPrevious.setVisibility(View.GONE);
}else{
bPrevious.setVisibility(View.VISIBLE);
}
if (position==nPreguntas-1){
bNext.setVisibility(View.GONE);
}else{
bNext.setVisibility(View.VISIBLE);
}
questions.currentQuestion(this, category);
enunciado.setImageResource(Enunciado[position]);
pregunta.setText(questions.getPregunta());
final RadioButton radBotA = new RadioButton(this);
final RadioButton radBotB = new RadioButton(this);
final RadioButton radBotC = new RadioButton(this);
final RadioButton radBotD = new RadioButton(this);
radBotA.setText("A. " + questions.getRespuestaA());
radBotB.setText("B. " + questions.getRespuestaB());
radBotC.setText("C. " + questions.getRespuestaC());
radBotD.setText("D. " + questions.getRespuestaD());
String nprueba = "Item " + questions.getId() + " de "+ nPreguntas;
NombrePrueba.setText(nprueba);
if (radBotA.isChecked()){
Answers[position]="A";
}
else if(radBotB.isChecked()){
Answers[position]="B"; }
else if(radBotB.isChecked()){
Answers[position]="C"; }
else if(radBotC.isChecked()){
Answers[position]="D"; }
else if(radBotD.isChecked()){
Answers[position]="D"; }
else {
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
radBotA.setChecked(false);
}
}
thank you all for your time
Edit:
public void save(){
SharedPreferences settings = getSharedPreferences("Answers", 0);
SharedPreferences.Editor e = settings.edit();
e.putBoolean("A0",radBotA.isChecked());
e.putBoolean("B0",radBotB.isChecked());
e.putBoolean("C0",radBotC.isChecked());
e.putBoolean("D0",radBotD.isChecked());
e.putBoolean("A1",radBotA.isChecked());
e.putBoolean("B1",radBotB.isChecked());
e.putBoolean("C1",radBotC.isChecked());
e.putBoolean("D1",radBotD.isChecked());
e.putBoolean("A2",radBotA.isChecked());
e.putBoolean("B2",radBotB.isChecked());
e.putBoolean("C2",radBotC.isChecked());
e.putBoolean("D2",radBotD.isChecked());
e.putBoolean("A3",radBotA.isChecked());
e.putBoolean("B3",radBotB.isChecked());
e.putBoolean("C3",radBotC.isChecked());
e.putBoolean("D3",radBotD.isChecked());
public void load(){
SharedPreferences settings = getSharedPreferences("Answers", 0);
boolean answerA0 = settings.getBoolean("A0", false);
boolean answerB0 = settings.getBoolean("B0", false);
boolean answerC0 = settings.getBoolean("C0", false);
boolean answerD0 = settings.getBoolean("D0", false);
boolean answerA1 = settings.getBoolean("A1", false);
boolean answerB1 = settings.getBoolean("B1", false);
boolean answerC1 = settings.getBoolean("C1", false);
boolean answerD1 = settings.getBoolean("D1", false);
boolean answerA2 = settings.getBoolean("A2", false);
boolean answerB2 = settings.getBoolean("B2", false);
boolean answerC2 = settings.getBoolean("C2", false);
boolean answerD2 = settings.getBoolean("D2", false);
boolean answerA3 = settings.getBoolean("A3", false);
boolean answerB3 = settings.getBoolean("B3", false);
boolean answerC3 = settings.getBoolean("C3", false);
boolean answerD3 = settings.getBoolean("D3", false);
However I don't know how to continue. I v' thinking about the following code but it gives me error and where posicion is the "Page Number":
public void Test(){
switch(posicion){
case(0):
if(answerA0==true){
e.putBoolean("A0",radBotA.isChecked());
}
}
}
}

If I understand you correctly, you want to retrieve some data in other activities. In that case the easiest way would be to use SharedPreferences.
After user answers the question (CheckBox's check state is being changed) you should store your information in SharedPreferences like this:
SharedPreferences settings = getSharedPreferences("Answers", 0); // first argument is just a name of your SharedPreferences which you want to use. It's up to you how you will name it, but you have to use the same name later when you want to retrieve data.
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("questionA", radBotA.isChecked()); // first argument is a name of a data that you will later use to retrieve it and the second argument is a value that will be stored
editor.putBoolean("questionB", radBotB.isChecked());
editor.putBoolean("questionC", radBotC.isChecked());
editor.putBoolean("questionD", radBotD.isChecked());
editor.commit(); // Commit the changes
So now you have those information stored in your internal storage. In other activity, you can retrieve this information:
SharedPreferences settings = getSharedPreferences("Answers", 0);
boolean answerA = settings.getBoolean("questionA", false); // The second argument is a default value, if value with name "questionA" will not be found
boolean answerB = settings.getBoolean("questionB", false);
boolean answerC = settings.getBoolean("questionC", false);
boolean answerD = settings.getBoolean("questionD", false);

I'm working on same application and the solution is that you have to store state of Radio button according to your question Number and for each question there is different key, like this:
final RadioButton rdSelection=(RadioButton)findViewById(mradioOptGroup.getCheckedRadioButtonId());
int child_index=mradioOptGroup.indexOfChild(rdSelection);
switch(mid)
{
case 1:
sharedpreferences.putint("",child_index);
break;
case 2:
sharedpreferences.putint("",child_index);
break;
}
and do like this for all your questions on every next question
and on every previous question you have to store state of radio button of mid+1
where mid is your question number

i just solved this problem , now i am able to save the current state of radio button on every next click and on every previous click i get back the radio state,even at the if the user changed the state by going to previous question or any of the question.

Related

Kotlin how to save Radio Button and display values?

So I'm trying to learn Kotlin and have been using Android Studios to practice and learn. Currently I'm trying to make a simple activity with RadioGroup (with Radio Buttons), save the selected value, and then display how much of each value (radiobutton) was selected.
My question is, how do I print which button was selected, and how many of this type of button was selected?
I tried the following:
//in MainActivity.kt in my MainActivity class
s1RadioGroup.setOnCheckedChangeListener { _, checkedId ->
//if catButton was selected add 1 to variable cat
if(checkedId == R.id.catRadio) {
catSum += 1
print(catSum)
}
//if dogButton was selected add 1 to variable dog
if(checkedID == R.id.dogRadio) {
dogSum += 1
print(dogSum)
}
Not sure if I'm going about it the right way, but the desired output is:
I have layout, ID's, clear button, and everything else working. But I'm not sure how to use onClickListener event on 'SaveButton' to save selected radio button and then displaying results (Ex: Cat = 1, Dog =2). I would appreciate any suggestions, or if you can point me in the right direction.
You can maybe try something like this:
RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
// restore previous state
rb.setChecked(lastButtonState);
// set a listener
rb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call this to enable editing of the shared preferences file
// in the event of a change
SharedPreferences.Editor editor = sharedpreferences.edit();
Boolean isChecked = rb.isChecked();
// use this to add the new state
editor.putBoolean(BUTTON_STATE, isChecked);
// save
editor.apply();
}
});
I realize that this is in Java, and you're asking for kotlin, but a SharedPreference, is what you would need to save the radio button's state.
if you want to save all datam you can use database or sharedprefrence.
and if you only want just display value is clicked, you can make like this in button save.
String result1 = ""
String result2 = ""
String result3 = ""
RadioGroup radioGroup = findViewById('yourRGidFromXml')
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton rb = findViewById(selecetedId)
result1= rb.getText.toString()
Log.i("ID", String.valueOf(selectedId));
}
});
//this just for see result
btnSave.OnclikListener(view -> {
Log.i("Result1",result1)
})
you can copy code and android will convert that code to kotlin.

Shared preferences doesn't store int data

I have an activity with two TextViews which show int values. These values change incrementally (1, 2, 3, and so...) when the user clicks a button. I use SharedPreferences to store that values via button click. When I close the app and open it again, the values are correctly displayed in the TextViews, but if they change, they should be added from the previous stored value. Problem is that they start to count from zero.
Here is my code:
public class Roulette1 extends ActionBarActivity {
Button button0, button1;
int click_button0, click_button1;
public static final String button0Str = "button0Key";
public static final String button1Str = "button1Key";
public static final String MyPREFERENCES = "MyPrefsRoulette1";
SharedPreferences sharedpreferences;
TextView times_0_tv;
TextView times_1_tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
times_0_tv = (TextView) findViewById(R.id.times_0);
times_1_tv = (TextView) findViewById(R.id.times_1);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
final TextView total_clicks_tv = (TextView) findViewById(R.id.total_clicks);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click_button1 = click_button1 + 1;
int total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
times_0_tv.setText(click_button0);
times_1_tv.setText(click_button1);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click_button0 = click_button0 + 1;
int total_clicks = click_button0 + click_button1;
total_clicks_tv.setText(String.valueOf(total_clicks));
times_0_tv.setText(click_button0);
times_1_tv.setText(click_button1);
}
});
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(button0Str))
{
times_0_tv.setText(sharedpreferences.getString(button0Str, ""));
}
if (sharedpreferences.contains(button1Str))
{
times_1_tv.setText(sharedpreferences.getString(button1Str, ""));
}
}
public void run1(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
String times0string = times_0_tv.getText().toString();
String times1string = times_1_tv.getText().toString();
editor.putString(button0Str, times0string);
editor.putString(button1Str, times1string);
editor.commit();
}
Hope you have any idea. Thanks!
When you read from sharedPrefs, remember to update the field counters and not just the value of the textViews.
As suggested in the comments, a possible solution would be to use the textView value as the state, updating that directly. Otherwise you have to keep the counters updated manually, for example by updating the fields value at the same time you update the textView value. Personally, I prefer to keep the state separated from the presentation, so that it is easier to compute something with that value later (the downside is that you have to keep the view synchronized. This might change with the new data binding library).
PS I purposely did not put any code, because the solution is trivial and there are other answers with code, but more importantly because I think that the data binding library is a much cleaner way to deal with this kind of problems, even though it's still in beta stage.
Sharedpreferences stored int data also. Check this link:
http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126&aaid=146
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
/**************** Storing data as KEY/VALUE pair *******************/
editor.putBoolean("key_name1", true); // Saving boolean - true/false
editor.putInt("key_name2", "int value"); // Saving integer
editor.putFloat("key_name3", "float value"); // Saving float
editor.putLong("key_name4", "long value"); // Saving long
editor.putString("key_name5", "string value"); // Saving string
// Save the changes in SharedPreferences
editor.commit(); // commit changes
I think there is some logical mistake in your code. Please check.
Add this code to the very beginning of your oncreate:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
times0string = String.valueOf(sharedPreferences.getString(button0Str, 0));
times1string = String.valueOf(sharedPreferences.getString(button1Str, 0));
To solve your problem, Try below code:
Replace first two line inside button1 onClick()
click_button1 = Integer.parseInt(sharedpreferences.getString(button1Str, "")) + 1;
int total_clicks = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + click_button1;
Replace first two line inside button0 onClick()
click_button0 = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + 1;
int total_clicks = click_button0 + Integer.parseInt(sharedpreferences.getString(button1Str, ""));

How to save RadioGroup State using SharedPreferences

i have created a radio group which represents a question and has 4 answer choices (a , b , c , and d ) , hence i created 4 radio buttons for the radiogroup , and it worked fine , but what i want is to keep the answer checked when i leave the activity and return back to it , here is the code :
public class P2 extends Page1 {
RadioGroup rg2 ;
RadioButton r2a , r2b , r2c , r2d ;
Button b2n , b2b ;
TextView tv ;
int count ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_p2);
rg2 = (RadioGroup) findViewById(R.id.RG2);
r2a = (RadioButton) rg2.getChildAt(0);
r2b = (RadioButton) rg2.getChildAt(1);
r2c = (RadioButton) rg2.getChildAt(2);
r2d = (RadioButton) rg2.getChildAt(3);
b2n = (Button) findViewById(R.id.B2n); // next button
b2b = (Button) findViewById(R.id.B2b); // back button to page1
tv = (TextView) findViewById(R.id.res);
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if (r2c.isChecked()){
count ++ ; // since c is the correct answer add 1 to count (mark)
}
}
});
}
public void move21(View view) { // called when b2b is clicked to go back to page1
Intent intent = new Intent(this , Page1.class) ;
startActivity (intent) ;
} }
BTW, i did not define id's for the radiobuttons , just the id (rg2) for the radiogroup .
how can i save the state of the radiogroup using shared preferences ?
thanks
What you can do before you leave the current activity is save the state with something like:
SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("r2a",r2a.isChecked());
editor.putBoolean("r2b",r2b.isChecked());
editor.putBoolean("r2c",r2c.isChecked());
editor.putBoolean("r2d",r2d.isChecked());
editor.apply();
Then, when you come back to the activity, you can do:
SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
boolean r2achecked = settings.getBoolean("r2a",false);
boolean r2bchecked = settings.getBoolean("r2b",false);
boolean r2cchecked = settings.getBoolean("r2c",false);
boolean r2dchecked = settings.getBoolean("r2d",false);
Then check each of those newly created bools and if one of them is true, set that button to checked.

Multiple saved preferences in android

I am very new to java and android but doing my best to make an app, basicaly I want a page with 6 text boxes on it, and each allows the user to type a 3 digit unique value into each, check a confirm box and then a button to save, then when the user revisits this part of the app the data will still be there, I managed to get it working for 1 box but if i add another it just duplicated box 1s value, here is my code for the class
public class Settings extends Activity implements OnClickListener {
CheckBox cb;
EditText et, et1;
Button b;
String test;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
cb = (CheckBox) findViewById(R.id.checkBox1);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
loadPrefs();
cb.setChecked(false);
}
private void loadPrefs() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
String name = sp.getString("NAME", "Kg");
if(cbValue){
cb.setChecked(true);
}else{
cb.setChecked(false);
}
et.setText(name + (" kg"));
}
private void savePrefs(String key, boolean value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePrefs(String key, String value) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked())
savePrefs("NAME", et.getText().toString());
finish();
}
}
any help would be greatly appreciated as time is short :(
Read this.
What you're not coding is saving the data.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbValue = sp.getBoolean("CHECKBOX", false);
What the second line does is says, is "CHECKBOX" a saved sharedpreference? No, it isn't. Okay let's get the default value of false then.
What you need to do is save it using this:
SharedPreferences.editor Editor = sp.edit();
Editor.putBoolean("CHECKBOX",true);
Editor.commit();
The first line defines the sharedpreference editor. The next line saves the boolean value true under the in effect filename (key) of CHECKBOX and then the commit line says, okay do the above and finalise it so that now whenever I call:
sp.getBoolean("CHECKBOX",false);
I will get true because I won't have to use the default value of false.
Try to make this easy for you...
First, in your proferences xml, each text box and check box needs it's own key.
Secondly, to make it easy for you to read/understand you should assign a different name for the pref save method void savePrefs(String key, String value).
For example String: void savePrefsString(String key, String value)
For example boolean: void savePrefsBoolean(String key, boolean value)
Be sure each one is called appropriately (savePrefsBoolean for boolean and savePrefsString for edittext).
Then for each edit text you will want to retrieve the key from preferences for that edittext.
Example:
String name1 = sp.getString("NAME1", "Kg");
String name2 = sp.getString("NAME2", "Kg");
String name3 = sp.getString("NAME3", "Kg");
Then:
et1.setText(name1 + (" kg"));
et2.setText(name2 + (" kg"));
et3.setText(name1 + (" kg"));
Do the same for your checkboxes (they are actually true/false booleans).
Example:
boolean cb1 = sp.getBoolean("CHECKBOX1", false); //false is default value
boolean cb2 = sp.getBoolean("CHECKBOX1", false);
boolean cb3 = sp.getBoolean("CHECKBOX1", false);
Then to set value from prefs:
if(cb1){
cb1.setChecked(true);
}else{
cb1.setChecked(false);
}
and to save what the user has pressed:
savePrefsBoolean("CHECKBOX1", cb1.isChecked()); // get check value of checkbox
savePrefsBoolean("CHECKBOX2", cb2.isChecked());
savePrefsBoolean("CHECKBOX3", cb3.isChecked());

How to make boolean buttons in Android random?

I have four buttons and they are boolean - i want to make them different every time the image and the text inside them changes - i.e. once a button will be true, other time it will be false. How can this be done? Thank u !
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
startActivity(item.getIntent());
return true;
}
public void onNoButton(View v) {
handleAnswerAndShowNextQuestion(false);
}
public void onYesButton(View v) {
handleAnswerAndShowNextQuestion(true);
}
private void handleAnswerAndShowNextQuestion(boolean bAnswer) {
int curScore = mGameSettings.getInt(GAME_PREFERENCES_SCORE, 0);
int nextQuestionNumber = mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 1) + 1;
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, nextQuestionNumber);
// Log the number of "yes" answers only
if (bAnswer == true) {
editor.putInt(GAME_PREFERENCES_SCORE, curScore + 1);
}
editor.commit();
if (mQuestions.containsKey(nextQuestionNumber) == false) {
// Load next batch
try {
loadQuestionBatch(nextQuestionNumber);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Loading updated question batch failed", e);
}
}
if (mQuestions.containsKey(nextQuestionNumber) == true) {
// Update question text
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getQuestionText(nextQuestionNumber));
// Update question image
ImageSwitcher questionImageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
Drawable image = getQuestionImageDrawable(nextQuestionNumber);
questionImageSwitcher.setImageDrawable(image);
} else {
// Tell the user we don't have any new questions at this time
handleNoQuestions();
}
}
private void handleNoQuestions() {
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getResources().getText(R.string.no_questions));
ImageSwitcher questionImageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
questionImageSwitcher.setImageResource(R.drawable.noquestion);
// Disable yes button
Button yesButton = (Button) findViewById(R.id.Button_Yes);
yesButton.setEnabled(false);
// Disable no button
Button noButton = (Button) findViewById(R.id.Button_No);
noButton.setEnabled(false);
}
This is simple. Just create your environment (image and buttons) like normal. Assign your proper listeners for the buttons. Then create a new Random object. Get a random boolean. If the boolean == true, add the true button to your layout first, else add the false button.
public View getEnvironment(Context context) {
RelativeLayout rl = new RelativeLayout(context);
ImageView image = new ImageView(context);
image.setId(0);
// Instantiate image (add image and listener is needed)
Button truee = new Button(context);
// Add event listener
Button falsee = new Button(context);
// Add event listener
Random rand = new Random();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(80, 80);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
image.setLayoutParams(lp);
RelativeLayout.LayoutParams left = new RelativeLayout.LayoutParams(40, 80);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.BELOW, 0);
RelativeLayout.LayoutParams right = new RelativeLayout.LayoutParams(40, 80);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.BELOW, 0);
if (rand.nextBoolean()) {
truee.setLayoutParams(left);
falsee.setLayoutParams(right);
} else {
truee.setLayoutParams(right);
falsee.setLayoutParams(left);
}
rl.addView(image);
rl.addView(truee);
rl.addView(falsee);
return rl;
}
Edit:
I did not realise the OP was asking for a CheckBox before posting. I will leave this up just in case someone wants it.
It's not wise to use buttons as radio buttons. Both are different, have different purposes and the idea is semantically wrong.
Skin your radio buttons as normal buttons.
Here is a good guide on skinning android
http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

Categories

Resources