how to create a book app in android studio

 How to make a story app with pdf file.


Step -1 : Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step -2 : Add the following code to res ⇒ layout ⇒ activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>

<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=".MainActivity">

 

   <ListView

       android:id="@+id/list_view"

       android:layout_width="match_parent"

       android:layout_height="match_parent"/>

 

</androidx.constraintlayout.widget.ConstraintLayout>

In the above code, we have taken a List view. When users click on the list, they can see the pdf file.

Step -3 : Add the following code to res ⇒ values ⇒ strings.xml.

<resources>

    <string-array name="Stories_name">

        <item>story 1</item>

        <item>story 2</item>

        <item>story 3</item>

    </string-array>

</resources>

In the above code, we have stored title of the story.

Step -4 : Add the following code to MainActivity.java.

import androidx.appcompat.app.AppCompatActivity;

 

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

public class MainActivity extends AppCompatActivity {

    ListView listView;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView=findViewById(R.id.list_view);

 

        String[] story_name=getResources().getStringArray(R.array.Stories_name);

 

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,story_name);

        listView.setAdapter(adapter);

 

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 

                Intent intent=new Intent(MainActivity.this,Main2Activity.class);

                intent.putExtra("key_position",position);

                startActivity(intent);

            }

        });

    }

}

Step -5 : Create an assets folder so right-click on app ⇒ New folder ⇒ asset folder ⇒ leave as it is and click on Finish.

Next right click on assets and paste all your pdf file-

·       story1.pdf

·       story2.pdf

·       story3.pdf

Step -6 : Add to build.gradle(Module app) and sync the project.

implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

Step -7 : Add the following code ⇒ Right-click on java ⇒ new ⇒ Activity ⇒ Empty Activity
Leave as it is and click on Finish.

Let assume you newly created Activity_main2.xml and Main2Activity.java.

Step -8 : Now add the following code to Activity_main2.xml.

<?xml version="1.0" encoding="utf-8"?>

<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=".Main2Activity">

 

    <com.github.barteksc.pdfviewer.PDFView

        android:id="@+id/pdfView"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Step -9 : Now add the following code to Main2Activity.java.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.github.barteksc.pdfviewer.PDFView;

 

public class Main2Activity extends AppCompatActivity {

    PDFView pdfView;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

 

        pdfView=findViewById(R.id.pdfView);

 

       int story_position=getIntent().getIntExtra("key_position",0);

 

       if (story_position==0){

           pdfView.fromAsset("story1.pdf").load();

       }

 

        else if (story_position==1){

            pdfView.fromAsset("story2.pdf").load();

        }

 

        else if (story_position==2){

            pdfView.fromAsset("story3.pdf").load();

        }

    }

}

Now Run and test your app.

 

Also Read More Tutorials

Create Age Calculator App In Android Studio

Crate Story App With Html File In Android Studio

Post a Comment

0 Comments