First Android Application Tutorial and Example

Welcome!!

This is a series of blogs and videos where you learn how to create a fully functional Android Application!!!

At the end of this series of blogs and videos, you would have learnt

1. Activity
2. Service
3. How to use Google SpeechRecognizer
4. How to user Zbar for barcode
5. AsyncTask
6. How to fetch data from server using AsyncTask
7. ViewPager for swipe views
8. Implementation of Fragments
9. Tab views
10. ExpandableListView implementation
11. ListView implementation
12. Menu in ActionBar
13. How to push data to server

Click the below link to take you to the series of posts.

E-Commerce-App-on-Android

Hope you find it useful !!!


Android Set Alarm and perform action at scheduled time | Schedule Android Notifications


Let us assume you want to set up alarms at scheduled time which should trigger notifications. Here is how you can achieve it.

You can trigger an alarm at a scheduled time and then use an IntentService to capture it once alarm goes off.

In the main activity (lets assume the name is MainActivity, you can create an alarm like this.
    AlarmManager mgr = (AlarmManager) MainActivity.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(MainActivity, NotifService.class);
    PendingIntent pi = PendingIntent.getService(MainActivity, 0, intent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);
    //where the PERIOD defines after how much milliseconds you want the alarm to go off.

To configure the AlarmManager differently, you can read more about it.

NotifService is what captures the alarm when it goes off. Create a class called NotifService it extends IntentService

public class NotifService extends IntentService {

    public NotifService() {
      super("My service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         //write your code for sending Notification
         Intent intent = new Intent(); 
         intent.setAction("com.xxx.yyy.youractivitytobecalled");
         PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder (getApplicationContext());
        builder.setContentTitle("Your Application Title");
        builder.setContentText("Notification Content");
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Your content");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setOngoing(false);
        builder.setPriority(0);

        Notification notification = builder.build();
        NotificationManager notificationManger = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
        notificationManger.notify(1, notification);

    }
    }

Register NotifService in your AndroidManifest.xml
 
    <application
             fill content here />

    <service android:name=".NotifService" >
    </service>

    </application>

Android Menu Options | Action Bar Menu Options

The menu options in Android which appear as 3 vertical dots on the action bar or any custom bar is a very utility way of providing users with selection choices without being intrusive. Here is a video on all you wanted to know about managing Menu options in Android.



The source code is as follows.

Inside the /res/menu folder, create a file start_menu.xml




<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
        android:id="@+id/action_login"
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/login"/>
<item
        android:id="@+id/action_orderhistory"
        android:orderInCategory="2"
        android:showAsAction="never"
        android:title="@string/orderhistory"/>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="3"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    
</menu>

In your activity you can put this code to inflate the menu



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

Android | Minimum Required SDK, Target SDK, Compile With

While creating a new project you would have seen that we need to define the following 3. Here is a detailed explanation of each of them.

Minimum Required SDK - As we know, Android typically does one major release a year. And with every version, Android introduces some new features. Now minimum required SDK defines what is the earliest SDK that you want your application to be compatible with. For example, if you define your Minimum Required SDK as Froyo, then a feature which was introduced in Icecream Sandwich may not work, in which case, you have to handle it specifically how it should behave on a phone which is running on Pre Icecream Sandwich Android Version.

To take a decision on this, please visit the following link to know what percentage of mobile devices are running on which version of Android.

https://developer.android.com/about/dashboards/index.html

Based on this, you can take a decision what your Minimum Required SDK is. for example, Froyo and Gingerbread combined constitutes only 10.4% of the devices (As of Nov 2014). If you are ok with leaving these devices out, then we can go ahead with Icecream Sandwich as Minimum Required SDK.

Target SDK - This is the highest Android SDK version for which you have tested your app and it is working fine. Mind it, when android releases a new version, it may happen that your application stops working. So you need to retest with every android release and check that your app is working fine.

Compile With - Here you have to define the Android SDK version with which you will compile your app code.



Async Task : Can you wait for AsyncTask to complete

Recently for one of my projects, i was using AsyncTask to fetch data from server using JSON and use the data to populate an arraylist. I wanted to use this arraylist once data fetch is over to render on screen.

To illustrate, I wanted to fetch the list of categories from Server using AsyncTask and then use this list of categories to populate a ListView where each category was a different row in the ListView.

To achieve this, my UI had to wait till the AsyncTask was complete. I read a lot and I got to know that using ProgressDialog is the way to go. So I designed my code like this.

In my Fragment Class, i defined

ListView searchResults;


In OnCreateView, I had the following code.
            

searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);

myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);

// The AsyncTask populates the ArrayList arrayCategory with the list of 
//categories that we received from the server

searchResults.setAdapter(new SearchResultsAdapter(getActivity(),arrayCategory));


In SearchResultsAdapter, I put the code to populate the ListView with data from arrayCategory.

In myAsyncTask, I initialized a ProgressDialog

I defined

ProgressDialog pd; 

in the myAsyncTask.

In onPreExecute
                 
pd= new ProgressDialog(getActivity());
pd.setCancelable(false);
pd.getWindow().setGravity(Gravity.CENTER);
pd.show();

            
In onPostExecute
                  

pd.dismiss();

This is where I was wrong. ProgressDialog in AsyncTask does stop the UI thread, which means that the users cannot do anything till AyncTask is complete. However, ProgressDialog doesn't stop processing in the thread.  So immediately after AsyncTask is called, while doInBackground is running, the system will move on to execute searchResults.setAdapter, even through the ProgressDialog is visible on the screen.

Note, arrayCategory has not yet been populated with data by AsyncTask which is still running, hence searchResults.setAdapter either does not populate all the data, or throws IndexOutOfBound exception.

There are 2 ways to handle this.

1. Using Get statement - This converts the AsyncTask into a Synchronous task. The Get statement waits for the AsyncTask to complete and returns what you want to be returned, like arrayCategory, for example. Through Get, you can return anything from the AsyncTask.

The code for the same would be something like this. In onCreateView, the code should look like this.

searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);

myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);

// The AsyncTask populates the ArrayList arrayCategory with the list of categories that we

// received from the server

try {

     String temp = m.get();

    } catch (InterruptedException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

    } catch (ExecutionException e) {

         // TODO Auto-generated catch block

         e.printStackTrace();
   }


searchResults.setAdapter(new SearchResultsAdapter(getActivity(),arrayCategory)); 

This way, the thread waits and only proceeds once some value is returned. Note: This is not the recommended way of handling this.

2. Calling searchResults.setAdapter from onPostExecute of AsyncTask.  It is recommended, that instead of using Get, we should put what ever we expect to be done after AsyncTask is complete inside doPostExecute. In this case, we should actually put the call the ListAdapter from within doPostExecute. That way, ListAdapter will only be called once doInBackground is complete and hence all data is populated from JSON into arrayCategory.

Android Spinner: how to set default value

While using a Spinner Widget in Android, we face a peculiar problem. Lets say that I have a spinner for choosing the City from a list of cities. This is how I want this to look like. However, here likes the problem.

                         

The code that I wrote is as follows

