Random "GROUP" of radiobutton? - android

How can i Random "GROUP" of radiobutton?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
Collections.shuffle(objects);
List<Button> buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.button0));
buttons.add((Button)findViewById(R.id.button1));
buttons.add((Button)findViewById(R.id.button2));
buttons.add((Button)findViewById(R.id.button3));
buttons.add((Button)findViewById(R.id.button4));
for (int i = 0; i < objects.size(); i++) {
buttons.get(i).setText(objects.get(i).toString());
}
}
The code about is not a "GROUP" of radio button, how it became a RadioGroup? and display in random order. Anyones help?

here is a example of creating radio button with radio group....
private void createRadioButton() {
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
Collections.shuffle(objects);
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText(objects.get(i)+"");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
}
});
}

Related

how to create radio group just below on checkbox in android

In my app i have created list view of checkbox in linear layout and i have created dynamic radio group of three radio button.when user click on any checkbox than radio group will be created at the end. but i want to create this radio group just below the clicked checked box. i am trying to set cursor position but not able to do.i don't have any idea what to do for this.
this is part of main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);
for (int i = 0; i < 25; i++) {
final CheckBox checkBox = new CheckBox(this);
checkBox.setText(Oils_and_Condiments[i]);
checkboxContainer.addView(checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
for (int i = 0; i < Oils_and_Condiments.length; i++) {
String a= (String) checkBox.getText();
if(a==Oils_and_Condiments[i])
{
RadioGroup rg = create_rb(i);
checkboxContainer.get
checkboxContainer.addView(rg);
rg.setVisibility(View.VISIBLE);
//rg.getChildAt(i).setVisibility(View.VISIBLE);
}
RadioGroup create_rb(int a ){
final RadioGroup radiogp = new RadioGroup(this);
radiogp.setOrientation(RadioGroup.HORIZONTAL);
RadioButton rb0 = new RadioButton(this);
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
rb0.setText("1KG");
rb0.setTextSize(10);
rb1.setText("2KG");
rb1.setTextSize(10);
rb2.setText("3KG");
rb2.setTextSize(10);
radiogp.addView(rb0);
radiogp.addView(rb1);
radiogp.addView(rb2);
//checkboxContainer.addView(radiogp);
return radiogp;
}

Set the ArrayList value to the RadioButton in the for loop

I am getting arrayList [5,8,13,18,19] from the server and I would like to create a RadioGroup in the xml file. After selecting the diserable items, I will put the selected items in arrayList and transmit the query to the server after clicking the OK button. How can I create such as programmatically RadioGroup?
I have tried this but I dont know how to loog through to set the RadioButton with the ArrayList value. How can I get that tp work?
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
items.get(i) = new RadioButton(this);
}
}
Try this
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
RadioButton rb = new RadioButton(this);
rb.setText(items.get(i)+"");
rg.addView(rb);
}
With the addView you are adding the dynamically created RadioButton to the RadioGroup.

How to make sure if all the radio buttons are checked by the user?

So I am trying to make a simple quiz application and I want to make sure that the user answers all the question before continuing to the next activity. At start the button I have set is unavailable and I want to make it available ONLY if all the question have been answered. I cant figure out where and how to check if all the question have an answer.
public class Quiz extends Activity
{
Button buttHole;
String countryName[] = { "India", "Pakistan", "China", "Nepal", "Pichmaala", "Blah" };
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
buttHole = (Button)findViewById(R.id.button1);
buttHole.setEnabled(false);
//Creating the list of Questions
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
for (int i = 1; i <= 3; i++)
{
//create text button
TextView title = new TextView(this);
title.setText("Question Number:" + i);
title.setTextColor(Color.GREEN);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[countryName.length];
RadioGroup radGr = new RadioGroup(this);
radGr.setOrientation(RadioGroup.VERTICAL);
radGr.setOnClickListener(l)
for (int j = 0; j < countryName.length; j++)
{
rb[j] = new RadioButton(this);
radGr.addView(rb[j]);
rb[j].setText(countryName[j]);
}
mLinearLayout.addView(radGr);
}
}
}
give radiobuttons id's as well and when you are going to submit this form(for example) mean on button click , get radio button object by id and then check radiobutton.isChecked().
Try following code to check whether user has given answer to all question or not
First Solution
int count = mLinearLayout.getChildCount();
for(int i=1;i<count ;i+=2){
RadioGroup rg = (RadioGroup) mLinearLayout.getChildAt(i);
if(rg.getCheckedRadioButtonId()==-1){
//Answer is not selected
//Do something to prevent execution
break;
}
}
Second Solution
public class Quiz extends Activity {
Button buttHole;
String countryName[] = { "India", "Pakistan", "China", "Nepal",
"Pichmaala", "Blah" };
ArrayList<RadioGroup> arrGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
buttHole = (Button) findViewById(R.id.button1);
buttHole.setEnabled(false);
// Creating the list of Questions
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
arrGroup = new ArrayList<RadioGroup>();
for (int i = 1; i <= 3; i++) {
// create text button
TextView title = new TextView(this);
title.setText("Question Number:" + i);
title.setTextColor(Color.GREEN);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[countryName.length];
RadioGroup radGr = new RadioGroup(this);
// take a reference of RadioGroup view
arrGroup.add(radGr);
radGr.setOrientation(RadioGroup.VERTICAL);
radGr.setOnClickListener(l);
for (int j = 0; j < countryName.length; j++) {
rb[j] = new RadioButton(this);
radGr.addView(rb[j]);
rb[j].setText(countryName[j]);
}
mLinearLayout.addView(radGr);
}
}
public boolean checkQuestionsAnswer() {
int count = arrGroup.size();
for (int i = 0; i < count; i++) {
if (arrGroup.get(i).getCheckedRadioButtonId() == -1) {
//Return false, answer is remaining to given
//You can pass here position also to know question number
return false;
}
}
//Return true if all answer are given
return true;
}
}

