Add In Addition To Take Sentiment Dynamically, Approximately Other Approach

Posted on

Last post exhibit a illustration of “Add too Remove thought dynamically“, each Remove Button convey its ain OnClickListener object, each OnClickListener object cite to a specified view.

It’s unopen to other approach: all Remove Buttons part a common OnClickListener object. Inside the OnClickListener, shout back the associate addView from the trigger thought past times calling its getParent() method.

package com.blogspot.android_er.androiddynamicview;  import android.content.Context; import android.os.Bundle; import; import android.text.method.ScrollingMovementMethod; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView;  populace storey MainActivity extends AppCompatActivity {      EditText textIn;     Button buttonAdd;     LinearLayout container;     TextView reList, info;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          textIn = (EditText)findViewById(;         buttonAdd = (Button)findViewById(;         container = (LinearLayout)findViewById(;         reList = (TextView)findViewById(;         information = (TextView)findViewById(;         info.setMovementMethod(new ScrollingMovementMethod());          buttonAdd.setOnClickListener(new View.OnClickListener(){             @Override             populace void onClick(View v) {                 LayoutInflater layoutInflater =                         (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);                 terminal View addView = layoutInflater.inflate(R.layout.row, null);                 TextView textOut = (TextView)addView.findViewById(;                 textOut.setText(textIn.getText().toString());                 Button buttonRemove = (Button)addView.findViewById(;                 /*                 terminal View.OnClickListener thisListener = novel View.OnClickListener(){                     @Override                     populace void onClick(View v) {                         info.append("thisListener called:\t" + this + "\n");                         info.append("Remove addView: " + addView + "\n\n");                         ((LinearLayout)addView.getParent()).removeView(addView);                          listAllAddView();                     }                 };                  buttonRemove.setOnClickListener(thisListener);                 container.addView(addView);                  info.append(                         "thisListener:\t" + thisListener + "\n"                                 + "addView:\t" + addView + "\n\n"                 );                 */                  buttonRemove.setOnClickListener(commonRemoveOnClickListener);                 container.addView(addView);                 info.append(                         "CommonRemoveOnClickListener:\t" + commonRemoveOnClickListener + "\n"                                 + "addView:\t" + addView + "\n"                                 + "buttonRemove:\t" + buttonRemove + "\n\n");                          listAllAddView();             }         });     }      soul void listAllAddView(){         reList.setText("");          int childCount = container.getChildCount();         for(int i=0; i<childCount; i++){             View thisChild = container.getChildAt(i);             reList.append(thisChild + "\n");         }     }      View.OnClickListener commonRemoveOnClickListener = novel View.OnClickListener(){         @Override         populace void onClick(View v) {             info.append("this View clicked:\t" + v + "\n");             info.append("this Listener called:\t" + this + "\n");             View thisParentView = (View) v.getParent();             info.append("Remove thisParentView: " + thisParentView + "\n\n");             ((LinearLayout)thisParentView.getParent()).removeView(thisParentView);              listAllAddView();         }     }; }  

activity_main.xml too row.xml, cite to in conclusion post “Add too Remove thought dynamically, buy the farm on rail of kid views“.