Recently Added

6/recent/ticker-posts

How to implement method in Recyclerview like Rename, Delete, Share and Show Properties in Android Studio Part 6

In this Part 6 I am going to create a method and with the help of this method we can rename, delete, share and show properties in recycler view 

So let's Start. 


rename_layout.xml

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:orientation="vertical">


    <EditText
        android:id="@+id/rename_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="thor2 "
        android:textColor="@color/black" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/cancel_rename_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?attr/selectableItemBackground"
            android:text="Cancel"
            android:textAllCaps="false"
            tools:targetApi="m"
            android:background="@android:color/transparent"
            android:textColor="@color/black"/>

        <Button
            android:background="@android:color/transparent"
            android:id="@+id/rename_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?attr/selectableItemBackground"
            android:text="Rename"
            android:textAllCaps="false"
            tools:targetApi="m"
            android:textColor="@color/black"/>

    </LinearLayout>

</LinearLayout>


file_properties.xml

<?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"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/pro_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:gravity="start"
            android:text="title"
            android:textAlignment="textStart"
            android:textColor="@color/black" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Path"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/pro_storage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:gravity="start"
            android:text="storage/0/emulated/videos"
            android:textAlignment="textStart"
            android:textColor="@color/black" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Size"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/pro_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:gravity="start"
            android:text="1 Gb"
            android:textAlignment="textStart"
            android:textColor="@color/black" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Duration"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/pro_duration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:gravity="start"
            android:text="00:00:00"
            android:textAlignment="textStart"
            android:textColor="@color/black" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Resolution"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/pro_resolution"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:gravity="start"
            android:text="135x654"
            android:textAlignment="textStart"
            android:textColor="@color/black" />


    </LinearLayout>

</LinearLayout>


Now come into the VideoAdapter.class and past the method

sharefiles(position);
private void shareFile(int p){
    Uri uri = Uri.parse(videoFolder.get(p).getPath());
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("video/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    context.startActivity(Intent.createChooser(intent,"share"));
    Toast.makeText(context, "loading.."Toast.LENGTH_SHORT).show();
}


deleteFiles(position,v);
private void deleteFiles(int p, View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("delete")
            .setMessage(videoFolder.get(p).getTitle())
            .setNegativeButton("cancel"new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //todo
                    // leave it as empty
                }
            }).setPositiveButton("ok"new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Uri contentUri = ContentUris.withAppendedId(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    Long.parseLong(videoFolder.get(p).getId()));
            File file = new File(videoFolder.get(p).getPath());
            boolean deleted = file.delete();
            if (deleted){
                context.getApplicationContext().getContentResolver()
                .delete(contentUri,
                        nullnull);
                videoFolder.remove(p);
                notifyItemRemoved(p);
                notifyItemRangeChanged(p,videoFolder.size());
                Snackbar.make(view,"File Deleted Success"
                Snackbar.LENGTH_SHORT).show();
            }else {
                Snackbar.make(view,"File Deleted Fail",
                 Snackbar.LENGTH_SHORT).show();
            }
        }
    }).show();
}





renameFiles(position,v);
private void renameFiles(int position, View view){
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.rename_layout);
    final EditText editText = dialog.findViewById(R.id.rename_edit_text);
    Button cancel = dialog.findViewById(R.id.cancel_rename_button);
    Button rename_btn = dialog.findViewById(R.id.rename_button);
    final File renameFile = new File(videoFolder.get(position).getPath());
    String nameText = renameFile.getName();
    nameText = nameText.substring(0nameText.lastIndexOf("."));
    editText.setText(nameText);
    editText.clearFocus();
    dialog.getWindow().setSoftInputMode(WindowManager.
    LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    cancel.setOnClickListener(v -> {
        dialog.dismiss();
    });
    rename_btn.setOnClickListener(v1->{
        String onlyPath = renameFile.getParentFile().getAbsolutePath();
        String ext = renameFile.getAbsolutePath();
        ext = ext.substring(ext.lastIndexOf("."));
        String newPath = onlyPath + "/" + editText.getText() + ext;
        File newFile = new File(newPath);
        boolean rename = renameFile.renameTo(newFile);
        if (rename){
            context.getApplicationContext().getContentResolver().
                    delete(MediaStore.Files.getContentUri("external"),
                            MediaStore.MediaColumns.DATA + "=?",
                            new String[]{renameFile.getAbsolutePath()});
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(Uri.fromFile(newFile));
            context.getApplicationContext().sendBroadcast(intent);
            Snackbar.make(view,"Rename Success"Snackbar.LENGTH_SHORT).show();
        }else {
            Snackbar.make(view,"Rename Failed"Snackbar.LENGTH_SHORT).show();
        }
        dialog.dismiss();
    });
    dialog.show();
}





showProperties(position);
private void showProperties(int p){
    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.file_properties);

    String name = videoFolder.get(p).getTitle();
    String path = videoFolder.get(p).getPath();
    String size = videoFolder.get(p).getSize();
    String duration = videoFolder.get(p).getDuration();
    String resolution = videoFolder.get(p).getResolution();

    TextView tit = dialog.findViewById(R.id.pro_title);
    TextView st = dialog.findViewById(R.id.pro_storage);
    TextView siz = dialog.findViewById(R.id.pro_size);
    TextView dur = dialog.findViewById(R.id.pro_duration);
    TextView res = dialog.findViewById(R.id.pro_resolution);

    tit.setText(name);
    st.setText(path);
    siz.setText(size);
    dur.setText(duration);
    res.setText(resolution+"p");

    dialog.show();

}




Watch the full video on Youtube : )


Post a Comment

1 Comments

  1. Hi,the fonction rename and delete dosen't work for me, and i watched the video and the code. Each time, the boolean is false idk why

    ReplyDelete