How to create PDF reader app in android studio
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pdfreader">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DocumentActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recylerview">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
activity_document.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".DocumentActivity">
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pdfview"/>
</RelativeLayout>
element_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:id="@+id/cardview"
android:layout_marginStart="8dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/pdfimage"
android:id="@+id/imgpdf"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/textpdfname"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:padding="5dp"
android:textColor="#000000"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="Hello EES squad">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
MainActivity
package com.example.pdfreader;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnPdfFileSelectListener {
private PdfAdapter pdfAdapter;
private List<File> pdfList;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runtimePermission();
}
private void runtimePermission() {
Dexter.withContext(MainActivity.this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
displayPdf();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(MainActivity.this, "Permission is Required", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
public ArrayList<File> findPdf(File file){
ArrayList<File> arrayList=new ArrayList<>();
File[] files=file.listFiles();
for( File singleFile : files){
if(singleFile.isDirectory()&& !singleFile.isHidden()){
arrayList.addAll(findPdf(singleFile));
}
else {
if(singleFile.getName().endsWith(".pdf")){
arrayList.add(singleFile);
}
}
}
return arrayList;
}
private void displayPdf() {
RecyclerView recyclerView=findViewById(R.id.recylerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
pdfList=new ArrayList<>();
pdfList.addAll(findPdf(Environment.getExternalStorageDirectory()));
pdfAdapter=new PdfAdapter(this,pdfList,this);
recyclerView.setAdapter(pdfAdapter);
}
@Override
public void onPdfFileSelected(File file) {
startActivity(new Intent(MainActivity.this,DocumentActivity.class)
.putExtra("path",file.getAbsolutePath()));
}
}
PdfAdapter
package com.example.pdfreader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.util.List;
public class PdfAdapter extends RecyclerView.Adapter<PdfViewHolder> {
private Context context;
private List<File> pdffiles;
private OnPdfFileSelectListener listener;
public PdfAdapter(Context context, List<File> pdffiles, OnPdfFileSelectListener listener) {
this.context = context;
this.pdffiles = pdffiles;
this.listener=listener;
}
@NonNull
@Override
public PdfViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PdfViewHolder(LayoutInflater.from(context).inflate(R.layout.element_holder,parent,false));
}
@Override
public void onBindViewHolder(@NonNull PdfViewHolder holder, final int position) {
holder.tvName.setText(pdffiles.get(position).getName());
holder.tvName.setSelected(true);
holder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onPdfFileSelected(pdffiles.get(position));
}
});
}
@Override
public int getItemCount() {
return pdffiles.size();
}
}
PdfViewHolder
package com.example.pdfreader;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
public class PdfViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public CardView container;
public PdfViewHolder(@NonNull View itemView) {
super(itemView);
tvName=itemView.findViewById(R.id.textpdfname);
container=itemView.findViewById(R.id.cardview);
}
}
DocumentActivity
package com.example.pdfreader;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.File;
public class DocumentActivity extends AppCompatActivity {
String filepath="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_document);
PDFView pdfView=findViewById(R.id.pdfview);
filepath=getIntent().getStringExtra("path");
File file=new File(filepath);
Uri path=Uri.fromFile(file);
pdfView.fromUri(path).load();
}
}
Dependencies
implementation('com.karumi:dexter:6.2.2')
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Image Download https://www.mediafire.com/file/eabxitf31aom3dg/Pdf-2127829.png/file
Video Link-https://youtu.be/3GEjRteP1yg
Comments
Post a Comment