String[] city_list = new String[3];
city_list[0] = "Delhi";
city_list[1] = "Gurgaon";
city_list[2] = "Noida";

ArrayAdapter<String > aa=new ArrayAdapter<String> (getApplicationContext(),         
R.layout.spinner_item, city_list);

city_spinner = (Spinner) findViewById(R.id.spinner1);
city_spinner.setAdapter(aa);

By default Delhi, being the first entry in the ArrayList aa which is the adapter for the spinner, it is shown by the Spinner by default. This is where the problem starts. Since the first entry is shown, the city_spinner.setOnItemSelectedListener identifies this as a selected item. Hence it does not wait for the user to select anything, it simply assumes that Delhi is the choice from user and moves ahead in code. To bypass this, I tried to keep a counter to identify the first time SetOnItemSelectedListener is called, and handle it to do nothing. My code looks like this.

city_spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
      if (spinner_count == 0)
      {
         spinner_count++;
      }
      else
      {
         //do what you want to do with the selected field.
      }
    }
});

The spinner_count will be 0 when the first time the activity is rendered, hence as soon as it displays first item (Delhi in this case) by default, it will trigger onItemSelected, but since we have handled it, nothing will happen and the UI will wait for the user to select an Item.

This created another challenge. So when I select Gurgaon or Noida with are the 2nd and 3rd entries, it is working fine. However, if I select Delhi from the dropdown, nothing happens. This is because, since the previous Item Selected by default was Delhi, and I am again trying to select Delhi, it is not getting identified as a new Item selection, hence this portion of the code is not getting called at all. So net net, you cannot select the first entry using this approach. 

So I tried a new approach. I added an item "Choose City" in city_list as the first element. Then inside onItemSelected, i specifically handed the case where if 'Choose City' is selected, then do nothing. 

The code is as follows.

String[] city_list = new String[4];

city_list[0] = "Choose City";
city_list[1] = "Delhi";
city_list[2] = "Gurgaon";
city_list[3] = "Noida";

ArrayAdapter<String > aa=new ArrayAdapter<String> (getApplicationContext(), 
R.layout.spinner_item, city_list);


city_spinner = (Spinner) findViewById(R.id.spinner1);
city_spinner.setAdapter(aa);

city_spinner.setOnItemSelectedListener(new OnItemSelectedListener()

{
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
     {
         if (parent.getItemAtPosition(position).equals("Choose City"))
         {
             //do nothing.
         }
         else
         {
            // write code on what you want to do with the item selection
         }
     }
});
The output will be as follows as works as expected.


First Android App: Step 1: ADT installation

Welcome!!! I am new to android and I have taken it as a task to learn application development in Android within the next 10 days. So I thought, why not document the steps in detail so that I can help others like me who are interested in picking up the tricks of the trade.

I have taken it upon myself to create a shopping Cart application which is fully functional with all features. This blog will trace my development one step at a time.

How did I begin?

Step 1: My first step was to go through http://www.tutorialspoint.com/android/

This tutorial is very good with respect to familiarizing you with the different components of an Android application. Although this will not help you much when it comes to developing an actual application. 

Take your time and go through it at least once. This greatly helped me understand the purpose of different components and how they are to be used. Don't think much about the syntax etc, they will come as you start coding.

Step 2: Download ADT and install it in your system. To download ADT, browse to http://developer.android.com/tools/sdk/eclipse-adt.html and download the latest version. Make sure you select the correct Bit ( 32 Bit or 64 Bit) depending on your Operating System.

Step 3: Once the ADT has been downloaded, extract the zip file and in the extracted folder, browse to /eclipse/eclipse.exe and open Eclipse.

Step 4: Now navigate to Windows / Android SDK Manager and Install all the packages that are being suggested. It is important that you install all the packages depending on your minimum API compatibility that you require.

First Android App | Step 2 : first Hello World Application

Now that the ADT is installed, you can browse to the adt / eclipse folder and open eclipse.exe.

Once eclipse has opened, it is time to create a new project.

Go to File / New / Android Application Project. A pop up window will open up like below.



Please enter an application Name. In our case it is Basket.
Project Name is automatically populated.
Package Name is typically the reverse of your domain name followed by project name.

So if you domain name is www.zing.com, and your project name is basket, then the package name is typically kept as com.zing.basket. Remember, you can put more words to package name followed by . (dot) but convention is to keep it to 3 words only.

Next we need to select the Minimum Required SDK, Target SDK, Compile With and Theme.

For more detailed explanation of Min Required SDK, Target SDK and Compile with, please visit another detailed blog post of mine. Click here...

In our case, we are selecting Minimum Required SDK as API 14, Target SDK and compile with as API 18. The theme we selected is Holo Light with Dark Action Bar.

Next we need to select if we want to create a custom launcher icon and also create an activity. Leave these configurations as default and click on next.

On clicking Next, we have to select the icon for our application. If you have created an icon, you can browse and select it, alternatively you can select any icon for the time being and you can change it to another icon later on.

These icon file has the name ic_launcher and they are placed inside the res/drawable folders. These can be replaced later on if required.

Next, select blank activity and click next.

In our project, the first activity which will get called is named StartScreen.java. So change the name as StartScreen, the layout name will be activity_start_screen, let navigation type be none. On clicking next, your project will be created along with the following files

1. src / com.zing.basket / StartScreen.java

2. res/layout/activity_start_screen.xml

3. res/menu/start_screen.xml

4. res/values/string.xml

5. res/values/dimens.xml

6. res/values/styles.xml

7. res/drawable folders/ic_launcher.jpg

8. AndroidManifest.xml


Now you can rightclick on the Project Name 'Basket' on the left side menu and run it as Android Application. Make sure you have connected your Android phone to your computer using USB and also that you have activated Debugger Options in your Android Phone.

You can watch the entire tutorial through our Youtube Video as well.





First Android App | Step 3 | Getting data from Server using AsyncTask JSON

Welcome back!! We are well on our way to building our first android app - a complete E-Commerce Application.

I assume you would have gone through Step 1 and Step 2 becoming coming here. If you haven't, please go through them as well.

In this step, we are trying to create the Splash screen for our Application, create database and tables and also fetch data from Server in JSON format through AsyncTask.

Please watch our video to get a detailed understanding of how the code works.



The source code is attached.

The RESTful API which is being called is http://lawgo.in/lawgo/city?format=json


JSONParser.java

package com.zing.basket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
 
    // constructor
    public JSONParser() {
 
    }
    
    public JSONObject getJSONFromUrl(String url) throws Exception {
      
     // Making HTTP request
      try {
          
         HttpParams httpParameters = new BasicHttpParams();
         int timeoutConnection = 20000;
         HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
         // Set the default socket timeout (SO_TIMEOUT) 
         // in milliseconds which is the timeout for waiting for data.
         int timeoutSocket = 25000;
      // defaultHttpClient
         DefaultHttpClient httpClient = new DefaultHttpClient();
         httpClient.setParams(httpParameters);
         //HttpPost httpPost = new HttpPost(url);
        HttpGet httpPost = new HttpGet(url);
         HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

          HttpResponse httpResponse = httpClient.execute(httpPost);
          
          HttpEntity httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();          
       
          
          /*HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(url);
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity entity = response.getEntity();
          is = entity.getContent();*/

      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
       
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
          //Log.d("string","string is "+json);
      } catch (Exception e) {
          //Log.e("Buffer Error", "Error converting result " + e.toString());
      }

      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);
      } catch (JSONException e) {
          //Log.e("JSON Parser", "Error parsing data " + e.toString());
      }

      // return JSON String
      return jObj;
  }
 
}


