Hi. You are creating a mobile android app. In next messages I'll give you a files, that…
- User
Hi. You are creating a mobile android app. In next messages I'll give you a files, that you already wrote. You have to end development of this app by completing my task. Untill a stop-phrase "That is all" I'll give you a files. Answer "Yes" if you understood.
- Assistant
Yes.
- User
app\src\main\java\com\example\mb7\presentation\main\MainActivity.kt package com.example.mb7.presentation.main
import android.content.Intent import android.net.Uri import android.os.Bundle import android.provider.MediaStore import android.widget.Button import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.mb7.R import java.io.File
- Assistant
Got it.
- User
app\src\main\java\com\example\mb7\presentation\main\PhotoAdapter.java package com.example.mb7.presentation.main;
import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;
import com.example.mb7.R;
import java.io.File; import java.util.List;
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {
private final List<File> mPhotoList; public PhotoAdapter(List<File> photoList) { mPhotoList = photoList; } public static class PhotoViewHolder extends RecyclerView.ViewHolder { public ImageView photoImageView; public TextView photoFilenameTextView; public CheckBox photoCheckbox; public PhotoViewHolder(@NonNull View itemView) { super(itemView); photoImageView = itemView.findViewById(R.id.photo_image_view); photoFilenameTextView = itemView.findViewById(R.id.photo_filename_text_view); photoCheckbox = itemView.findViewById(R.id.photo_checkbox); } } @NonNull @Override public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false); return new PhotoViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) { File photoFile = mPhotoList.get(position); holder.photoImageView.setImageURI(Uri.fromFile(photoFile)); holder.photoFilenameTextView.setText(photoFile.getName()); holder.photoCheckbox.setChecked(false); // or true based on item selection } @Override public int getItemCount() { return mPhotoList.size(); } - Assistant
Got it.
- User
app\src\main\res\layout\activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".presentation.main.MainActivity">
<!-- Textview for the header --> <TextView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose Photos" android:textSize="24sp" android:textStyle="bold" android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <!-- Container for the chosen photos --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/photo_list" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/header" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@+id/select_photos_button"/> <!-- Button for selecting photos from the gallery --> <Button android:id="@+id/select_photos_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Select Photos" android:textColor="@android:color/white" android:background="@color/white" android:layout_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/photo_list" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> - Assistant
Got it.
- User
app\src\main\res\layout\item_photo.xml
<!-- Imageview for displaying the photo --> <ImageView android:id="@+id/photo_image_view" android:layout_width="64dp" android:layout_height="64dp" android:layout_gravity="center_vertical" android:scaleType="centerCrop"/> <!-- Textview for displaying the filename of the photo --> <TextView android:id="@+id/photo_filename_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:textColor="@android:color/black" android:textSize="18sp" android:layout_marginStart="16dp"/> <!-- Checkbox for selecting the photo item --> <CheckBox android:id="@+id/photo_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> - Assistant
Got it.
- User
That is all
- Assistant
Alright, let me know if you need further assistance.
- User
Now, make following: when photo is choosed, they should be drawn at screen.
- Assistant
Alright, to display the selected photos on the screen, you need to make the following changes:
-
In the
MainActivityclass, modify thephotoListvariable to be a mutableArrayListinstead of aMutableList. Changeprivate lateinit var photoList: MutableList<File>to
private lateinit var photoList: ArrayList<File> -
In the
onCreatemethod of theMainActivityclass, initializephotoListas an emptyArrayList.photoList = ArrayList()
-
- User
This and all next time you should send me the whole code. Send me changed files
- Assistant
- User
I was mistaken. Next time send me only changes, not all files.
- Assistant
Alright, I'll make sure to only send the modified code in the future. Let me know if you need any further assistance.
- User
This code doesn't draw a photos, it is draw a placeholed instead. This code should draw real choosen images. Fix it
- Assistant
My apologies for the confusion. To fix the issue and display the selected photos correctly, please update the
PhotoAdapterclass as follows:class PhotoAdapter(private val photoList: ArrayList<File>) : RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() { class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val photoImageView: ImageView = itemView.findViewById(R.id.photo_image_view) val photoFilenameTextView: TextView = itemView.findViewById(R.id.photo_filename_text_view) val photoCheckbox: CheckBox = itemView.findViewById(R.id.photo_checkbox) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent, false) return PhotoViewHolder(itemView) } override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) { val photoFile = photoList[position] Glide.with(holder.itemView.context) .load(Uri.fromFile(photoFile)) .centerCrop() .placeholder(R.drawable.ic_image) .into(holder.photoImageView) holder.photoFilenameTextView.text = photoFile.name holder.photoCheckbox.isChecked = false } override fun getItemCount(): Int { return photoList.size } } - User
It is still doesn't show photos
- Assistant
My apologies, it seems that the
Uri.fromFilemethod has been deprecated, so the syntax for loading image files has changed. Instead of:Glide.with(holder.itemView.context) .load(Uri.fromFile(photoFile)) .centerCrop() .placeholder(R.drawable.ic_image) .into(holder.photoImageView)You can use the following syntax:
Glide.with(holder.itemView.context) .load(photoFile) .centerCrop() .placeholder(R.drawable.ic_image) .into(holder.photoImageView)