3

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:

enter image description here

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

}
ChrisB
  • 2,497
  • 2
  • 24
  • 43
Priya singh
  • 87
  • 1
  • 9
  • I think someone has already anwered your question http://stackoverflow.com/questions/3995215/add-and-remove-views-in-android-dynamically – Bethan Jan 09 '17 at 06:22

2 Answers2

2

Add this line of code :
layout.removeAllViews(); layout.invalidate(); row.removeAllViews(); row.invalidate();

Deepmala singh M
  • 379
  • 1
  • 16
0

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));
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50