table row selection error in scrollview

i am creating a table layout with radio group by dynamically adding radiobuttons to it and i want to know which row is selected containing the radio group and i want to retrive the texview data also in the same row .how can i do that any suggestion will be a great help for me.
public class TabActivity extends Activity {
TableLayout table;
RadioGroup mRadioGroup;
ArrayList<String> list_name;
int color_blue = -16776961;
int color_gray = -7829368;
int color_black = -16777216;
int color_white = -1;
final int CHECK_BUTTON_ID = 982301;
int ids_check[];
boolean bool_check[];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
table = (TableLayout) findViewById(R.id.tableLayout1);
list_name = new ArrayList<String>();
list_name.add("Close");
list_name.add("Cristiano");
list_name.add("David");
list_name.add("Fernando");
list_name.add("Messi");
list_name.add("Kaka");
list_name.add("Wayne");
list_name.add("use");
list_name.add("e");
list_name.add("eff");
list_name.add("euyr");
list_name.add("ejjyytuty");
list_name.add("madre");
list_name.add("yuir");
list_name.add("eyrty");
list_name.add("etytr");
list_name.add("ewrrtt");
bool_check = new boolean[list_name.size()];
ids_check = new int[list_name.size()];
createTableRows();
}
public void createTableRows()
{
for (int i = 0; i < list_name.size(); i++)
{
final TableRow table_row = new TableRow(this);
TextView tv_name = new TextView(this);
Button btn_check = new Button(this);
ImageView img_line = new ImageView(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table_row.setBackgroundColor(color_black);
table_row.setGravity(Gravity.CENTER_HORIZONTAL);
// table_row.setFocusable(true);
mRadioGroup = new RadioGroup(this);
// test adding a radio button programmatically
final RadioButton[] mbutton=new RadioButton[7];
for(int l=0;l<7;l++){
mbutton[l]=new RadioButton(this);
mbutton[l].setText("test"+l);
mRadioGroup.addView(mbutton[l]);
}
// LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
// RadioGroup.LayoutParams.WRAP_CONTENT,
// RadioGroup.LayoutParams.WRAP_CONTENT);
// mRadioGroup.addView(newRadioButton);
tv_name.setText((CharSequence) list_name.get(i));
tv_name.setTextColor(color_blue);
tv_name.setTextSize(30);
tv_name.setTypeface(Typeface.DEFAULT_BOLD);
tv_name.setWidth(150);
btn_check.setLayoutParams(new LayoutParams(30, 30));
btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked);
img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
img_line.setBackgroundResource(R.drawable.separater_line);
table_row.addView(tv_name);
table_row.addView(btn_check);
table_row.addView(mRadioGroup);
table.addView(table_row);
table.addView(img_line);
//table.addView(mRadioGroup);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
int t=table.indexOfChild(table_row);
System.out.println(t);
if(btn.getId() == checkedId) {
String text = btn.getText().toString();
// do something with text
Log.d(text," event1");
return;
}
}
}
});
}
}
}
Why are you not using ListView ? there's nothing in the code that cannot be done by a list view?
You can add you layout by setting the custom adapter.

Creating RadioButtons programmatically

I want to create a series of radio buttons which correspond to an array of strings within an Android app. The radio buttons should toggle content to be displayed from the array. How do I do this?
You must add the radio buttons to a RadioGroup and then the RadioGroup to the layout
I miss some information like what is submit, but your code should look like:
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
Questions();
}
});
}
Another code for dynamically creating the radiobutton
<TableRow>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiobuttons">
</RadioGroup>
</TableRow>
Code:
public void makeRadioButtons(Vector tmpVector, int i, LinearLayout.LayoutParams lp)
{
RadioButton rb = new RadioButton(this);
rb.setText((String) tmpVector.elementAt(i));
//rg is private member of class which refers to the radio group which I find
//by id.
rg.addView(rb, 0, lp);
}

Categories

Resources