Add & Delete View from Layout

Add & delete view from Layout

I've done it like so:

((ViewManager)entry.getParent()).removeView(entry);

Add and Remove Views in Android Dynamically?

ViewParents in general can't remove views, but ViewGroups can. You need to cast your parent to a ViewGroup (if it is a ViewGroup) to accomplish what you want.

For example:

View namebar = View.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);

Note that all Layouts are ViewGroups.

Remove certain views of layout with loop

As per your use-case you can add a containerLayout in your layout and add all dynamic view to that container and when you want to remove the dynamic view's just do below.

public void AddViewToContainer(View view){

ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);

layout.addView(view);

}

public void RemoveViews() {

ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);

layout.removeAllViews();

}

And Second option is do removeAllView() on your layout and add your editText and Fab button again,

But this should not be preferred as currently you have only two View to retain but if the list grows that is too much overhead for the simple operation so better go with the first solution which is easy to manage and manipulate also.

Thanks.

How to dynamically add and delete view in android

  1. Use a ListView for displaying the list of patterns
  2. Create a custom layout for each list item. e.g.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="555*" />
    <TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="matched 5 " />
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X" />
    </LinearLayout>
  3. Create a Custom Adapter class extending BaseAdapter

    • It can maintain a list for the patterns to be shown
    • In the getView method of the custom adapter -
      • inflate the xml
      • set the information (like pattern and number of matches) based on the index parameter, using the list
      • set onclick listener for the button (delete that item from list and call notifyDatasetInvalidated())
      • return the view.
  4. On "Add Number" add item to the list in the adapter

Add & delete item from LinearLayout using ArrayAdapter

Inside your for loop, write these line of code:

for (ik = 0; ik < adapterCount; ik++) {

final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);

item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}

Hope it will help you out.

How to remove dynamically created views on button click

Try following code.

button.setOnClickListener(new View.OnClickListener() {      
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});

Explanation

  • Here first take "GrandParent" of any view.
  • Then take its "Parent" view
  • With reference to "GrandParent" remove that "Parent" view.
  • this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.

removing a view from the activity

I've recreated your problem and i don't see any errors in presented code. From what i understand you should have something similar to this:

public class MainActivity extends AppCompatActivity {

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

Button buttonadd = (Button) findViewById(R.id.addButton);

buttonadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddNewView(view);
}
});
}

public void AddNewView(View view) {
Intent intent = new Intent(this, DisplayNewView.class);
startActivity(intent);
}
}


public class DisplayNewView extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);

LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.activity_display_view, null);
TextView tv = (TextView) view.findViewById(R.id.myViewLabel);
tv.setTextSize(40);

Button buttondelete = (Button) view.findViewById(R.id.deleteButton);
buttondelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeView(view);
}
});

addContentView(view, tlp);

}

public void removeView(View view) {
ViewGroup vg = (ViewGroup) (view.getParent());
vg.removeView(view);
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stackquest.MainActivity">

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add">
</Button>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_new_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stackquest.DisplayNewView">

<TextView
android:id="@+id/myViewLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete">
</Button>
</LinearLayout>

Clicking on delete button should delete the button view. If you wish to delete all views you can use vg.removeAllViews(); and as others have mentioned why create new intent for dynamically manipulating views?

How to add and remove CardView from LinearLayout on button click in android studio?

The parameter v in onDelete(View v) is not the CardView that you want to be removed.
It's the ImageView that you click.

In onAddField() do this:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());

what the above code does is store the new CardView object in delRowBtn's tag.
Also I removed -1 from parentRelativeLayout.getChildCount() to add the new CardView at the end.

But the above logic must be applied also to the 1st CardView that is already there, so in onCreate() add this:

CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);


In onDelete() do this:

CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);

what the above code does is retrieves the CardView object from delRowBtn's tag and removes it from parentRelativeLayout.

The full code of your Activity:

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private ViewGroup.LayoutParams params;
private int count = 1;

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

CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();

initViews();
initListeners();
}

private void initListeners() {

}

private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);

parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
}

public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
count++;
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if (count == 1) return;
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
count--;

}catch (Exception e){
e.printStackTrace();
}
}
}


Related Topics



Leave a reply



Submit