
Create PDF Reader App in Android Studio
Hello guys, in this post I came up with an easy solution for creating PDF reader application for Android device using Android Studio. PDF Reader is used to reading PDF documents, the extension (file format) for PDF documents is .pdf and 70% documents are developed as PDF document this increases the use of PDF reader App. So developing Android Application for PDF files will be beneficial for those developers who like to get more downloads for their App in Google Play Store.
Actually, I too added PDF reader feature in one of my Android App, you can download that app from play store BuzzMyCode and see how in real time the App works with PDF feature. The reason why I embedded functionality to read (Display) PDF within Syncsas App is to increase engagement of user significantly. In this tutorial you will learn how to make simple PDF Reader application in Android, this can be improved by adding more controls.
Step.1# Create a new Android Project in Android Studio with name SyncsasPDFreader or you can assign any name to Project.
Step.2# Select Minimum SDK in Phone and Tablet as API 19: Android 4.4 (KitKat) as the lower version is used by few people and the main reason is that if we create App for lower version then it become more complicated with coding. So for basic app use API 19 and if you like to develop for lower version then please contact me.
Step.3# Now select the Empty activity, you can select any other activity if you know how to use it in coding.
Now you to edit two files one is present in Java folder and another one is Xml file which is in res folder (resource) if you are not able to locate this files then follow the path:
For Java file- ../app/Java/<your package name>/MainActivity.java
For Xml file- ../app/res/layout/activty_main.xml
Make sure you have a pdf file and copy that pdf file in assets folder, which can be created under res folder.
Step.4# Now edit MainActivity.java with the following code:
public class MainActivity extends AppCompatActivity { privateImageViewimg; privateintcurrentpage = 0; private Button next, previous; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); next = (Button) findViewById(R.id.next); previous = (Button) findViewById(R.id.previous); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentpage++; render(); } }); previous.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentpage--; render(); } }); } private void render() { try { img = (ImageView) findViewById(R.id.image); int width = img.getWidth(); int height = img.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); File file = new File("/sdcard/downloads/Example.pdf"); //Add your own PDF file path which you Uploaded in assets folder. PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)); if(currentpage<0){ currentpage =0; }else if(currentpage>renderer.getPageCount()){ currentpage = renderer.getPageCount() -1; Matrix matrix = img.getImageMatrix(); Rectrect = new Rect(0,0, width , height); renderer.openPage(currentpage).render(bitmap,rect,matrix , PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY ); img.setImageMatrix(matrix); img.setImageBitmap(bitmap); img.invalidate(); } } catch (Exception e) { e.printStackTrace(); } } }
Step.5# And in activity_main.xml file add the following the code
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/image" android:scaleType="fitCenter" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/previous" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="<<" /> <Button android:id="@+id/next" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text=">>" /> </LinearLayout> </LinearLayout>
Step.6# Finally in AndroidManifest.xml file which can be easily located in the manifest folder under app root folder, add the following line of code above opening application tag.
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
Here we had completed with our PDF Reader app, for detailed information regarding this tutorial you can ask in comment box. Keep sharing our tutorial with Android Developers.