This is the layout I want to create but dynamically through java code.
Here parent layout is LinearLayout.
The tableLayout created through java code consists of 5 radio buttons which are created dynamically.
I need to place these radio button in radio group.
How to do that.
So far i tried this which is working fine. But am not able to add all the 5 radio buttons in 1 radio group.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); //parent layout
t1 = new TableLayout(this);
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
t1.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
TableRow row1 = new TableRow(this);
row.addView(rb1);
row.addView(rb2);
row1.addView(rb3);
t1.addView(row);
t1.addView(row1);
l.addView(t1);
}
I can't find you'r declaration of a RadioGroup.
You need something like this:
...
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
row.addView(rg);
...
RadioGroup inherits from LinearLayout. You'll have to create your own implementation of RadioGroup that inherits from TableLayout.
If you examine the source code of RadioGroup, you'll see it just keep track of its child views which are RadioButton's and makes sure only one of them is checked. Apart from that it provides a listener if checked RadioButton changes.
You can try modifying the source to make it extend from TableLayout etc.
Related
Is possible to place the checkbox and button on the same line in a vertical Linear Layout?
I made the button and checkbox programmatically with this code:
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.line);
btn.setTextSize(24);
btn.append(rs1.getString(0));
CheckBox checkBox = new CheckBox(this);
//ll = Linear Layout (vertical)
ll.addView(checkBox);
ll.addView(btn);
In this case the checbox is over the button
Someone can help me?
EDIT:
Image that explain what i want
You can use a higher hierarchy for your views. Like adding your both views in a horizontal LinearLayout and then add the new layout to your root layout. Your code would look like this:
LinearLayout myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.line);
btn.setTextSize(24);
btn.append(rs1.getString(0));
CheckBox checkBox = new CheckBox(this);
myLinearLayout.addView(checkBox);
myLinearLayout.addView(btn);
ll.addView(myLinearLayout);
I'm trying to add buttons in Table layout.
Initially I will have a single row with a single button.
When the user clicks that button, It should create 2 dynamic buttons in the next row 'Horizontally'.
But with my code, I'm getting vertically dynamic buttons Instead of horizontally.
while(id <rowcount+2)
{
tr = new TableRow(MainActivity.this);
table.addView(tr);
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
you are creating new object of TableRow each time, TableRow represents elements in Horizontal and TableLayout represent data in Vertical that's why you getting Buttons in Vertical manner
tr = new TableRow(MainActivity.this);
while(id <rowcount+2)
{
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);
you are creating a new row each time for the loop
try this.
tr = new TableRow(MainActivity.this);
while(id <rowcount+2) {
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);
I am creating an attendance list of student at run time from database and wants to create dynamically radio buttons in groups against each student and then take their attendance values on a submit button: my code looks like
Ll = new LinearLayout(this);
rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
RadioButton rb1 = new RadioButton(this);
rb1.setText("P");
rb1.setId(id);
rg.addView(rb1); //the RadioButtons are added to the radioGroup instead of the layout
///////////////////////////////////////////////
RadioButton rb2 = new RadioButton(this);
rb2.setText("A");
rb2.setId(id+1);
rg.addView(rb2); //the RadioButtons are added to the radioGroup instead of the layout
/////////////////////////////////////
RadioButton rb3 = new RadioButton(this);
rb3.setText("L");
rb3.setId(id+2);
rg.addView(rb3); //the RadioButtons are added to the radioGroup instead of the layout
Ll.addView(rg);//you add the whole RadioGroup to the layout
tr.addView((View)Ll); // A
how can i get values from dynamically created radion groups in my Android activity
Hope this code will help you to get selected radio button value
int selectedId = rg.getCheckedRadioButtonId();
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Toast.makeText(context,
radiochecked.getText(), Toast.LENGTH_SHORT).show();
I need to implement a multiple choice question. I have to add radio buttons and text view for multiple choice. I need to implement it dynamically corresponding to the number of choices. Can someone help me
for (Answer answer : answers) {
LinearLayout linearLayoutRw2 = new LinearLayout(this);
linearLayoutRw2
.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
RadioGroup radioGroup = new RadioGroup(
ShowQuestionsActivity.this);
linearLayoutRw2.addView(radioGroup);
RadioButton rb = new RadioButton(ShowQuestionsActivity.this);
radioGroup.addView(rb);
TextView ansText = new TextView(ShowQuestionsActivity.this);
ansText.setText(Html.fromHtml(answer.getAnswerText()));
linearLayoutRw2.addView(ansText);
linearLayoutShowQues.addView(linearLayoutRw2);
}
Try Like this,
First create a ListView dynamically.
ListView choicelist = new ListView(this);
choicelist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
choicelist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, your_answer_list));
choicelist.setSelector(new ColorDrawable(0x0));
choicelist.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
choicelist.setCacheColorHint(0);
choicelist.setVerticalFadingEdgeEnabled(false);
And Finally add this List to your Layout
linearLayoutShowQues.addView(choicelist);
For Customisation you can use Custom Adapter.
Hope this will help you.
I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).
for(int j=0;j<3;j++)
{
RadioGroup rdgrp = new RadioGroup;
for(int i=0;i<=10;i++)
{
RadioButton rdbut = new RadioButton(this);
rdbut.setText("RadioButtion"+i);
rdbut.setId(i);
rdbut.setTag("somename");
rdgrp.addView(rdbut);
}
}
the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.
What could be the problem?
Change your code like this.
RadioGroup rdgrp[] = new RadioGroup[3];
For(int j=0;j<3;j++)
{
RadioButton rdbut[] = new RadioButton[10];
For(int i=0;i<=10;i++)
{
rdbut[i].setText("RadioButtion"+i);
rdbut[i].setId(j*100+i);
rdbut[i].setTag("somename");
rdgrp[j].addView(rdbut[i]);
}
}
You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.
Thanks
Sunil
Use Some thing like this xml design in user layout file.
<TableLayout
android:id="#+id/tbl_layoutchoice"
style="#style/InfoTableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip" >
<RadioGroup
android:id="#+id/SelectLayout_Group"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</TableLayout>
and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this
mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);
and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.
ArrayList<String> layoutlist = new ArrayList<String>(3);
int index = -1;
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
for (String layout : layoutlist) {
RadioButton r = new RadioButton(this);
index++;
r.setText(layout);
r.setId(index);
r.setLayoutParams(lp);
r.setTextAppearance(this, R.style.TextBase);
mRadioGroup.addView(r);
}
So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.