Android Studio Pdf View Plugins- Full setup step by step

 Android PDF View Plugins

1st Step -  Installation
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

2nd Step- gradle.properties - Add this Line

android.enableJetifier=true

3rd Step -layout.xml 

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/ViewPdf"
android:layout_height="match_parent"
android:layout_width="match_parent"

/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"

android:padding="170dp"
android:visibility="visible"
android:layout_height="match_parent">

</ProgressBar>


</RelativeLayout>

4th Step - Activity.java 




public class ViewBookActivity extends AppCompatActivity{
private ActivityViewBookBinding binding;
Integer pageNumber = 0;
String pdfFileName;
private PDFView pdf;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_book);
binding = ActivityViewBookBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

pdf = findViewById(R.id.ViewPdf);



String pdfurl="https://samplewebsite.com/sample.pdf";



try {

new RetrivePdfStream().execute(pdfurl);


}catch(Exception e){

binding.progressbar.setVisibility(View.GONE);
Toast.makeText(ViewBookActivity.this,"Load error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
}

}

class RetrivePdfStream extends AsyncTask<String,Void, InputStream>{


@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;

try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());

}
} catch (IOException e) {


return null;


}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {


binding.progressbar.setVisibility(View.GONE);

}
}).onError(new OnErrorListener() {
@Override
public void onError(Throwable t) {

Toast.makeText(ViewBookActivity.this, "Error in load", Toast.LENGTH_SHORT).show();
binding.progressbar.setVisibility(View.GONE);
}
}).enableSwipe(true).onPageChange(new OnPageChangeListener() {
@Override
public void onPageChanged(int page, int pageCount) {



binding.pageno.setText("Page-"+page+"/"+pageCount);
}
}).swipeHorizontal(true)
.load();
}
}

}

Comments

Popular posts from this blog

Amount Debited but Cash not dispense from Atm - Shutdown Atm during cash withdrawal।

Logical AND gate