Edit Pdf

Edit

We can edit a PDF, we have to create a new copy from original.

Generate pdf

private static String sInPutfile = "Android/data/Example.pdf";
    private static String sOutPutFile = "Android/data/ExampleCopy.pdf";

Then we have to PdfReader and PdfStamper, you can check the library iText.

private void editPDF(String string) {

        try {

            FileInputStream is = new FileInputStream(mPdfFile);
            PdfReader pdfReader = new PdfReader(is);
            PdfStamper pdfStamper = new PdfStamper(pdfReader,
                    new FileOutputStream(mPdfFileOutPut));

            for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {

                //create content from pdfStamper
                PdfContentByte content = pdfStamper.getUnderContent(i);

                // Text over the existing page
                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                        BaseFont.WINANSI, BaseFont.EMBEDDED);

                content.beginText();
                //set the font
                content.setFontAndSize(bf, 18);
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, string, 430, 15, 0);
                content.endText();

            }

            pdfStamper.close();

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

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

        }

    }

And we can edit the PDF generated other PDF in android.

Example in Github

Generate PDF

Generate

In this example we use iText library, we can download in this link.

Generate pdf

Then we add the library in libs file and we include this conde in your activity.

private void generateAndwritePDF() {

        // create a new document
        Document document = new Document();

        try {
            PdfWriter.getInstance(document, new FileOutputStream(mPdfFile));
            document.open();
            document.add(new Paragraph("Title"));
            document.close();

            mProgressDialog.cancel();

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

        }

    }

And we can generate pdf in android.

Example in Github

Open PDF

How to open PDF in Android?

Open pdf

We have to put the ourexample.pdf in our divice, in this case in Android/data

File file =new File(Environment.getExternalStorageDirectory(),"Android/data/ExampleForm.pdf");

if (file.exists()) {
     Uri path = Uri.fromFile(file);
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(path, "application/pdf");
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

     try {
        startActivity(intent);
     }
        catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this,
            "No Application Available to View PDF",
            Toast.LENGTH_SHORT).show();

        }
     }
}

Example in Github

To avoid multiple button click at same time

Bottleneck

Sometimes user can made bottleneck in your app when user click multiple button click at same time. you can avoid, it’s very easy.

To avoid multiple button clik at same time

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second(or any time span).

public class MainActivity extends Activity implements View.OnClickListener{

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

        buttonRight = (Button) findViewById(R.id.btnRight_activity_main);
        buttonLeft = (Button) findViewById(R.id.btnLeft_activity_main);

        buttonLeft.setOnClickListener(this);
        buttonRight.setOnClickListener(this);
    }

    // variable to track event time
    private long mLastClickTime = 0;

    @Override
    public void onClick(View v) {
        // Preventing multiple clicks, using threshold of 1 second
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
           return;
        }
        mLastClickTime = SystemClock.elapsedRealtime();
        pressedOnClick(v);
    }

    public void pressedOnClick(View v){
        switch (v.getId()){
            case R.id.btnLeft_activity_main:
                showToast("Left_activity_main");
                break;

            case R.id.btnRight_activity_main:
                showToast("Right_activity_main");
                break;
        }

    }
}

example in github

Video 360

You can build a application with a video 360.

video 360

You can download the project here

The first step:

We have to put in build.gradle, in dependences this line:

compile files('src/main/libs/panframe-1.9.jar')

Then we have to add video in src/raw

Now in our activity we put this code:

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;

