Just open your main XML file and paste below code:
</LinearLayout>
Now open your java file and paste below code:
Now run your code
<LinearLayout xmlns:android="http://schemas. android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="fill_ parent"
android:layout_height="fill_ parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_ content"
android:layout_height="wrap_ content"
android:text="My BOOK"
android:onClick="book"/>
Now open your java file and paste below code:
package com.smr; //package name
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.content. ActivityNotFoundException;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
setContentView(R.layout.activi ty_main);
}
public void book(View v)
{
//your pdf file path
File file=new File("/sdcard/anna.pdf");
if(file.exists())
{
Intent i= new Intent(Intent.ACTION_VIEW);
//set pdf to doc if you want to open doc
i.setDataAndType(Uri.fromFile( file),"application/pdf");
i.setFlags(Intent.FLAG_ ACTIVITY_CLEAR_TOP);
try
{
startActivity(i);
}
catch( ActivityNotFoundException e)
{
//if pdf reader not found than open browser to download pdf reader
Intent i1=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play. google.com/store/apps/details? id=com.adobe.reader&hl=en"));
startActivity(i1);
}
}
else
{
//if pdf not found on given location
Toast.makeText( getApplicationContext(),"anna.pdf not found", Toast.LENGTH_LONG).show();
}
}
}Now run your code
No comments:
Post a Comment