Showing posts with label remove. Show all posts
Showing posts with label remove. Show all posts

Add and Remove view dynamically keep track of child views

| 0 comments |
I have a old example of "Add and Remove view dynamically". Somebody ask how the Remove button keep track of the views. So I modify the example to explain in more details.


In this example, when user click on the Add button, it will create a child View, addView. The addView have a Button, buttonRemove. The buttonRemove have its own OnClickListener object. The OnClickListener object refer to the specified addView when it is created.

That means the OnClickListener object of the buttonRemove in the first child view("abc") not the same object in the second child view("abcdef"), not the same object in the third child view("abcdefghi")...All have its own individual OnClickListener object. And each OnClickListener object refer to a specified addView object. Such that it can keep track of the child Views, addView.


MainActivity.java
package com.blogspot.android_er.androiddynamicview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;

public class 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(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
reList = (TextView)findViewById(R.id.relist);
info = (TextView)findViewById(R.id.info);
info.setMovementMethod(new ScrollingMovementMethod());

buttonAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);

final View.OnClickListener thisListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
info.append("thisListener called: " + this + " ");
info.append("Remove addView: " + addView + " ");
((LinearLayout)addView.getParent()).removeView(addView);

listAllAddView();
}
};

buttonRemove.setOnClickListener(thisListener);
container.addView(addView);

info.append(
"thisListener: " + thisListener + " "
+ "addView: " + addView + " "
);

listAllAddView();
}
});
}

private void listAllAddView(){
reList.setText("");

int childCount = container.getChildCount();
for(int i=0; i<childCount; i++){
View thisChild = container.getChildAt(i);
reList.append(thisChild + " ");
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="horizontal"
android_padding="16dp"
tools_context="com.blogspot.android_er.androiddynamicview.MainActivity">


<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<RelativeLayout
android_layout_width="match_parent"
android_layout_height="wrap_content">

<Button
android_id="@+id/add"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_alignParentRight="true"
android_text="Add" />

<EditText
android_id="@+id/textin"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_alignParentLeft="true"
android_layout_toLeftOf="@id/add" />
</RelativeLayout>

<LinearLayout
android_id="@+id/container"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="vertical"></LinearLayout>

</LinearLayout>

<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<TextView
android_id="@+id/relist"
android_layout_width="match_parent"
android_layout_height="0dp"
android_layout_weight="1"
android_textStyle="bold"
android_background="#E0E0E0"/>
<TextView
android_id="@+id/info"
android_layout_width="match_parent"
android_layout_height="0dp"
android_layout_weight="1"
android_textStyle="italic"
android_background="#D0D0D0"
android_gravity="bottom"/>

</LinearLayout>
</LinearLayout>


layout/row.xml, the layout of the child views.
<RelativeLayout 

android_layout_width="match_parent"
android_layout_height="wrap_content">
<Button
android_id="@+id/remove"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_alignParentRight="true"
android_text="Remove"/>
<TextView
android_id="@+id/textout"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_alignParentLeft="true"
android_layout_toLeftOf="@id/remove"/>
</RelativeLayout>


download filesDownload the files (Android Studio Format) .

Its another approach to share a common OnClickListener between all buttonRemove.

more:
- Add and Remove view dynamically, with EditText/AutoCompleteTextView inside

Read More..

Add and Remove view dynamically with EditText AutoCompleteTextView inside

| 0 comments |

My previous post a example of "Add and Remove view dynamically", It is another version to have a EditText (AutoCompleteTextView actually) inside the dynamic view.


MainActivity.java
package com.blogspot.android_er.androiddynamicview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

AutoCompleteTextView textIn;
Button buttonAdd;
LinearLayout container;
TextView reList, info;

private static final String[] NUMBER = new String[] {
"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten"
};
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, NUMBER);

textIn = (AutoCompleteTextView)findViewById(R.id.textin);
textIn.setAdapter(adapter);

buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout) findViewById(R.id.container);
reList = (TextView)findViewById(R.id.relist);
reList.setMovementMethod(new ScrollingMovementMethod());
info = (TextView)findViewById(R.id.info);
info.setMovementMethod(new ScrollingMovementMethod());

buttonAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
AutoCompleteTextView textOut = (AutoCompleteTextView)addView.findViewById(R.id.textout);
textOut.setAdapter(adapter);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);

final View.OnClickListener thisListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
info.append("thisListener called: " + this + " ");
info.append("Remove addView: " + addView + " ");
((LinearLayout)addView.getParent()).removeView(addView);

listAllAddView();
}
};

buttonRemove.setOnClickListener(thisListener);
container.addView(addView);

info.append(
"thisListener: " + thisListener + " "
+ "addView: " + addView + " "
);

listAllAddView();
}
});
}

private void listAllAddView(){
reList.setText("");

int childCount = container.getChildCount();
for(int i=0; i<childCount; i++){
View thisChild = container.getChildAt(i);
reList.append(thisChild + " ");

AutoCompleteTextView childTextView = (AutoCompleteTextView) thisChild.findViewById(R.id.textout);
String childTextViewValue = childTextView.getText().toString();
reList.append("= " + childTextViewValue + " ");
}
}

}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="horizontal"
android_padding="16dp"
tools_context="com.blogspot.android_er.androiddynamicview.MainActivity">


<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<RelativeLayout
android_layout_width="match_parent"
android_layout_height="wrap_content">

<Button
android_id="@+id/add"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_alignParentRight="true"
android_text="Add" />

<AutoCompleteTextView
android_id="@+id/textin"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_alignParentLeft="true"
android_layout_toLeftOf="@id/add" />
</RelativeLayout>

<LinearLayout
android_id="@+id/container"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_orientation="vertical"></LinearLayout>

</LinearLayout>

<LinearLayout
android_layout_width="0dp"
android_layout_height="match_parent"
android_layout_weight="1"
android_orientation="vertical">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<TextView
android_id="@+id/relist"
android_layout_width="match_parent"
android_layout_height="0dp"
android_layout_weight="1"
android_textStyle="bold"
android_background="#E0E0E0"/>
<TextView
android_id="@+id/info"
android_layout_width="match_parent"
android_layout_height="0dp"
android_layout_weight="1"
android_textStyle="italic"
android_background="#D0D0D0"
android_gravity="bottom"/>

</LinearLayout>
</LinearLayout>


layout/row.xml
<RelativeLayout 

android_layout_width="match_parent"
android_layout_height="wrap_content">
<Button
android_id="@+id/remove"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_alignParentRight="true"
android_text="Remove"/>
<AutoCompleteTextView
android_id="@+id/textout"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_alignParentLeft="true"
android_layout_toLeftOf="@id/remove"/>
</RelativeLayout>

download filesDownload the files (Android Studio Format) .

Read More..