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);
Related
I want to work as AutoCompleteTextview. But I am not using auto complete text view in my project. I have used edit text and taking it the value for sorting the value of adapter using those values of adapter I am creating a dynamic button. But actually, I want to delete dynamically created button. When a user enters new value in edit text at that case it sorts new value in adapter according to the button has to created. But, my problem is that dynamically created button does not get deleted when a user enters new text on edit text view. It has to looking like this:
if (!s.equals("")) {
final String query = s.toString().trim();
filteredTags.clear();
((ViewManager) btnTag.getParent()).removeView(btnTag);
for (int i = 0; i < TagArray.size(); i++) {
final String tagName = TagArray.get(i).gettagName();
if (tagName.contains(query)) {
filteredTags.add(TagArray.get(i));
}
}
count1 = filteredTags.size();
layout = (LinearLayout) dialog.getCustomView().findViewById(R.id.layoutTags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
layout.setWeightSum(1);
float rowneed = ((float) count1 / 5);
k = 0;
for (int i = 0; i < ceil(rowneed); i++) {
row1 = new LinearLayout(getContext());
row1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
/* layout.setVisibility(View.VISIBLE);
row.setVisibility(View.VISIBLE);*/
for (int j = 0; j < 5; j++) {
btnTag = new Button(getContext());
btnTag.setHeight(15);
btnTag.setWidth(0);
btnTag.setMinimumWidth(155);
btnTag.setMinimumHeight(135);
mTagList1 = new ArrayList<>();
if (k < count1) {
btnTag.setText(filteredTags.get(k).gettagName());
btnTag.setId(k);
k++;
btnTag.setVisibility(View.VISIBLE);
} else {
btnTag.setVisibility(View.INVISIBLE);
}
Log.e("count", " " + k + " " + count1 + " " + ceil(rowneed) + " " + edtTag.getText().toString());
btnTag.setTextSize(7);
btnTag.setGravity(0);
row1.addView(btnTag);
}
layout.addView(row1);
}
for (int btnId = 0; btnId < filteredTags.size(); btnId++) {
btnTag = (Button) dialog.getCustomView().findViewById(btnId);
final int finalId1 = btnId;
btnTag.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TagNameArray.add(new Tags(filteredTags.get(finalId1).gettagId(), filteredTags.get(finalId1).gettagName()));
// Log.e("button","Button clicked index = " + finalId +" "+ TagArray.get(finalId1).gettagName()+" "+TagNameArray.size());
}
});
}
}
Add this line of code :
layout.removeAllViews();
layout.invalidate();
row.removeAllViews();
row.invalidate();
This might help you, give me a feedback for what you got, wish I help you
set a dynamic tag for btnTag for example
btnTag.setTag(DynamicTagInt++);
and then
row1.removeView(btnTag.findViewById(DynamicTagInt));
//DynamicTagInt= the desired button that you want to delete
or by the ID of the button for example
row1.removeView(btnTag.findViewWithTag(k));
I've created some buttons dynamically, when i start activity all is well, but when i rotate a screen or press back Button, i lose all buttons, i've googled then i've found that the views without IDs can't be saved, so i want to set IDs for them dynamically and i want to find them inside R.java file with there name like this :
public static final class id {
public static final int Button1=0x7f070027;
public static final int Button2=0x7f070024;
}
I've tried setId() but the problem is not solved, and i think it's not the same thing when use setID(); and set id manually inside String file, so i need your help thanks in advance(^_^).
here's my source code :
TableLayout TL = (TableLayout) findViewById(R.id.tlRDV);
TL.removeAllViews();
j = 0;
for (int i = 0; i < nbH; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tr.setId(View.generateViewId());
TextView text_view = new TextView(this);
Button b = new Button(this);
text_view.setWidth(1000);
j = j + averageConsultationTime;
HMA = ((BeganConsultationH * 60) + (BeganConsultationM))
+ j - averageConsultationTime;
int h = HMA / 60;
int m = HMA % 60;
b.setBackgroundColor(0xff99cc00);
b.setText(h + "H" + m);
tr.addView(b);
for (int k = 0; k < 7; k++) {
Button but = new Button(this);
but.getId(View.generateViewId());
but.setText("" + but.getId());
but.setOnClickListener(getOnClickDoSomething(but,
jours[k] + "_" + h + "H" + m));
tr.addView(but);
}
TL.addView(tr);
}
}
}
}
I've created many Buttons dynamically with Tags identifiers, but I don't know how to find them by tags, I've tried findViewWithTag but it doesn't work.
If someone can show me how to do it by Identifiers or by Tags ?
Can someone help me and give me an example how to do it?
Thank you in advance.
Here is my source code :
public void onClick(View v) {
if (v == buttonPlanificationRdvMed) {
for (int i = 0; i < nbH; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
//tr.setOrientation(tr.HORIZONTAL);
TextView text_view = new TextView(this);
Button b = new Button(this);
text_view.setWidth(1000);// give how much you need
j = j + averageConsultationTime;
HMA = ((BeganConsultationH * 60) + (BeganConsultationM))
+ j - averageConsultationTime;
int h = HMA / 60;
int m = HMA % 60;
b.setBackgroundColor(0xff99cc00);
b.setText(h + "H" + nbH);
tr.addView(b);
for (int k = 0; k < 7; k++) {
Button but = new Button(this);
but.setTag(table[k]+"_"+h + "H" + nbH);
but.setText(but.getTag()+""+table[k]);
tr.addView(but);
}
TL.addView(tr);
}
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");
}
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