activity_start_screen.xml



 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B22222"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartScreen">

    <TextView
        android:id="@+id/SelectCityText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="107dp"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="#F8F8FF"
        android:textSize="40sp"/>

<RelativeLayout>


StartScreen.java


package com.zing.basket;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class StartScreen extends Activity {
 
    SQLiteDatabase sqLite;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_start_screen);
  
 //initializing the action bar and hiding it. 
 ActionBar actionBar = getActionBar();
 actionBar.hide();
  
   
 //create a database if it doesnt exist, if it exists, it will open the database.
  
 sqLite=this.openOrCreateDatabase("basketbuddy", MODE_PRIVATE, null);
  
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS USER (USER_ID VARCHAR,NAME VARCHAR,EMAIL VARCHAR)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CITY_LIST (ID INTEGER primary key autoincrement,CITY_NAME VARCHAR)" );
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS USER_PREF (ID INTEGER primary key autoincrement,CITY_NAME VARCHAR default 'NONE', VOICE_ON VARCHAR, NOTIF VARCHAR)" );
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CHECKLIST (ID INTEGER primary key autoincrement, PRODUCT_NAME VARCHAR, PRODUCT_CHECKED VARCHAR)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS PRODUCT_MASTER (ID INTEGER primary key autoincrement, PRODUCT_CODE INTEGER, PRODUCT_DIVISION INTEGER, PRODUCT_DEPARTMENT INTEGER"+ 
 "PRODUCT_GF INTEGER, PRODUCT_F INTEGER, PRODUCT_SUBF INTEGER, PRODUCT_NAME VARCHAR, PRODUCT_BARCODE INTEGER, PRODUCT_GRAMMAGE VARCHAR, PRODUCT_MRP INTEGER, PRODUCT_BBPRICE INTEGER)");
 sqLite.execSQL("CREATE TABLE IF NOT EXISTS CART (ID INTEGER primary key autoincrement, PRODUCT_CODE INTEGER, PRODUCT_DIVISION INTEGER, PRODUCT_DEPARTMENT INTEGER"+ 
 "PRODUCT_GF INTEGER, PRODUCT_F INTEGER, PRODUCT_SUBF INTEGER, PRODUCT_NAME VARCHAR, PRODUCT_BARCODE VARCHAR, PRODUCT_GRAMMAGE VARCHAR, PRODUCT_MRP INTEGER, PRODUCT_BBPRICE INTEGER, PRODUCT_QTY INTEGER, PRODUCT_VALUE INTEGER)");
  
 //call asynctask to fetch list of city to populate the dropdown. 
  
 updateList ul=new updateList();
 ul.execute();
  
    }
 
    class updateList extends AsyncTask<Void, Void, String>
    {
 JSONParser jParser;
 String url=new String();
  
 @Override
 protected void onPreExecute() {
    super.onPreExecute();
    url="http://lawgo.in/lawgo/city?format=json";
    jParser = new JSONParser();
 }
   
   
 @Override
 protected String doInBackground(Void... params) 
 {
     try
       {
  JSONObject json=jParser.getJSONFromUrl(url);
  Cursor sample;
  try 
  {
      JSONArray jar=json.getJSONArray("city");
        Log.d("arindam", ""+jar.length());
      
                    for(int i=0;i<jar.length();i++)
        {
      
   JSONObject j=jar.getJSONObject(i);
     
   //check if the city is already there in citylist.
   sample=sqLite.rawQuery("SELECT CITY_NAME FROM CITY_LIST WHERE CITY_NAME='"+j.getString("cityname")+"'",null);
   
   if(sample.moveToFirst())
   {
    continue;
   }
   else
   {
    sqLite.execSQL("insert into CITY_LIST (CITY_NAME) VALUES ('"+j.getString("cityname")+"')");
    Log.d("arindam",j.getString("cityname"));
   }
      
      }
  } catch (JSONException e) 
  {
      // TODO Auto-generated catch block
        return "perror";
  }
    
    }
    catch(Exception e)
    {
  e.printStackTrace();
  return "error";
    }
   
    return "success";
        }
  
 protected void onPostExecute(String result) 
 { 
    super.onPostExecute(result);
    if(result.equals("perror"))
    {
   moveTaskToBack(true);
   Toast.makeText(getApplicationContext(), "Error!!! Oops..that was humiliating. Could you please try again", Toast.LENGTH_LONG).show();
    }
    else if(result.equals("error"))
    {
   moveTaskToBack(true);
   Toast.makeText(getApplicationContext(), "Please Check your Internet Connection", Toast.LENGTH_LONG).show();
    }
    else
    {
   new Handler().postDelayed(new Runnable(){
   @Override
   public void run()
   {
        
    Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null); 
    Log.d("arindam" ,"c count"+c.getCount());
    if (c.getCount()== 0)
   {
     sqLite.execSQL("INSERT INTO USER_PREF (CITY_NAME, VOICE_ON, NOTIF) VALUES('NONE','Y','Y')");
   }
       
    
    Cursor d=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null);
    Log.d("arindam" ,"d count"+d.getCount());
       
    if(d.moveToFirst())
    { 
     Log.d("arindam" ,"d NONE"+d.getString(0));
     if(d.getString(0).equals("NONE"))
        {
         //Intent intent=new Intent(StartScreen.this,CityScreen.class);
         //startActivity(intent);
        }
        else
        {
         //Intent intent=new Intent(StartScreen.this,HomeScreen.class);
         //startActivity(intent);
        }
     finish(); 
    }
      }
   },1000);
       }
     }
 }
 
 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);
 }
 
 @Override
 public void onDestroy()
 {
  super.onDestroy();
  sqLite.close();
 }

}

AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>





First Android App | Step 4 - Spinner to select default city

Welcome Back!!

This is the step 4 on our way to build a fully functional e-commerce application from scratch.

In step 3, we created the Splash screen which downloaded the list of Cities from the server in JSON format using AsyncTask. In this post, we are going to build the City selection screen. This screen will only come once on first time login of the user post installation. Once the user selects the default city, next time onwards, the application will move directly to HomeScreen from the StartScreen and skip CityScreen completely.

The complete working of this part has been explained in my youtube video.



The final output will look like this.



The source code is as follows:

activity_city_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B22222"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CityScreen" >

    <TextView
        android:id="@+id/SelectCityText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/selectcity"
        android:layout_marginTop="120dp"
        android:textColor="#ffffff"
        android:textSize="28sp" />
    
    
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/SelectCityText"
        android:popupBackground="#470E0E" />

