1

With the following code I am able to successfully remove a View from a LinearLayout when clicking for a longtime on a button:

button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                linearlayout.removeView(view);
                return false;
            }
        });

And the activity it's updated instantly.
However when I try to achieve the same thing (removing that same View from the same LinearLayout) by the negative button of an AlertDialog, it only works when I close the app (basically when the activity is restarted). I don't know why it doesn't update the activity instantly.

Code:

.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        System.out.println("test");
                        ll.removeView(view);
                        //ll.invalidate();
                        //dialog.dismiss();
                        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                              //stuff
                            }
                        }, 1000);
                    }
                });
Jade Kush
  • 23
  • 5

1 Answers1

0

It should do the job as longclicked , I had try both work the same ways here the code :

XML File:

<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:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="18dp"
    android:text="Hello world!" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

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

</LinearLayout>

Here the activity class's code :

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    LinearLayout layout = findViewById(R.id.linearLayout);
    TextView subView = findViewById(R.id.anotherTextView);


    findViewById(R.id.textView).setOnClickListener(view -> {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //Uncomment the below code to Set the message and title from the strings.xml file
        builder.setMessage("This is a message of testing").setTitle("title");

        //Setting message manually and performing action on button click
        builder.setMessage("Do you want to close this application ?")
                .setCancelable(true)
                .setPositiveButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                })
                .setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        layout.removeView(subView);
                    }
                });
        //Creating dialog box
        AlertDialog alert = builder.create();
        //Setting the title manually
        alert.setTitle("AlertDialogExample");
        alert.show();
    });
}

Here My Dialog Import Package:

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

It work under my case.

Nrohpos
  • 283
  • 2
  • 7