import com.panframe.android.lib.PFAsset;
import com.panframe.android.lib.PFAssetObserver;
import com.panframe.android.lib.PFAssetStatus;
import com.panframe.android.lib.PFNavigationMode;
import com.panframe.android.lib.PFObjectFactory;
import com.panframe.android.lib.PFView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends ActionBarActivity implements PFAssetObserver, SeekBar.OnSeekBarChangeListener {

    PFView mPfview;
    PFAsset mPfasset;
    PFNavigationMode mCurrentNavigationMode = PFNavigationMode.MOTION;

    boolean 			mUpdateThumb = true;;
    Timer mScrubberMonitorTimer;

    ViewGroup mFrameContainer;
    Button mStopButton;
    Button				mPlayButton;
    Button				mTouchButton;
    SeekBar				mScrubber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        mFrameContainer = (ViewGroup) findViewById(R.id.framecontainer);
        mFrameContainer.setBackgroundColor(0xFF000000);

        mPlayButton = (Button)findViewById(R.id.playbutton);
        mStopButton = (Button)findViewById(R.id.stopbutton);
        mTouchButton = (Button)findViewById(R.id.touchbutton);
        mScrubber = (SeekBar)findViewById(R.id.scrubber);

        mPlayButton.setOnClickListener(playListener);
        mStopButton.setOnClickListener(stopListener);
        mTouchButton.setOnClickListener(touchListener);
        mScrubber.setOnSeekBarChangeListener(this);

        mScrubber.setEnabled(false);

        loadVideo("android.resource://" + getPackageName() + "/" + R.raw.skyrim360);

        showControls(true);
        mPfasset.play();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Show/Hide the playback controls
     *
     * @param  bShow  Show or hide the controls. Pass either true or false.
     */
    public void showControls(boolean bShow)
    {
        int visibility = View.GONE;

        if (bShow)
            visibility = View.VISIBLE;

        mPlayButton.setVisibility(visibility);
        mStopButton.setVisibility(visibility);
        mTouchButton.setVisibility(visibility);
        mScrubber.setVisibility(visibility);

        if (mPfview != null)
        {
            if (!mPfview.supportsNavigationMode(PFNavigationMode.MOTION))
//				_touchButton.setVisibility(View.GONE);
                Log.d("SimplePlayer", "Not supported nav");
        }
    }

    /**
     * Start the video with a local file path
     *
     * @param  filename  The file path on device storage
     */
    public void loadVideo(String filename)
    {

        mPfview = PFObjectFactory.view(MainActivity.this);
        mPfasset = PFObjectFactory.assetFromUri(this, Uri.parse(filename), this);

        mPfview.displayAsset(mPfasset);
        mPfview.setNavigationMode(mCurrentNavigationMode);

        mFrameContainer.addView(mPfview.getView(), 0);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mUpdateThumb = false;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mPfasset.setPLaybackTime(seekBar.getProgress());
        mUpdateThumb = true;
    }

    @Override
    public void onStatusMessage(final PFAsset pfAsset, PFAssetStatus pfAssetStatus) {

        switch (pfAssetStatus)
        {
            case LOADED:
                Log.d("SimplePlayer", "Loaded");
                break;
            case DOWNLOADING:
                Log.d("SimplePlayer", "Downloading 360 movie: "+ mPfasset.getDownloadProgress()+" percent complete");
                break;
            case DOWNLOADED:
                Log.d("SimplePlayer", "Downloaded to "+pfAsset.getUrl());
                break;
            case DOWNLOADCANCELLED:
                Log.d("SimplePlayer", "Download cancelled");
                break;
            case PLAYING:
                Log.d("SimplePlayer", "Playing");
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                mScrubber.setEnabled(true);
                mScrubber.setMax((int) pfAsset.getDuration());
                mPlayButton.setText("pause");
                mScrubberMonitorTimer = new Timer();
                final TimerTask task = new TimerTask() {
                    public void run() {
                        if (mUpdateThumb)
                            mScrubber.setProgress((int) pfAsset.getPlaybackTime());
                    }
                };
                mScrubberMonitorTimer.schedule(task, 0, 33);
                break;
            case PAUSED:
                Log.d("SimplePlayer", "Paused");
                mPlayButton.setText("play");
                break;
            case STOPPED:
                Log.d("SimplePlayer", "Stopped");
                mPlayButton.setText("play");
                mScrubberMonitorTimer.cancel();
                mScrubberMonitorTimer = null;
                mScrubber.setProgress(0);
                mScrubber.setEnabled(false);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                break;
            case COMPLETE:
                Log.d("SimplePlayer", "Complete");
                mPlayButton.setText("play");
                mScrubberMonitorTimer.cancel();
                mScrubberMonitorTimer = null;
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                break;
            case ERROR:
                Log.d("SimplePlayer", "Error");
                break;
        }
    }

    /**
     * Click listener for the play/pause button
     *
     */
    private View.OnClickListener playListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (mPfasset.getStatus() == PFAssetStatus.PLAYING)
            {
                mPfasset.pause();
            }
            else
                mPfasset.play();
        }
    };

    /**
     * Click listener for the stop/back button
     *
     */
    private View.OnClickListener stopListener = new View.OnClickListener() {
        public void onClick(View v) {
            mPfasset.stop();
        }
    };

    /**
     * Click listener for the navigation mode (touch/motion (if available))
     *
     */
    private View.OnClickListener touchListener = new View.OnClickListener() {
        public void onClick(View v) {
//			if (mPfview != null)
//			{

            Button touchButton = (Button)findViewById(R.id.touchbutton);
            if (mCurrentNavigationMode == PFNavigationMode.TOUCH)
            {
                mCurrentNavigationMode = PFNavigationMode.MOTION;
                touchButton.setText("motion");
            }
            else
            {
                mCurrentNavigationMode = PFNavigationMode.TOUCH;
                touchButton.setText("touch");
            }
            mPfview.setNavigationMode(mCurrentNavigationMode);
        }
//		}
    };

    /**
     * Called when pausing the app.
     * This function pauses the playback of the asset when it is playing.
     *
     */
    public void onPause() {
        super.onPause();
        if (mPfasset != null)
        {
            if (mPfasset.getStatus() == PFAssetStatus.PLAYING)
                mPfasset.pause();
        }
    }

    public void  onStartCommand(Intent intent, int flags, int startId) {
        mPfasset.play();
    }

}

You can download the project here