-2

I am inflating a view from the parent view .it is inflating fine.the view is adding as many time i wanted ,but when i want to delete a particular view ,it's not removing that view. while that button is working on toasts.but not on removing view.

here is mine main activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".Activity.Reminders">

<android.support.v7.widget.Toolbar
    android:id="@+id/remindertoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reminders"
        android:textSize="17sp"
        android:layout_marginStart="20dp"/>


    <ImageView
        android:id="@+id/addreminder"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginStart="190dp"
        android:layout_marginEnd="30dp"
        android:src="@drawable/add"
        tools:ignore="ContentDescription" />


</android.support.v7.widget.Toolbar>


<ImageView
    android:id="@+id/imageView10"

    android:layout_width="157dp"
    android:layout_height="69dp"
    android:layout_marginStart="230dp"
    android:layout_marginTop="8dp"
    app:srcCompat="@drawable/addreminder1" />

<ImageView
    android:id="@+id/imageView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="132dp"
    android:layout_marginTop="134dp"
    android:layout_marginEnd="123dp"

    app:srcCompat="@drawable/alarmclock"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/textView68"
    android:layout_width="wrap_content"
    android:layout_height="28dp"
    android:layout_marginStart="126dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="126dp"
    android:text="@string/no_reminders_added_yet" />

And here is mine activity which i am inflating:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:background="@drawable/shape2"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:id="@+id/reminder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reminder" />

        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="200dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/custom_mesaage"
            android:importantForAutofill="no"
            android:inputType="text"
            tools:targetApi="o" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="36sp" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/everyday" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioGroup
            android:id="@+id/group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:ignore="UselessParent">


            <RadioButton
                android:id="@+id/day"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/day" />

            <RadioButton
                android:id="@+id/month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/month" />


        </RadioGroup>


    </LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/listofmode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/listofmode2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/deleteimage"
            android:layout_width="40dp"
            android:layout_height="40dp"
           android:background="@drawable/delete"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/deletetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/delete" />


        <ImageView
            android:id="@+id/downimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginStart="220dp"
            android:src="@drawable/downn"
            tools:ignore="ContentDescription" />
        <ImageView
            android:id="@+id/upimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"
            android:src="@drawable/upp"
            tools:ignore="ContentDescription" />

    </LinearLayout>

</LinearLayout>

i want to added view to be removed from the parent but its not.i have added a toast to the button click which it is showing .

here is the code:

 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            @SuppressLint("InflateParams") final View rowView = inflater.inflate(R.layout.reminderaddition, null);

  deleteme.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                layout.removeView((View) v.getParent());
                  Toast.makeText(getApplicationContext(), "this is a textview", Toast.LENGTH_LONG).show();
                            }
                        });
                        delete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                layout.removeView((View) v.getParent());
                                Toast.makeText(getApplicationContext(), "this is a button", Toast.LENGTH_LONG).show();
                            }
                        });

i am using this tutorial:

itsandpro
  • 33
  • 8
  • what is layout in this line - layout.removeView( – NehaK Dec 20 '18 at 07:03
  • post whole activity's code for better understanding – NehaK Dec 20 '18 at 07:04
  • LinearLayout layout = findViewById(R.id.mainlayout); @NehaK – itsandpro Dec 20 '18 at 07:07
  • 1
    It's not clear which of those `setOnClickListener()` calls you're using, but the `View v` parameter is the `View` that's being clicked. Assuming that's the ` – Mike M. Dec 20 '18 at 07:25
  • @MikeM. both the setonclicklistner are supposed to delete the child view from the parent view – itsandpro Dec 20 '18 at 07:39
  • Well, you've only got one normally-clickable `View` in that layout with `delete` in the ID – the aforementioned ` – Mike M. Dec 20 '18 at 07:41
  • @MikeM.I am using a linear layout as an container here and have many views like text-views, edit-texts and switches inside that container.Here deleteme is referring to a text-view while delete is an image-view .while clicking on the both i am getting the toast response but unable to remove the view. – itsandpro Dec 20 '18 at 07:45
  • There is no `Toast` in `deleteme`'s `OnClickListener`. Please be specific with your problem descriptions. Which `TextView`? Which `ImageView`? – Mike M. Dec 20 '18 at 07:49
  • @MikeM. check now i changed the view from image to button. – itsandpro Dec 20 '18 at 07:55
  • The `View` type doesn't matter. I've already given you my suggestion. If that doesn't work, then I would suggest that you [edit] your question to provide a [mcve]. – Mike M. Dec 20 '18 at 07:58
  • which view you want to remove? write its id here – NehaK Dec 20 '18 at 08:21

2 Answers2

0

Replace layout.removeView((View) v.getParent());

With layout.removeView((View) v.getParent().getParent());

as you have 2 level hierarchy in your layout file so you have to get root linear layout which is again parent of your internal linear layout.

Mayank Sharma
  • 2,735
  • 21
  • 26
0

To remove view use following code :

layout.removeView(findViewById(VIEW_ID_YOU_WANT_TO_REMOVE));
NehaK
  • 2,639
  • 1
  • 15
  • 31