</RelativeLayout>


spinner_item.xml

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

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textSize="20dip"
    android:gravity="left"  
    android:textColor="#ffffff"         
    android:padding="5dip"/>

CityScreen.java

package com.zing.basket;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class CityScreen extends Activity {
 
 SQLiteDatabase sqLite;
 Spinner city_spinner;

 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_city_screen);
  
  Typeface type= Typeface.createFromAsset(getAssets(),"fonts/book.TTF");
  
  //This section is to hide the action bar. 
  ActionBar actionBar = getActionBar();
  actionBar.hide();
  
  //Ideally SQL should be handled in a separate helper, but for ease of understanding to start off, I
  //have kept the code here. 
  sqLite=this.openOrCreateDatabase("basketbuddy", MODE_PRIVATE, null);
  
  Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM CITY_LIST",null);
  
  //ideally atleast 1 city should be there in city_name. As I have already synced this with the server
  // in StartScreen.java
  
  if (c.getCount()== 0)
  {
   Toast.makeText(getApplicationContext(), "Oh ho..Some unexpected problem. Please restart the application", Toast.LENGTH_LONG).show();
   
  }
  
  TextView city_selection=(TextView) findViewById(R.id.SelectCityText);
  city_selection.setTypeface(type);
  
  //defining the array that will hold the City Names. 
  String[] city_name_db=new String[(c.getCount()+1)];
    
    
  //by default, the first entry for city list is "Choose City". We will understand why 
  //this is necessary later.
  city_name_db[0] = "Choose City";
  
  //moving the city names from sqlite to an array city_name_db
  if(c.moveToFirst())
  {
   int count=1;
   do{
    city_name_db[count]=c.getString(0);
    count++;
   }while(c.moveToNext());
  }
  
  //creating an ArrayAdapter for the spinner and then associating the city ArrayAdapter
  //to the spinner.
  
  ArrayAdapter aa=new ArrayAdapter(getApplicationContext(),R.layout.spinner_item,city_name_db);
  city_spinner = (Spinner) findViewById(R.id.spinner1);
  city_spinner.setAdapter(aa);
  
  //There is an inherent problem with Spinners. Lets assume that there are 3 cities Delhi, Gurgaon and Noida. The
  //moment I populate these 3 cities to the spinner, by default Delhi will get selected as this is the 
  //first entry. OnItemSelectedListener will get triggered immediately with Delhi as selection and the code 
  //will proceed. Net net, even the default first value is taken as an ItemSelected trigger. 
  
  //the way to bypass this is to add a default value 'Choose City' in the ArrayAdapter list. And then
  //inside the onItemSelected method, ignore if 'Choose City' has been selected. 
  
  //SetOnItemSelectedListener listens for any change in item selected, if found thn it will call onItemSelected method. 
  
  city_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView parent, View view,
     int position, long id) 
   {
    if (parent.getItemAtPosition(position).equals("Choose City"))
    {
     //do nothing.
     
    }
    else
    {
     // save the selected city as a default city for shopping
     //this city selection is saved in DB. We may even decide to send this data to server, 
     //however in this example, we are not doing so. 
     
     String city_name=city_spinner.getSelectedItem().toString();
     Cursor c=sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",null);
     if (c.getCount()== 0)
     {
      sqLite.execSQL("insert into USER_PREF (CITY_NAME, VOICE_ON) VALUES ('"+city_name+"','Y','Y')");
     }
     if(c.moveToFirst())
     {
      sqLite.execSQL("update USER_PREF set CITY_NAME = '"+city_name+"'");
     }
     
     //Intent intent=new Intent(CityScreen.this,HomeScreen.class);
     //startActivity(intent);
     sqLite.close();
     finish();
    }
   }

   @Override
   public void onNothingSelected(AdapterView arg0) 
   {
    // TODO Auto-generated method stub
    
   }
  });
   
 }
 
 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);
 }
 
 @Override
 public void onResume()
 {
  super.onResume();
  
 }
 
 @Override
 public void onPause()
 {
  super.onPause();
 }

}




Remove the commenting from these 2 lines of code from StartScreen.java inside the onPostExecute method of AsyncTask

Intent intent=new Intent(StartScreen.this,CityScreen.class);
startActivity(intent);

AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            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.zing.basket.CityScreen"
            android:label="@string/city_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
    </application>
    

</manifest>

First Android App | Step 5 | HomeScreen with ViewPager | FragmentPagerAdapter | Action Bar with 3 tabs

Hellos!!

In this post we will discuss the construction of the HomeScreen for our E-Commerce Application. The HomeScreen will have an ActionBar with Menu and 3 tabs which we have implemented through a ViewPager and each of the tabs have been defined as Fragments.

The complete tutorial is available in the following youtube video.




The final result is as follows.




Source Code

activity_home_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.V4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#ffffff">
    
    
</android.support.V4.view.ViewPager>

fragment_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" 
   android:background="#AAE6E6">
   
</RelativeLayout>

fragment_mycart.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:id="@+id/mychecklist"
   android:orientation="vertical" 
   android:background="#00FF33">

</LinearLayout>

fragment_quickorder.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:background="#E6BBE6" >

</RelativeLayout>


HomeScreen.java

package com.zing.basket;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class HomeScreen extends FragmentActivity implements ActionBar.TabListener 
{
 
ActionBar bar;
ViewPager viewpager;
FragmentPageAdapter ft;

  
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  
  viewpager = new ViewPager(this);
        viewpager.setId(R.id.pager);
        setContentView(viewpager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());
        viewpager.setAdapter(ft);
        
  final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        
        bar.addTab(bar.newTab().setText("Search").setTabListener(this));
        bar.addTab(bar.newTab().setText("Cart").setTabListener(this));
        bar.addTab(bar.newTab().setText("Quick Order").setTabListener(this));
        
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
   
   @Override
   public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub
    bar.setSelectedNavigationItem(arg0);
   }
   
   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
    
   }
   
   @Override
   public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub
    
   }
  });
        
        
 }
 
 
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start_screen, menu);
        return true;
    }
 
 
 @Override
 public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  
 }


 @Override
 public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  viewpager.setCurrentItem(tab.getPosition());
 }


 @Override
 public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
  // TODO Auto-generated method stub
  
 }
}

FragmentPageAdapter.java

package com.zing.basket;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragmentPageAdapter extends FragmentPagerAdapter {

 public FragmentPageAdapter(FragmentManager fm) {
  super(fm);
  // TODO Auto-generated constructor stub
 }

 @Override
 public Fragment getItem(int arg0) {
 
  switch (arg0)
  {
  case 0:
    return new Search();
  case 1: 
    return new MyCart();
  case 2:
    return new QuickOrder();
  default:
    break;
  }
  return null;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return 3;
 }

}


Search.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Search extends Fragment
{
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  return myFragmentView;
 }  
 
}



QuickOrder.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class QuickOrder extends Fragment {
 
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
 {
  myFragmentView = inflater.inflate(R.layout.fragment_quickorder, container, false);
   
  return myFragmentView;
 }
}


MyCart.java

