Reputation: 7092
In my main activity java code file, I'm adding a new view with this method:
public void AddNewView(View view) {
Intent intent = new Intent(this, DisplayNewView.class);
startActivity(intent);
}
This uses a class called DisplayNewView which has this method:
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);
addContentView(view,tlp);
}
So this sucessfully adds the view.
But now, in the same xml resource file that this view uses, I have a button that is supposed to remove this view. This button is also in the same DisplayNewView java code file.
It looks like this:
public void removeView(View view) {
ViewGroup vg = (ViewGroup)(view.getParent());
vg.removeView(view);
}
But when I click on the button, nothing happens. I also don't see any errors in the debugger console in Android Studio.
From my web searches, it looks like I'm trying to remove the view in the right way.
Is there anything I'm missing?
Thanks!
Upvotes: 1
Views: 3889
Reputation: 367
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?
Upvotes: 2