27 Jan 2014

Open/Read PDF file in Android

Hello Friends,

Today i am going to post pdf view sample. There are many ways to open PDF file in android application.



Solution 1 : View PDF  using external application like adobe reader.  

here i have already created on directory(pdf_Writer_Reader) in SDcard and it contain one PDF file, which i am going to open in PDFviewr application.

import java.io.File;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
 Button btnOpenPDF;
 public String Directory = Environment.getExternalStorageDirectory() + "/pdf_Writer_Reader/"; 

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btnOpenPDF=(Button) findViewById(R.id.btnViewPDF);

  btnOpenPDF.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    // Just Pass here PDF name which you want to open..
    OpenPDF openPDF = new OpenPDF("Impact  Brochure");
    openPDF.execute();

   }
  });
 }

 private class OpenPDF extends AsyncTask<Uri, Void, Void>
 {

  ProgressDialog pdgs = null;
  private Uri path = null;
  String filename = "";

  public OpenPDF(String filename)
  {
   this.filename = filename;

  }

  @Override
  protected void onPreExecute() {

   pdgs = ProgressDialog.show(MainActivity.this, "","Please wait...", true, false);
  }

  @Override
  protected Void doInBackground(Uri... params)
  {     

   File file=new File(Directory+filename+".pdf");
   if(file.exists())
   {
    path = Uri.fromFile(file);
    Log.e("Pdf Path=",""+path);
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void unused)
  {
   if (pdgs != null && pdgs.isShowing())
    pdgs.dismiss();

   try
   {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/pdf");
    try
    {
     startActivity(intent);

    }
    catch(ActivityNotFoundException e)
    {
     Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }


   } catch (Exception e)
   {
    e.printStackTrace();
   }
  }
 }
}

Solution 2 : Read/Write PDF  within application.  

here below is the library in which you will achieve read/write PDF file within application.
Download

Note: Do not forgot to add below  permission in android Manifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

19 Oct 2013

External Font Across the Android Application

Hello Friends, Today i am going to share Custom Font style of text view for whole application.

Sometimes in Android app we may need to maintain same font across application. At point of time previews post is not sufficient.

Here below Example shows how to use External font across the application.

Here First I have downloaded ".ttf” file from google. Created a new folder “font” in assets folder and paste your recently downloaded font file.




we can do that by just extending your class from android TextView.

Below code is to my customized TextView class(CustomFontTextView.java)

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomFontTextView extends TextView
{
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle)
 {
  super(context, attrs, defStyle);
  setFont();
 }

 public CustomFontTextView(Context context, AttributeSet attrs)
 {
  // call the constructor which has the complete definition
  this(context, attrs, 0);
 }

 public CustomFontTextView(Context context)
 {
  super(context);
 }

 public void setFont()
 {
  String ttf_fontPath = "fonts/TIMES_SQ.TTF";
Typeface font = Typeface.createFromAsset(getContext().getAssets(),ttf_fontPath);
  setTypeface(font);
 }
}

In our Layout insted of declare <TextView/> tag we just specify <com.hb.CustomFontTextView/>
in any were in our application.

 <com.hb.CustomFontTextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Hello Android"
        android:textColor="#65E561"
        android:textSize="45dp" />

Then external font will be applied to Text.



I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)