I'm trying to add new function, that if you click and hold an item from a list a window will pop up asking if you are sure you want to delete this specific item. Meaning if I click and hold on the first item on the list, a dialog will pop us asking if I want to delete it. And clicking yes will result in doing so.
What is the best way to achieve that?
To display files I'm currently using an RecyclerView and CardView.
Code and picture below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:foreground="?android:attr/selectableItemBackground"
android:transitionName="open_mediaplayer"
app:cardCornerRadius="4dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center_vertical"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:id="@+id/imageView"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:id="@+id/file_name_text"
android:text="Audio File_2244322"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12sp"
android:fontFamily="sans-serif-condensed"
android:layout_marginTop="7dp"
android:id="@+id/file_length_text"
android:text="00:44"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/file_time_added"
android:textSize="12sp"
android:text="30-12-2019"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is how it looks like
I would use a View.OnLongClickListener
on your item in RecyclerView. When the listener is triggered, display an alert asking if the user really wants to delete the item. If yes, delete the item wherever it's necessary (at least in the dataset) and update the RecyclerView using adapter.notifyItemRemoved(indexOfDeletedElement)