package com.zing.basket;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyCart extends Fragment {
 
 View myFragmentView;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
   myFragmentView = inflater.inflate(R.layout.fragment_mycart, container, false);
   
   return myFragmentView;
  }
 
}


Remove the commenting of 2 lines of code in StartScreen.java, inside the onPostExecute method of AsyncTask.

Intent intent=new Intent(StartScreen.this,HomeScreen.class);
startActivity(intent);

Remove the commenting of 2 lines of code in CityScreen.java, where we are calling the HomeScreen.class through intent

Intent intent=new Intent(CityScreen.this,HomeScreen.class);
startActivity(intent);



AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.zing.basket.StartScreen"
            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.zing.basket.CityScreen"
            android:label="@string/city_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
        
        <activity
            android:name="com.zing.basket.HomeScreen"
            android:label="@string/home_screen"
             android:screenOrientation="portrait"
            android:launchMode="singleTask" >
        </activity>
        
        
    </application>
    

</manifest>


res/values/strings.xml - Add this line of code

<string name="home_screen">Home Screen</string>

First Android App | Step 6 | Custom Objects

Before we proceed, it is important that we understand the importance and need for custom objects..

The video for the same can be see on youtube



The code for custom object Product.java is as follows. This is to be placed inside com.zing.basket.util (a separate util folder created with /src/ for storing all the util codes)

package com.zing.basket.util;

public class Product{
 
 private String product_name;
 private String product_code;
 private String product_barcode;
 private String product_department;
 private String product_division;
 private String product_family;
 private String product_groupfamily;
 private String product_subfamily;
 private String product_grammage;
 private String product_mrp;
 private String product_bbprice;
 private String product_qty;
 private String product_value;
 
 public void setProductName (String product_name)
 {
     this.product_name = product_name;
 }
 
 public String getProductName()
 {
     return product_name;
 }

 public void setProductGrammage (String product_grammage)
 {
     this.product_grammage = product_grammage;
 }
 
 public String getProductGrammage()
 {
     return product_grammage;
 }
 
 public void setProductBarcode (String product_barcode)
 {
     this.product_barcode = product_barcode;
 }
 
 public String getProductBarcode()
 {
     return product_barcode;
 }
 
 public void setProductDivision (String product_division)
 {
     this.product_division = product_division;
 }
 
 public String getProductDivision()
 {
     return product_division;
 }
 
 public void setProductDepartment (String product_department)
 {
     this.product_department = product_department;
 }
 
 public String getProductDepartment()
 {
     return product_department;
 }
 
 public void setProductFamily (String product_family)
 {
     this.product_family = product_family;
 }
 
 public String getProductFamily()
 {
     return product_family;
 }
 
 public void setProductGroupFamily (String product_groupfamily)
 {
     this.product_groupfamily = product_groupfamily;
 }
 
 public String getProductGroupFamily()
 {
     return product_groupfamily;
 }
 
 public void setProductSubFamily (String product_subfamily)
 {
     this.product_subfamily = product_subfamily;
 }
 
 public String getProductSubFamily()
 {
     return product_subfamily;
 }
 
 public void setProductMRP (String product_mrp)
 {
     this.product_mrp = product_mrp;
 }
 
 public String getProductMRP()
 {
     return product_mrp;
 }
 
 public void setProductBBPrice (String product_bbprice)
 {
     this.product_bbprice = product_bbprice;
 }
 
 public String getProductBBPrice()
 {
     return product_bbprice;
 }
 
 public void setProductQty (String product_qty)
 {
     this.product_qty = product_qty;
 }
 
 public String getProductQty()
 {
     return product_qty;
 }
 
 public void setProductValue (String product_value)
 {
     this.product_value = product_value;
 }
 
 public String getProductValue()
 {
     return product_value;
 }
 
 public void setProductCode(String product_code)
 {
     this.product_code = product_code;
 }
 
 public String getProductCode()
 {
     return product_code;
 }
}

First Android App | Step 7 | Populate ListView with data from JSON | SearchView and Listview to display search results

In this blog, what we are trying to achieve is the search functionality.

There is a search box on the top. When user tries to enter some text, a restful API will be called on the server with this text as the search text. The returned data which is in JSON format will be parsed and pushed into an ArrayList. Then this ArrayList will be used to populate the ListView of the results.

Once the results are displayed, the user can scroll up or down and there would be a provision for user to add this element to cart as well.

The final result looks like this


The complete video on how to achieve this has been explained in the below video.



The source code is as follows

Update the fragment_search.xml with the following code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" 
   android:background="#E6E6E6">

    <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:background="#FFFFFF"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"/>
    
    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imageButton1"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        
    </SearchView>
    
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="0dp"
        android:layout_marginTop="3dp"
        android:layout_toLeftOf="@+id/imageButton2"
        android:src="@android:drawable/ic_btn_speak_now" />
    
    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:src="@drawable/barcode"/>
    
    
 <ListView
        android:id="@+id/listview_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
     android:layout_below="@+id/imageButton2"
        android:layout_marginTop="25dp" 
        android:dividerHeight="5dp"
        android:divider="#E6E6E6"/>
 
</RelativeLayout>

Please the following file listtwo_searchresults.xml in the res/layout folder

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_height="match_parent">
    
    
    <ImageView
        android:id="@+id/productImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft = "5dp"
        android:layout_alignParentTop="True"
        android:src="@drawable/ic_launcher" />
    
    <TextView
        android:id="@+id/product_name"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/productImage"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Product Name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="18dp" />
    
    <TextView
        android:id="@+id/product_mrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/productImage"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="MRP:"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dp" />
    
    <TextView
        android:id="@+id/product_mrpvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_mrp"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Rs 100"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dp" />
 
    <TextView
        android:id="@+id/product_bb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_mrpvalue"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="20dp"
        android:text="BB Price:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14dp" />
    
    <TextView
        android:id="@+id/product_bbvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/product_bb"
        android:layout_below="@+id/product_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:text="Rs 95"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14dp" />

    <Button
        android:id="@+id/add_cart"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_below="@+id/matching_product"
        android:layout_alignParentRight="true"
        android:layout_marginRight ="10dp"
        android:gravity="center"
        android:drawableLeft="@drawable/cart_low" 
        style="?android:attr/borderlessButtonStyle" />
    
</RelativeLayout>


Place the barcode icon in the res/drawable-hdpi folder. Change the name of the file as barcode.jpg





Place the cart icon in the res/drawable-hdpi folder. Change the name of the file as cart_low.jpg





Update the code for Search.java


package com.zing.basket;

package com.zing.basket;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;


public class Search extends Fragment
{
 View myFragmentView;
 SearchView search;
 ImageButton buttonBarcode;
 ImageButton buttonAudio;
 Typeface type;
 ListView searchResults;
 String found = "N";
 
 
 //This arraylist will have data as pulled from server. This will keep cumulating.
 ArrayList<Product> productResults = new ArrayList<Product>();
 //Based on the search string, only filtered products will be moved here from productResults
 ArrayList<Product> filteredProductResults = new ArrayList<Product>();
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  //get the context of the HomeScreen Activity
  final HomeScreen activity = (HomeScreen) getActivity();
  
