Want to Pre-set Radio buttons based on string - android

Hi I am creating an edit page for a database and i want to
get the string ive passed over via intent
then depending on if the string is equal to "on" or "off"
i want to change the the selection of the radio button
so far i have this
radiogroup = R.id.radioGroup1;
if (switch.equals("on")){
radiogroup.check(R.id.radio1);
R.id.radio1.setChecked(true);
} else {
radiogroup.check(R.id.radio0);
R.id.radio1.setChecked(true);
}
btw this is in my oncreate as i want it to run as soon as the Activity is open

Inflate your radio buttons, per example with
RadioButton radio1 = findViewById(R.id.radio1);
Then you can use
radio1.setChecked(true/false)
like you were trying.

You need to find the RadioButtons like this:
RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
after that you can call
radio1.setChecked(true); or radio1.setChecked(false);

Related

Radio button shuffling in android

I am trying to shuffle radio button in android, here's is the code what i have done till now, but as I proceed further I am not getting what should be the code or how to do that.
Please correct if I am wrong, or doing some silly mistake as I am new to it.
ArrayList<RadioButton> arrayText = new ArrayList<>();
arrayText.add(ropt0);
arrayText.add(ropt1);
arrayText.add(ropt2);
arrayText.add(ranswer);
Collections.shuffle(arrayText);
I am not getting what next after Collections.shuffle(arrayText);
should be the code
Please suggest
I think you don't quite understand what are you doing and what do you want. In your code you just adding all your RadioButtons to ArrayList and then shuffle this list. This is same as:
ArrayList<Stirng> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
Collections.shuffle(list);
You see, this isn't connected to UI in any way. If you want to shuffle buttons in UI (so user will see it) you have 2 approaches:
Create and add your radio buttons in the code. Define some RadioGroup in view XML file and then create and add your RadioButtons
RadioGroup lay = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> buttons = new ArrayList<>();
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
buttons.add(rb1);
buttons.add(rb2);
Collections.shuffle(buttons);
for(RadioButton rb:
buttons){
lay.addView(rb,new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
Shuffle just a right button position. Create N buttons in XML. And then set all texts in your code, so you can decide which button is the right one.
List<String> answers = new ArrayList<>(4); // 4 - button number
//Fill the list here
int rightAnswerPosition = ...; //Decide it when you fill your list
rb1.setText(answers.get(0));
//.. and so on

getCheckedRadioButtonId() of RadioGroup returns 2131034181/2/3/4

as part of my application I have to create four radioButtons within a radiogroup and get the radio button clicked. I wrote the following code but my selectId attribute gives values 2131034181 when first option is selected and 2131034182 when second option is selected and so on, there's change in unit digit of value when 3 and 4 are clicked. Why is it so?
int selectId=rbg.getCheckedRadioButtonId();
RadioButton selected= (RadioButton) rbg.findViewById(selectId);
String selected_user = (String) selected.getText();
To get radiobutton,use findViewById from context instead of radiogroup,So change
RadioButton selected= (RadioButton) rbg.findViewById(selectId);
String selected_user = (String) selected.getText();
to
RadioButton selected = (RadioButton)findViewById(selectId);
String selected_user = selected.getText().toString();

Two radio buttons getting selected in Radio Group

Two buttons getting selected in the radio group.
I do not know where I am getting wrong. Please help me out.
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
rg.clearCheck();
rg.setId(Integer.valueOf(entry1.getKey()));
Log.v("rg getid", "rg"+rg.getId());
for(int i =0;i<values.size();i++){
// Create Button
final RadioButton btn = new RadioButton(Survay_MainActivity.this);
btn.setId(i);
btn.setTextColor(Color.parseColor("#000000"));
btn.setBackgroundColor(Color.TRANSPARENT);
btn.setGravity(Gravity.LEFT);
btn.setText(values.get(i));
rg.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
JSONObject quesAns = new JSONObject();
String ans=btn.getText().toString().trim();
try {
quesAns.put(String.valueOf(rg.getId()), ans);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonarray.put(quesAns);
Log.v("jsonarray", "jsonarray"+jsonarray);
}
});
}
views.addView(rg);
1) I am creating the RadioGroup out of the loop.
2) Adding radio button to the RadioGroup in the for loop
3) When the loop finishes the RadioGroup is added to the linerlayout.
You just need to change different ids for different radio button.
There may be some id clash in gen file.
Radio button1 : android:id="#+id/one
Radio button2 : android:id="#+id/two"
Radio button3 : android:id="#+id/three"
Hope this would help.
There are some things misplaced these changes you have to make out.
1.Change your OnClickListener by OnCheckChangeListener
2.Clear check your radioGroup after adding all radioButtons and before adding it in LinearLayout.
Your problem comes from the fact that you are setting an id for your items manually
rg.setId(Integer.valueOf(entry1.getKey()));
btn.setId(i);
Android works with id's generated automatically that are different. Setting the id's manually it might happen that you give the same ID to the radio group and the radio button.
I used your code and set the group to ID 3 and radios from 0 to 4.
Needless to say that after I click the button with id 3 it always stays on because the group has the same id.
So, try to remove the id setting part, or, if you insist make sure you always have distinct id's.
I faced the same problem. The cause is the RadioGroup's id is same as one of RadioButtons
the problem is this line. you didn't send where is this code belonged to. when you get out of this scope and come back you will create new RadioGroup. You should have one and only one RadioGroup so the RadioButtons have same RadioGroup and by default one of them will be selected. To this, you can define RadioGroup in an XML and use this to define variable for it RadioGroup rg = (RadioGroup) findViewById(R.id.YOURNAME); or you can define your RadioGroup as Instance variable(Global).
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);

