Wednesday 27 March 2013

Integrating ZXing-QR in your Android Application


Follow the steps to integrating ZXing-QRSCan in your app:


ZXing is one of the most popular barcode scanning applications on the market.
They make it very easy for you to integrate into your application
via an intent but this means that your users must manually install the application from the market.


Step 1:  Obtain the zxing source code:
      
      Also you can find the full source code at this url

     
Step 2 :  Build zxing core using Apache Ant:
 
      You will need to build the core project into a jar file using apache ant
    (download from here http://ant.apache.org/ivy/download.cgi).
      Using a shell or cmd prompt navigate to the root directory of
      the downloaded zxing src and execute “ant -f core/build.xml”.
      This will produce a file core/core.jar which
      we will use in the next step.  

Step 3:     Build ZXing Android using Eclipse:
     
      Create a New Android Project (File –> New –> Android Project).
      Set the project name to ZXing (or similar).
      Select the “Create project from existing source” radio button
      Click “Browse” and navigate to the android project that you downloaded from
      zxing  and click “OK” Select “Finish”
     
       The project will not currently build. We need to add the core.jar file (that we
  produced    in the previous step) into our project. Right-click on ZXing
  project –> properties –> Java Build Path –> Add External Jars –> Navigate to
  and select core.jar –> Open –> OK.

  Actually, while we’re here we should do one more very important thing!
  Right-click on ZXing project –> properties –> Android –> Scroll down and
  check/tick the “Is Library” checkbox –> OK

Step 4:     Include ZXing Android into your project.
 
      Within Eclipse,  Right-click on YOURPROJECTNAMEHERE project –>
      properties –>Android –> Scroll down to Libraries section –> Click Add –>
      Select ZXing (which should appear as an option as a result of completing
      previous step).

Source Code:
     
     
1. AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidpeoplesconnect.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".QRCodeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

  </manifest>
 
 
 
2. QRCodeActivity.java

 package com.androidpeoplesconnect.app;

import com.google.zxing.client.android.CaptureActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class QRCodeActivity extends Activity {
 /** Called when the activity is first created. */
 Button b1;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  b1 = (Button) findViewById(R.id.submit);
  b1.setOnClickListener(new OnClickListener() {

   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(TestQRCodeActivity.this,
      CaptureActivity.class);
    // Intent intent = new
    // Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    startActivityForResult(intent, 0);
   }
  });

 }

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  if (requestCode == 0) {
   if (resultCode == 1) {
    // Handle successful scan
    String capturedQrValue = intent.getStringExtra("RESULT");
    //String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    Toast.makeText(TestQRCodeActivity.this,
      "Scan Result:" + capturedQrValue, Toast.LENGTH_SHORT)
      .show();

   } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
   }
  } else {

  }
 }
}




Friday 22 March 2013

Simple Android Splash Screen


Hi Every one ,In this tutorial I’m going to show you how to easily create an android splash screen in your application.
The main aim of this splash screen implementation is to start and display an activity for a certain amount of time and then start new activity.

Step 1:

SplashScreen.java:


package com.androidpeoplesconnect.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

/* Splash screen activity */
public class SplashScreen extends Activity {

       private static final int SPLASH_DURATION = 2000; // 2 seconds

       boolean mIsBackButtonPressed;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);

        Handler handler = new Handler();

        // run a thread after 2 seconds to start the home screen
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // make sure we close the splash screen so the user won't come back when it presses back key

                finish();
               
                if (!mIsBackButtonPressed) {
                    // start the home screen if the back button wasn't pressed already
                    Intent intent = new Intent(SplashScreen.this, Home.class);
                    SplashScreen.this.startActivity(intent);
               }
               
            }

        }, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

    }

    @Override
   public void onBackPressed() {

        // set the flag to true so the next activity won't start up
        mIsBackButtonPressed = true;
        super.onBackPressed();

    }
}

Step 2:

Create XML File  in res -> layout-> splash.xml



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/splash_screen"/>

Step 3:  Home.java :


import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;




public class Home extends Activity {

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Toast.makeText(this, "Your in Home", Toast.LENGTH_LONG).show();


}
}


Step 4: Mention the Home.java in  the manifest file

 <activity  android:name=".Home" ></activity>

Step 5: place the splash_screen.png image in drawable folder