  //define a typeface for formatting text fields and listview. 
  
  type= Typeface.createFromAsset(activity.getAssets(),"fonts/book.TTF");
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  search=(SearchView) myFragmentView.findViewById(R.id.searchView1);
        search.setQueryHint("Start typing to search...");
        
  searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);
  buttonBarcode = (ImageButton) myFragmentView.findViewById(R.id.imageButton2);
  buttonAudio = (ImageButton) myFragmentView.findViewById(R.id.imageButton1);
  
  
  //this part of the code is to handle the situation when user enters any search criteria, how should the 
  //application behave?
  
  search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() 
        {
      
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
       // TODO Auto-generated method stub
        
       //Toast.makeText(activity, String.valueOf(hasFocus),Toast.LENGTH_SHORT).show();
      }
     });
  
  search.setOnQueryTextListener(new OnQueryTextListener() 
        {
       
      @Override
      public boolean onQueryTextSubmit(String query) {
       // TODO Auto-generated method stub
        
       return false;
      }
      
      @Override
      public boolean onQueryTextChange(String newText) {
       
        if (newText.length() > 3)
        {
         
         searchResults.setVisibility(myFragmentView.VISIBLE);
         myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);
        }
        else
        {
         
         searchResults.setVisibility(myFragmentView.INVISIBLE);
        }
       
       
               
       return false;
      }
      
     });
  return myFragmentView;
 }  
 
 //this filters products from productResults and copies to filteredProductResults based on search text 

 public void filterProductArray(String newText) 
 {

  String pName;
  
  filteredProductResults.clear();
  for (int i = 0; i < productResults.size(); i++)
  {
   pName = productResults.get(i).getProductName().toLowerCase();
   if ( pName.contains(newText.toLowerCase()) ||
     productResults.get(i).getProductBarcode().contains(newText))
   {
    filteredProductResults.add(productResults.get(i));

   }
  }
  
 }
 
 //in this myAsyncTask, we are fetching data from server for the search string entered by user.
 class myAsyncTask extends AsyncTask<String, Void, String> 
 {
  JSONParser jParser;
  JSONArray productList;
  String url=new String();
  String textSearch;
  ProgressDialog pd;


  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   productList=new JSONArray();
   jParser = new JSONParser();
   pd= new ProgressDialog(getActivity());
   pd.setCancelable(false);
   pd.setMessage("Searching...");
   pd.getWindow().setGravity(Gravity.CENTER);
   pd.show();
  }

  @Override
  protected String doInBackground(String... sText) {
   
   url="http://lawgo.in/lawgo/products/user/1/search/"+sText[0];
   String returnResult = getProductList(url);
   this.textSearch = sText[0];
   return returnResult;
   
  }

  public String getProductList(String url)
  {
   
   Product tempProduct = new Product();
   String matchFound = "N";
   //productResults is an arraylist with all product details for the search criteria
   //productResults.clear();
   
   
   try {
    
    
    JSONObject json = jParser.getJSONFromUrl(url);
    
    productList = json.getJSONArray("ProductList");
  
    //parse date for dateList 
    for(int i=0;i<productList.length();i++)
    {
     tempProduct = new Product();
     
     JSONObject obj=productList.getJSONObject(i);
     
     tempProduct.setProductCode(obj.getString("ProductCode"));
     tempProduct.setProductName(obj.getString("ProductName"));
     tempProduct.setProductGrammage(obj.getString("ProductGrammage"));
     tempProduct.setProductBarcode(obj.getString("ProductBarcode"));
     tempProduct.setProductDivision(obj.getString("ProductCatCode"));
     tempProduct.setProductDepartment(obj.getString("ProductSubCode"));
     tempProduct.setProductMRP(obj.getString("ProductMRP"));
     tempProduct.setProductBBPrice(obj.getString("ProductBBPrice"));
     
     //check if this product is already there in productResults, if yes, then don't add it again. 
     matchFound = "N";
     
     for (int j=0; j < productResults.size();j++)
     {
      
      if (productResults.get(j).getProductCode().equals(tempProduct.getProductCode()))
      {
       matchFound = "Y";    
      }
     }
     
     if (matchFound == "N")
     {
       productResults.add(tempProduct);
     }
     
    }
    
    return ("OK");
    
   } catch (Exception e) {
    e.printStackTrace();
    return ("Exception Caught");
   }
  }
  
  @Override
  protected void onPostExecute(String result) {

   super.onPostExecute(result);
   
   if(result.equalsIgnoreCase("Exception Caught"))
   {
    Toast.makeText(getActivity(), "Unable to connect to server,please try later", Toast.LENGTH_LONG).show();

    pd.dismiss();
   }
   else
   {
  
    
    //calling this method to filter the search results from productResults and move them to 
    //filteredProductResults
    filterProductArray(textSearch);
    searchResults.setAdapter(new SearchResultsAdapter(getActivity(),filteredProductResults));
    pd.dismiss();
   }
  }

 }
}

class SearchResultsAdapter extends BaseAdapter
{
  private LayoutInflater layoutInflater;
  
  private ArrayList<Product> productDetails=new ArrayList<Product>();
  int count;
  Typeface type;
  Context context;
  
  //constructor method
 public SearchResultsAdapter(Context context, ArrayList<Product> product_details) {
 
  layoutInflater = LayoutInflater.from(context);
 
   this.productDetails=product_details;
   this.count= product_details.size();
   this.context = context;
   type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
   
 }

 @Override
 public int getCount() {
  return count;
 }

 @Override
 public Object getItem(int arg0) {
  return productDetails.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
  return arg0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  
   ViewHolder holder;
      Product tempProduct = productDetails.get(position);
      
   if (convertView == null) 
      {
       convertView = layoutInflater.inflate(R.layout.listtwo_searchresults, null);
          holder = new ViewHolder();
          holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
          holder.product_mrp = (TextView) convertView.findViewById(R.id.product_mrp);
          holder.product_mrpvalue = (TextView) convertView.findViewById(R.id.product_mrpvalue);
          holder.product_bb = (TextView) convertView.findViewById(R.id.product_bb);
          holder.product_bbvalue = (TextView) convertView.findViewById(R.id.product_bbvalue);
          holder.addToCart = (Button) convertView.findViewById(R.id.add_cart);

    convertView.setTag(holder);
      } 
   else 
      {
             holder = (ViewHolder) convertView.getTag();
      }
  
       
   holder.product_name.setText(tempProduct.getProductName());
   holder.product_name.setTypeface(type);
      
   holder.product_mrp.setTypeface(type);
   
   holder.product_mrpvalue.setText(tempProduct.getProductMRP());
   holder.product_mrpvalue.setTypeface(type);
   
   holder.product_bb.setTypeface(type);
   
   holder.product_bbvalue.setText(tempProduct.getProductBBPrice());
   holder.product_bbvalue.setTypeface(type);
   
      return convertView;
 }
 
