Here is my code
final RadioButton[] rb = new RadioButton[6];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
for(int i=0; i<6; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Dynamic Radio Button " + i);
rb[i].setId(i);
rb[i].getGlobalVisibleRect(null);
rg.addView(rb[i]);
}
ll.addView(rg);
I am trying to display these radio buttons programatically. I got 6 radio buttons but am unable to display these as rows and columns.
You can use TableLayout to arrange the Radio Button in Rows and Columns
Sample Code
LinearLayout ll = new LinearLayout(this);
TableLayout tl = new TableLayout(this);
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
for (int j = 0; j < 6; j++) {
RadioButton rb = new RadioButton(this);
rb.setText("Dynamic Radio Button " + i);
rb.setId(i);
rb.getGlobalVisibleRect(null);
tr.addView(rb);
}
tl.addView(tr);
}
ll.addView(tl);
setContentView(ll);
Screenshots link
Related
I am getting issue with radiobutton. I have created dynamic radiobuttons but i am able to multiple select.I need to check only one as per radiobutton functionality.
Here is my code:
List<String> answers = Constants_Variables.missionMainResponse.getResponse().getData().get(early_pos).getQuestions().get(position).getANS();
ViewGroup answer_radio_group = (ViewGroup) itemView.findViewById(R.id.answer_radio_group);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
final RadioButton[] rb = new RadioButton[answers.size()];
Log.v("", TAG + "=Answers list size=" + answers.size());
for (int j = 0; j < answers.size(); j++) {
Log.v("", TAG + "=Answers=" + j);
rb[j] = new RadioButton(context);
rb[j].setText(answers.get(j) + "");
rb[j].setId(j);
ll.addView(rb[j]);
}
rb[0].setChecked(true);
answer_radio_group.addView(ll);
Please help me out I want to select only one option.
Thanks.
you need to add RadioButtons in RadioGroup first and than in other view or layout.
answer_radio_group.addView(ll); this line should be inside for loop
for (int j = 0; j < answers.size(); j++) {
Log.v("", TAG + "=Answers=" + j);
rb[j] = new RadioButton(context);
rb[j].setText(answers.get(j) + "");
rb[j].setId(j);
answer_radio_group.addView(rb[j]);
}
rb[0].setChecked(true);
I am creating a number of buttons in a table row programmatically, but they always end up left-aligned. How can I center the entire row? I am trying this to create each row:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
You can simply center the row using a Gravity. Just set it to your row with
row.setGravity(Gravity.CENTER_HORIZONTAL);
Try this:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setLayoutParams(buttonParams);
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
I want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.
The orientation of the Radio Group is horizontal. I am creating Radio Group and Radio Buttons Dynamically . It is displaying the radio buttons with respect to the device screen size. How to align the Radio Buttons in multiple rows if they are in large number.
Many Thanks.
if (status.equals("success")) {
JSONArray jarry = jobj
.getJSONArray("questions");
for (int i = 0; i < jarry.length(); i++) {
LinearLayout ll = new LinearLayout(
RajaAndroid.this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.VERTICAL);
JSONObject jo = jarry.getJSONObject(i);
// rg = new RadioGroup(RajaAndroid.this);
RadioGroup rg = new RadioGroup(
RajaAndroid.this); // create the
// RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or
// RadioGroup.VERTICAL
TextView textView = new TextView(
RajaAndroid.this);
textView.setText(jo.getString("q_id")
+ ". " + jo.getString("question"));
textView.setTextColor(0xff0000ff);
textView.setTextSize(20);
layout.addView(textView, p);
layout.addView(rg, p);
JSONArray ja = jo.getJSONArray("options");
for (int j = 0; j < ja.length(); j++) {
JSONObject jo1 = ja.getJSONObject(j);
RadioButton rb = new RadioButton(
RajaAndroid.this);
ImageView iv = new ImageView(
RajaAndroid.this);
iv.setImageResource(R.drawable.excellent);
Drawable db = getDrawable(R.drawable.ic_launcher);
// are added to the radioGroup
// instead of the layout
rb.setText(jo1.getString("option_name"));
rb.setTextColor(0xff000000);
rb.setTextSize(18);
rb.setCompoundDrawablesRelativeWithIntrinsicBounds(
null, db, null, null);
rb.setGravity(Gravity.BOTTOM);
rb.setPadding(0, 0, 0, 5);
rb.setOnClickListener(RajaAndroid.this);
//rg.addView(iv, j);
rg.addView(rb);
}
}
} else {
Alerts.alertWithOk(RajaAndroid.this,
"No Questions found", "Ok");
}
I have a list of bitmap images, How can I add this bitmap to a tableLayout programmatically?
thanks.
Try something like this:
TableLayout table = new TableLayout(this);
for (int i = 0; i < mRows; i++) {
TableRow tr = new TableRow(mContext);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < mCols; j++) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.star_on)
tr.addView(view);
}
table.addView(tr);
}
This generates the whole table / grid programmatically. You can see how it instantiates an ImageView and sets the image.