Step 1: Create a New Project
Read from the website: https://codester.com.bd/archives/432
Step 2: Add dependency to build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
এবার সিঙ্ক অপশন আসবে উপরের ডান কোণায় সিঙ্ক নাও অপশনে ক্লিক করুন।
Step 3: Add permission to the internet in your AndroidManifest.xml file
Add below two lines inside your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
- XML
<!--PDF Viewer to display our PDF--> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/idPDFView" android:layout_width="match_parent" android:layout_height="match_parent" />
Step 5: Working with the MainActivity.java file
Navigate to the app > java > your apps package name > MainActivity.java file. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
- Java
PDFView pdfView; // url of our PDF file. String pdfurl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
// initializing our pdf view.
pdfView = findViewById(R.id.idPDFView);
new RetrivePDFfromUrl().execute(pdfurl);
} // create an async task class for loading pdf file from URL.
class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
// we are using inputstream
// for getting out PDF.
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
// below is the step where we are
// creating our connection.
HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
// response is success.
// we are getting input stream from url
// and storing it in our variable.
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
// this is the method
// to handle errors.
e.printStackTrace();
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
// after the execution of our async
// task we are loading our pdf in our pdf view.
pdfView.fromStream(inputStream)
.enableSwipe(true)
.swipeHorizontal(true)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true)
.spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.load();
}}অ্যাপ এর ভিতরে মাল্টিপল পিডিএফ, অনক্লিক লিসেনার । মাধ্যমে যদি ইউজারকে সিলেট করার সুযোগ দেন। তাহলে দয়াকরে স্টেপ গুলো পূরণ করুন । আপনি লিস্ট করে দিবেন,ইউজার দেখবে যেটা পছন্দ হবে ক্লিক করবেন,এবং ওই পিডিএফ অ্যাক্টিভিটি দিয়ে ভিউ দেখতে পাবে।
Step 1: Create a New Activity And Design Layout
Step 2: Create a Design (\app\src\main\res\layout\Home.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_margin="10dp"
android:textStyle="bold"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PDF Book 1" />
<Button
android:id="@+id/button2"
android:layout_margin="10dp"
android:textStyle="bold"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PDF Book 2 " />
</LinearLayout>
Step 3: Programming Code (\app\src\main\java\com\App\Home.java)
public class
private Button button1,button2;
// initializing our button view.
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(home.this,MainActivity.class);
intent.putExtra("inputStream","https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(home.this,MainActivity.class);
intent.putExtra("inputStream","https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
startActivity(intent);
}
});পিডিএফ ভিউ করার জন্য একটা অ্যাক্টিভিটি তৈরি করুন এবং সেখানে এই প্রোগ্রামটি লিখুন। পিডিএফ ভিউয়ার করার জন্য একটা অ্যাক্টিভিটি তৈরি করুন ভিউয়ার। সেখানে এই প্রোগ্রামটি দেখে দেখে টাইপ করুন। ইউজার ক্লিক করার সাথে সাথে তাকে ডাইরেক্ট করে ভিউ অ্যাকটিভিটিতে নিয়ে আসবে এবং সিলেক্ট করা লিংকে অটোমেটিক পিডিএফ লোড নিবে।
Step 4: Create a Design (\app\src\main\res\layout\PDFViewer.xml)
<!--PDF Viewer to display our PDF-->
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/idPDFView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 5: Programming Code (\app\src\main\java\com\App\PDFViewer.java)
public class
// for PDF view. PDFView pdfView;
// initializing Received selected link.
Intent intent=getIntent();
String pdfurl= getIntent().getStringExtra("inputStream");
/// End
// initializing our pdf view.
pdfView = findViewById(R.id.idPDFView);
new RetrivePDFfromUrl().execute(pdfurl);
/// End
}
// create an async task class for loading pdf file from URL.
class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
// we are using inputstream
// for getting out PDF.
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
// below is the step where we are
// creating our connection.
HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
// response is success.
// we are getting input stream from url
// and storing it in our variable.
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
// this is the method
// to handle errors.
e.printStackTrace();
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
// after the execution of our async
// task we are loading our pdf in our pdf view.
pdfView.fromStream(inputStream)
.enableSwipe(true)
.swipeHorizontal(true)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true)
.spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.load();}