How to make book app in android studio
Introduction
In this article i will show you how to make book app in android studio. You can easily make an android app. Few thing needs to make an app. First thing is to download some PDF files those are used in app then suggest icon and logo for app.
Steps
- Add some dependencies in Build.Gradule.
- Create activity (DescriptionActivity.java)
- Create 2 Classes (Constant.java) , (TitleAdapter.java)
- Create Interface (CustomItemClickListner.java)
- Create Menu (menubar.xml)
- Add some colors
- Create 2 layouts (item_show.xml), (header.xml)
- Change Style as noActionBar
- Create assets folder
- Paste PDF files in assets folder
Code
Build.Gradule
implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.github.barteksc:android-pdf-viewer:2.8.2' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:design:28.0.0'
Constant.java
public class Constant { public static final String Java_Chapter1= "Java_Chapter1"; public static final String Java_Chapter2= "Java_Chapter2"; public static final String Java_Chapter3= "Java_Chapter3"; public static final String Java_Chapter4 = "Java_Chapter4"; public static final String Java_Chapter5 = "Java_Chapter5"; }
TitleAdapter.java
import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.myViewHlder> { private Context myContext; private ArrayList<String> titlelist; private CustomItemClickListner clickListner; public TitleAdapter(Context myContext, ArrayList <String> titlelist, CustomItemClickListner clickListner) { this.myContext = myContext; this.titlelist = titlelist; this.clickListner = clickListner; } @NonNull @Override public myViewHlder onCreateViewHolder(@NonNull final ViewGroup viewGroup, int i) { View view = LayoutInflater.from(myContext).inflate(R.layout.item_show,viewGroup,false); final myViewHlder viewHlder= new myViewHlder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clickListner.onClickItem(v,viewHlder.getPosition()); } }); return viewHlder; } @Override public void onBindViewHolder(@NonNull myViewHlder myViewHlder, int i) { myViewHlder.titleText.setText(titlelist.get(i).replace("_"," ")); } @Override public int getItemCount() { return titlelist.size(); } public class myViewHlder extends RecyclerView.ViewHolder { TextView titleText; public myViewHlder(@NonNull View itemView) { super(itemView); titleText = (TextView) itemView.findViewById(R.id.bookname); } } }
CustomItemClickListner.java
import android.view.View; public interface CustomItemClickListner { public void onClickItem(View v, int position); }
MainActivity.java
import android.content.Context; import android.content.Intent; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Context mContext; ArrayList<String> titleArrayList; private RecyclerView mRecycleView; RecyclerView recyclerView; DrawerLayout drawerLayout; public Toolbar toolbar; ActionBarDrawerToggle actionBarDrawerToggle; NavigationView navigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); mContext = MainActivity.this; titleArrayList = new ArrayList<String> (); titleArrayList.add ( Constant.Java_Chapter1 ); titleArrayList.add ( Constant.Java_Chapter2 ); titleArrayList.add ( Constant.Java_Chapter3 ); titleArrayList.add ( Constant.Java_Chapter4 ); titleArrayList.add ( Constant.Java_Chapter5 ); setContentView ( R.layout.activity_main ); navigationView=( NavigationView ) findViewById ( R.id.navbar ); mRecycleView = ( RecyclerView ) findViewById ( R.id.recycler ); mRecycleView.setHasFixedSize ( true ); mRecycleView.setLayoutManager ( new LinearLayoutManager ( getApplicationContext () ) ); TitleAdapter adapter = new TitleAdapter ( mContext, titleArrayList, new CustomItemClickListner () { @Override public void onClickItem(View v, int position) { Intent deIntent = new Intent ( mContext, DescriptionActivity.class ); deIntent.putExtra ( "titles", titleArrayList.get ( position ) ); startActivity ( deIntent ); Toast.makeText ( mContext, "Clicked" + titleArrayList.get ( position ), Toast.LENGTH_SHORT ).show (); } } ); mRecycleView.setAdapter ( adapter ); navigationView.setNavigationItemSelectedListener ( new NavigationView.OnNavigationItemSelectedListener () { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId ()) { case R.id.naveHome: Toast.makeText ( MainActivity.this,"Home",Toast.LENGTH_LONG ).show (); break; case R.id.facebook: Toast.makeText ( MainActivity.this,"Facebook",Toast.LENGTH_LONG ).show (); break; case R.id.moreapp: Toast.makeText ( MainActivity.this,"More App",Toast.LENGTH_LONG ).show (); break; case R.id.shareapp: Toast.makeText ( MainActivity.this,"Share APP",Toast.LENGTH_LONG ).show (); break; case R.id.setting: Toast.makeText ( MainActivity.this,"Setting",Toast.LENGTH_LONG ).show (); break; case R.id.about: Toast.makeText ( MainActivity.this,"About",Toast.LENGTH_LONG ).show (); break; } return false; } } ); handleToolBar (); } void handleToolBar() { drawerLayout=(DrawerLayout ) findViewById ( R.id.drawerLayout ); toolbar = findViewById ( R.id.toolLayout ); setSupportActionBar ( toolbar ); actionBarDrawerToggle=new ActionBarDrawerToggle ( this,drawerLayout,toolbar,R.string.app_name,R.string.app_name); drawerLayout.addDrawerListener ( actionBarDrawerToggle ); actionBarDrawerToggle.syncState (); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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" android:fitsSystemWindows="true" android:id="@+id/drawerLayout" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolLayout" android:layout_width="match_parent" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:background="@color/colorPrimary" android:layout_height="wrap_content"> </android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView android:layout_weight="1" android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> <android.support.design.widget.NavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:id="@+id/navbar" app:menu="@menu/menubar" app:headerLayout="@layout/header" > </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
Create Menu (menubar.xml)
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single" android:id="@+id/group1"> <item android:title="Home" android:id="@+id/naveHome" /> <item android:title="Facebook" android:id="@+id/facebook" /> <item android:title="More App" android:id="@+id/moreapp"/> <item android:title="Share App" android:id="@+id/shareapp" /> </group> <group android:checkableBehavior="single" android:id="@+id/group2" > <item android:title="Setting" android:id="@+id/setting"/> <item android:id="@+id/about" android:title="About" /> </group> </menu>
Style.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
item_show.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:padding="12dp" android:layout_marginTop="22dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" /> <LinearLayout android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:padding="8dp" android:id="@+id/bookname" android:textSize="22dp" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="How to learn Android" android:textColor="@android:color/holo_blue_dark" android:layout_marginLeft="12dp" /> </LinearLayout> </LinearLayout> </LinearLayout>
header.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="172dp" android:background="@color/backgroud" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:orientation="vertical" android:padding="14dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Niazi Developer" android:textColor="@android:color/white" android:textSize="18dp" /> </LinearLayout>
DescriptionActivity.java
import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.webkit.WebView; import com.github.barteksc.pdfviewer.PDFView; public class DescriptionActivity extends AppCompatActivity { private static final String TAG = "DescriptionActivity"; private Context mContext; private PDFView pdfView; private Bundle extras; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_description); mContext = DescriptionActivity.this; pdfView= ( PDFView ) findViewById(R.id.pdfView); extras= getIntent().getExtras(); if(!extras.equals(null)) { String data = extras.getString("titles"); Log.d(TAG, "onCreate: The coming data is"+data ); String url = data+".pdf"; pdfView.fromAsset (url).load (); }} }
activity_description.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=".DescriptionActivity"> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.barteksc.pdfviewer.PDFView> </LinearLayout>
Output