Building an android quiz without using unnecessary activities

I am new to Android programming and want to make a simple quiz that has around 50 questions. My problem is that I am unsure of how to do this without creating 50 individual layouts, java classes and activities. I would like each question to be different, and the format of the questions is multiple choice (A, B, C and D). Any helpful suggestions?
Thanks.
It's easy! Just create one acitivty and then update it's data every time user answers the question:
1) Changing TextView with question
2) Changing Buttons with answers
/*updating question
*questions is the list of questions obviously
*answers is the list of lists:) When you take a list from answers
*you get a list with
*4 items - they are the answers for current question
*/
public void updateQuestion() {
mQuestionTextView.setText(questions.get(currentQuestionNum));
mButtonA.setText(answers.get(currentQuestionNum).get(0));
mButtonB.setText(answers.get(currentQuestionNum).get(1));
mButtonC.setText(answers.get(currentQuestionNum).get(2));
mButtonD.setText(answers.get(currentQuestionNum).get(3));
}
EDIT:
public ArrayList<String> getAnswers(int questionsNumber) {
return ArrayList<String> currentQuestionAnswers = answers.get(questionsNumber);
}
At every question, you can do something like this:
TextView textview = (TextView) findViewById(R.id.question);
question.setText(current_question);
And for the multiple choice, if you are using a RadioGroup, you can have 4 textviews that correspond to each option and you can modify the text, depending on the question.
For recognizing which option has been selected, put this inside an onClickListener
radioGroup = (RadioGroup) findViewById(R.id.radio);
btnSend = (Button) findViewById(R.id.btnSend);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
}
});

Dynamically Change the Text of RadioButton

I am creating a quiz application in android.For each question I have set up 4 radio buttons for the user to click. How do i change the text of the radiobutton dynamically for each question? The text for these radio button are stored in a raw text file.
Here you go.
I have set up 4 radio buttons for the user to click.
You must have defined them in radioGroup,right?
Then you can iterate in RadioGroup to set names to RadioButton
or you can get RadioButton by index and set name to it.
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.group);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(String.valueOf(i));
}
You can get an object of the button by using
RadioButton button = (RadioButton)findViewById(R.id.yourButtonId);
Then write:
button.setText("This is another text...");
Didn't try to run it, but it should work...

Categories

Resources