  static class ViewHolder 
  {         
         TextView product_name;
         TextView product_mrp;
         TextView product_mrpvalue;
         TextView product_bb;
         TextView product_bbvalue;
         TextView product_savings;
         TextView product_savingsvalue;
         TextView qty;
         TextView product_value;
         Button addToCart;
                
  }
    
}
 


First Android App | Step 8 | ZBar Barcode Scanning in Android Example

In this session, we are going to see how ZBar can be integrated an Android Application. ZBar is one of the most prominent Free Barcode Scanners available.

The following video explains to you step by step on how to implement the same.




Source Code

AndroidManifest.xml

 Add the following lines
Add another activity within

      <activity 
            android:label="@string/barcode" 
            android:launchmode="singleTop" 
            android:name="com.zing.basket.Barcode" 
            android:screenorientation="portrait" 
            android:theme="@android:style/Theme.Dialog">
        </activity>

Add the following permissions
    
    <uses-permission android:name="android.permission.CAMERA">
    <uses-feature android:name="android.hardware.camera">
    <uses-feature android:name="android.hardware.camera.autofocus">


In res/values/strings.xml, add the following line.

<string name="barcode">Barcode Scanning...</string>

Create a new file in src folder called Barcode.java

package com.zing.basket;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.widget.FrameLayout;

public class Barcode extends Activity{

 private Camera mCamera;
    private CameraPreview mPreview;
    private Handler autoFocusHandler;
    
    ImageScanner scanner;
    private boolean barcodeScanned = false;
    private boolean previewing = true;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        
        setContentView(R.layout.activity_barcode);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        //getActionBar().hide();
  
        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();
        // Instance barcode scanner
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 400);
        scanner.setConfig(0, Config.Y_DENSITY, 400);
        scanner.setConfig(0, Config.ENABLE, 0);
        scanner.setConfig(Symbol.EAN13, Config.ENABLE,1);
        scanner.setConfig(Symbol.EAN8, Config.ENABLE,1);
        scanner.setConfig(Symbol.UPCA, Config.ENABLE,1);
        scanner.setConfig(Symbol.UPCE, Config.ENABLE,1);
        
        mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
        FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
        preview.addView(mPreview);
        
        super.onCreate(savedInstanceState);
    }

    
    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance()
    {
        Camera c = null;
        try 
        {
            c = Camera.open();
        } catch (Exception e)
        {
         //nada
        }
        return c;
    }
 
    private void releaseCamera() 
    {
        if (mCamera != null) 
        {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

   
    
    PreviewCallback previewCb = new PreviewCallback() 
    {
        public void onPreviewFrame(byte[] data, Camera camera) 
        {
            Camera.Parameters parameters = camera.getParameters();
            Size size = parameters.getPreviewSize();
            Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);
            int result = scanner.scanImage(barcode);
            if (result != 0) 
            {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) 
                {
                    barcodeScanned = true;
                    
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("BARCODE", sym.getData());
                    setResult(1,returnIntent);
                    releaseCamera();
                    finish();
                }
            }
        }
    };
    
    // Mimic continuous auto-focusing
    AutoFocusCallback autoFocusCB = new AutoFocusCallback() 
    {
        public void onAutoFocus(boolean success, Camera camera) 
        {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };
    
    private Runnable doAutoFocus = new Runnable() 
    {
        public void run() 
        {
            if (previewing)
                mCamera.autoFocus(autoFocusCB);
        }
    };
    
    public void onPause() 
    {
        super.onPause();
        releaseCamera();
    }

    @Override
    public void onBackPressed() {
     
     releaseCamera();
     Intent intent = new Intent();
        intent.putExtra("BARCODE","NULL");
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    
}


Create a new file in src folder called CameraPreview.java

package com.zing.basket;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
{

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private PreviewCallback previewCallback;
    private AutoFocusCallback autoFocusCallback;
   
    
    public CameraPreview(Context context, Camera camera,PreviewCallback previewCb,AutoFocusCallback autoFocusCb) 
    {
        super(context);
        mCamera = camera;
        previewCallback = previewCb;
        autoFocusCallback = autoFocusCb;
 
        // Set camera to continuous focus if supported, otherwise use
        // software auto-focus. Only works for API level >=9.
 
        // Camera.Parameters parameters = camera.getParameters();
        // for (String f : parameters.getSupportedFocusModes()) {
        //     if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
        //         mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        //         autoFocusCallback = null;
        //         break;
        //     }
        // }
         
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.

        mHolder = getHolder();
        mHolder.addCallback(this);
 
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    } 
 
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try 
        {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d("DBG", "Error setting camera preview: " + e.getMessage());
        }
    }
 
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // Camera preview released in activity
    }
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        /*
         * If your preview can change or rotate, take care of those events here.
         * Make sure to stop the preview before resizing or reformatting it.
         */
        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }
        try {
            // Hard code camera surface rotation 90 degs to match Activity view in portrait
            mCamera.setDisplayOrientation(90);
 
            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(previewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(autoFocusCallback);
        } catch (Exception e){
            Log.d("DBG", "Error starting camera preview: " + e.getMessage());
        }
    }
}


Make the changes in Search.java.

1. Add 2 methods onActivityResult to receive the scanned barcode back from Barcode Activity
2. buttonBarcode.setOnClickListener to call the Barcode Activity.

package com.zing.basket;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;



