How can i add as well as remove layout pragmatically in android? - android

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));

Related

How to implement setOnClickListener to get selected value from dynamically created RadioButton Android

I have a dynamically created radio buttons inside a RadioGroup since the values are coming from the database. I want to be able to implement setOnClickListener so I can get the whatever the selected value is. Whenever I try to click on the radio button, nothing shows up.
public void viewAll() {
Cursor res = myDb.getAllData();
LinearLayout bg = (LinearLayout) findViewById(R.id.backgroundSM);
LinearLayout display = (LinearLayout) findViewById(R.id.viewAllMsg);
final RadioButton[] rb = new RadioButton[5];
rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
res.moveToFirst();
for(int i=0; i<res.getCount(); i++){
rb[i] = new RadioButton(this);
//Toast.makeText(getApplicationContext(),res.getString(1) + " ", Toast.LENGTH_LONG).show();
rb[i].setText(" " + res.getString(1)
+ " " + res.getInt(0));
rb[i].setId(i + 100);
rg.addView(rb[i]);
res.moveToNext();
}
display.addView(rg); // add the whole RadioGroup to the layout
rg.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
}
int id = rg.getCheckedRadioButtonId();
Toast.makeText(getApplicationContext(),id + " worked", Toast.LENGTH_LONG).show();
}
});
}
What is wrong with my code? Thank you so much for your help.
Selected position will be saved in position variable
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (radioGroup.getChildAt(i).getId() == id) {
position = i + 1; //+1 is used to have a start value of 1 like your example
break;
}
}
}
});

Android : How to add IDs Views to R.java dynamically with String resources?

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);
}
}
}
}

Android : how to get Views by TAGs?

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);
}

Multiple radiobutton selecion issue- dynamic radiobutton creation

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);

Get all the selected radio button and check box value created dyamically?

I want to save all the selected answer(checkbox and radiobutton) and its corresponding question which is set in dynamic Textview in sqlite db ,
but the problem is when i am clicking "save button" only last selected
radio button value is saved to sqlite , not all selected value and not able to get all selected check box value also .
Please help me .
save= (Button) findViewById(R.id.save);
save.setOnClickListener(this);
ll = (LinearLayout) findViewById(R.id.linearLayout1);
for (int j = 0; j < Questions.length; j++) {
tv = new TextView(this);
tv.setId(j);
tv.setText(Questions[j].getQuestionNo() + "."
+ Questions[j].getQuestion());
ll.addView(tv);
Answer[] answer = Questions[j].getAnswer();
if (Questions[j].getMultipleChoice().equalsIgnoreCase("false")) {
rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
RadioButton[] rb = new RadioButton[answer.length];
for (int i = 0; i < answer.length; i++)
{
// add radio buttons
rb[i] = new RadioButton(this);
rb[i].setText(answer[i].getLabel());
int id = Integer.parseInt(j + "" + i);
rb[i].setId(id);
rg.addView(rb[i]);
}
ll.addView(rg);
else
{
// add checkboxes
for (int i = 0; i < answer.length; i++) {
cb = new CheckBox(this);
cb.setText(answer[i].getLabel());
int id = Integer.parseInt(j + "" + i);
cb.setId(id);
ll.addView(cb);
}
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
if (rg.getCheckedRadioButtonId() != -1) {
int id = rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String answer = (String) btn.getText();
//only last selected radio button value is coming
}
// not able to get all the selected checkbox value
break;
default:
break;
}
}
I don't know android.But the issue arise me in struts2.I solved that issue by generating dynamic checkbox like below.
<s:iterator value="samplelist">
<input type="checkbox" value="true" style="margin-top:2px;" name="p_<s:property value='profileId'/>
</s:iterator>
so i have found out if it is checked or not in java code by using the for loop
for(int i=0;i<samplelist.size();i++)
request.getParameter("P_something");

Categories

Resources