public class Search extends Fragment
{
 View myFragmentView;
 SearchView search;
 ImageButton buttonBarcode;
 ImageButton buttonAudio;
 Typeface type;
 ListView searchResults;
 String found = "N";
 
 
 //This arraylist will have data as pulled from server. This will keep cumulating.
 ArrayList<Product> productResults = new ArrayList<Product>();
 //Based on the search string, only filtered products will be moved here from productResults
 ArrayList<Product> filteredProductResults = new ArrayList<Product>();
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
 }
 
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) 
 {
  //get the context of the HomeScreen Activity
  final HomeScreen activity = (HomeScreen) getActivity();
  
  //define a typeface for formatting text fields and listview. 
  
  type= Typeface.createFromAsset(activity.getAssets(),"fonts/book.TTF");
  myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);  
     
  search=(SearchView) myFragmentView.findViewById(R.id.searchView1);
        search.setQueryHint("Start typing to search...");
        
        search.setIconifiedByDefault(false);
        
  searchResults = (ListView) myFragmentView.findViewById(R.id.listview_search);
  buttonBarcode = (ImageButton) myFragmentView.findViewById(R.id.imageButton2);
  buttonAudio = (ImageButton) myFragmentView.findViewById(R.id.imageButton1);
  
  
  //this part of the code is to handle the situation when user enters any search criteria, how should the 
  //application behave?
  
  buttonBarcode.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
             startActivityForResult(new Intent(activity, Barcode.class),1); 
            }
        });
  
  search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() 
        {
      
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
       // TODO Auto-generated method stub
        
       //Toast.makeText(activity, String.valueOf(hasFocus),Toast.LENGTH_SHORT).show();
      }
     });
  
  search.setOnQueryTextListener(new OnQueryTextListener() 
        {
       
      @Override
      public boolean onQueryTextSubmit(String query) {
       // TODO Auto-generated method stub
        
       return false;
      }
      
      @Override
      public boolean onQueryTextChange(String newText) {
       
        if (newText.length() > 3)
        {
         
         searchResults.setVisibility(myFragmentView.VISIBLE);
         myAsyncTask m= (myAsyncTask) new myAsyncTask().execute(newText);
        }
        else
        {
         
         searchResults.setVisibility(myFragmentView.INVISIBLE);
        }
       
       
               
       return false;
      }
      
     });
  return myFragmentView;
 }  
 
 //this captures the result from barcode and populates in the searchView. 
 @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data)  
    {  
          
  //Log.d("arindam","insideOnActivityResult");
  //super.onActivityResult(requestCode, resultCode, data);    
        if(requestCode==1)  
        {  
         String barcode=data.getStringExtra("BARCODE");
         if (barcode.equals("NULL"))
         {
          //that means barcode could not be identified or user pressed the back button
          //do nothing 
         }
         else
         {
          search.setQuery(barcode, true);
          search.setIconifiedByDefault(false);
         }
        }
          
    } 
 
 //this filters products from productResults and copies to filteredProductResults based on search text 

 public void filterProductArray(String newText) 
 {

  String pName;
  
  filteredProductResults.clear();
  for (int i = 0; i < productResults.size(); i++)
  {
   pName = productResults.get(i).getProductName().toLowerCase();
   if ( pName.contains(newText.toLowerCase()) ||
     productResults.get(i).getProductBarcode().contains(newText))
   {
    filteredProductResults.add(productResults.get(i));

   }
  }
  
 }
 
 //in this myAsyncTask, we are fetching data from server for the search string entered by user.
 class myAsyncTask extends AsyncTask<String, Void, String> 
 {
  JSONParser jParser;
  JSONArray productList;
  String url=new String();
  String textSearch;
  ProgressDialog pd;


  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   productList=new JSONArray();
   jParser = new JSONParser();
   pd= new ProgressDialog(getActivity());
   pd.setCancelable(false);
   pd.setMessage("Searching...");
   pd.getWindow().setGravity(Gravity.CENTER);
   pd.show();
  }

  @Override
  protected String doInBackground(String... sText) {
   
   url="http://lawgo.in/lawgo/products/user/1/search/"+sText[0];
   String returnResult = getProductList(url);
   this.textSearch = sText[0];
   return returnResult;
   
  }

  public String getProductList(String url)
  {
   
   Product tempProduct = new Product();
   String matchFound = "N";
   //productResults is an arraylist with all product details for the search criteria
   //productResults.clear();
   
   
   try {
    
    
    JSONObject json = jParser.getJSONFromUrl(url);
    
    productList = json.getJSONArray("ProductList");
  
    //parse date for dateList 
    for(int i=0;i<productList.length();i++)
    {
     tempProduct = new Product();
     
     JSONObject obj=productList.getJSONObject(i);
     
     tempProduct.setProductCode(obj.getString("ProductCode"));
     tempProduct.setProductName(obj.getString("ProductName"));
     tempProduct.setProductGrammage(obj.getString("ProductGrammage"));
     tempProduct.setProductBarcode(obj.getString("ProductBarcode"));
     tempProduct.setProductDivision(obj.getString("ProductCatCode"));
     tempProduct.setProductDepartment(obj.getString("ProductSubCode"));
     tempProduct.setProductMRP(obj.getString("ProductMRP"));
     tempProduct.setProductBBPrice(obj.getString("ProductBBPrice"));
     
     //check if this product is already there in productResults, if yes, then don't add it again. 
     matchFound = "N";
     
     for (int j=0; j < productResults.size();j++)
     {
      
      if (productResults.get(j).getProductCode().equals(tempProduct.getProductCode()))
      {
       matchFound = "Y";    
      }
     }
     
     if (matchFound == "N")
     {
       productResults.add(tempProduct);
     }
     
    }
    
    return ("OK");
    
   } catch (Exception e) {
    e.printStackTrace();
    return ("Exception Caught");
   }
  }
  
  @Override
  protected void onPostExecute(String result) {

   super.onPostExecute(result);
   
   if(result.equalsIgnoreCase("Exception Caught"))
   {
    Toast.makeText(getActivity(), "Unable to connect to server,please try later", Toast.LENGTH_LONG).show();

    pd.dismiss();
   }
   else
   {
  
    
    //calling this method to filter the search results from productResults and move them to 
    //filteredProductResults
    filterProductArray(textSearch);
    searchResults.setAdapter(new SearchResultsAdapter(getActivity(),filteredProductResults));
    pd.dismiss();
   }
  }

 }
}

class SearchResultsAdapter extends BaseAdapter
{
  private LayoutInflater layoutInflater;
  
  private ArrayList<Product> productDetails=new ArrayList<Product>();
  int count;
  Typeface type;
  Context context;
  
  //constructor method
 public SearchResultsAdapter(Context context, ArrayList<Product> product_details) {
 
  layoutInflater = LayoutInflater.from(context);
 
   this.productDetails=product_details;
   this.count= product_details.size();
   this.context = context;
   type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
   
 }

 @Override
 public int getCount() {
  return count;
 }

 @Override
 public Object getItem(int arg0) {
  return productDetails.get(arg0);
 }

 @Override
 public long getItemId(int arg0) {
  return arg0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  
   ViewHolder holder;
      Product tempProduct = productDetails.get(position);
      
   if (convertView == null) 
      {
       convertView = layoutInflater.inflate(R.layout.listtwo_searchresults, null);
          holder = new ViewHolder();
          holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
          holder.product_mrp = (TextView) convertView.findViewById(R.id.product_mrp);
          holder.product_mrpvalue = (TextView) convertView.findViewById(R.id.product_mrpvalue);
          holder.product_bb = (TextView) convertView.findViewById(R.id.product_bb);
          holder.product_bbvalue = (TextView) convertView.findViewById(R.id.product_bbvalue);
          holder.addToCart = (Button) convertView.findViewById(R.id.add_cart);

    convertView.setTag(holder);
      } 
   else 
      {
             holder = (ViewHolder) convertView.getTag();
      }
  
       
   holder.product_name.setText(tempProduct.getProductName());
   holder.product_name.setTypeface(type);
      
   holder.product_mrp.setTypeface(type);
   
   holder.product_mrpvalue.setText(tempProduct.getProductMRP());
   holder.product_mrpvalue.setTypeface(type);
   
   holder.product_bb.setTypeface(type);
   
   holder.product_bbvalue.setText(tempProduct.getProductBBPrice());
   holder.product_bbvalue.setTypeface(type);
   
      return convertView;
 }
 
  static class ViewHolder 
  {         
         TextView product_name;
         TextView product_mrp;
         TextView product_mrpvalue;
         TextView product_bb;
         TextView product_bbvalue;
         TextView product_savings;
         TextView product_savingsvalue;
         TextView qty;
         TextView product_value;
         Button addToCart;
         
         
  }
  
  
  
}

 


Download this zip file and place the contents in the /libs folder of your project.

Download zbar zip file