Thursday, February 16, 2012

Tab with ListView

In this sample we are going to create an application which includes two tabs under with two custom listview for each tabs.

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TabHost;

public class TabListActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);
     
        TabHost mTabHost = getTabHost();
     
        final ListView one=(ListView)findViewById(R.id.listOne);
        final ListView two=(ListView)findViewById(R.id.listTwo);
     
        String[] listItemsone = new String[] {"one","two","three"};
        String[] listItemstwo = new String[] {"A","B","C"};
     
//Typed array which contains array values be stored in an separate xml file
        TypedArray itemsOneIcons = getResources().obtainTypedArray(R.array.listOneicons);
        TypedArray itemsTwoIcons = getResources().obtainTypedArray(R.array.listTwoicons);
       
         one.setAdapter(new ImageAndTextAdapter(getApplicationContext(), R.layout.listrow, listItemsone, itemsOneIcons));
         two.setAdapter(new ImageAndTextAdapter(getApplicationContext(), R.layout.listrow, listItemstwo, itemsTwoIcons));
     
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Tab1").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Tab ").setContent(R.id.tab2));
     
        //Used to reduce the height of the Tabs
        mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height /=2;
        mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height /=2;   
  
        mTabHost.setCurrentTab(0);   
         one.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
           //Used to get selected Item
              String selectedFromList = (String) ( one.getItemAtPosition(myItemInt));           
             //Navigation
              Intent  selectedItem  = new Intent (CurrentActivity, Navigation Activity);
              //Sending value to next screen
               selectedItem.putExtra("Choice", selectedFromList);
              startActivity( selectedItem);
            }              
      }
        );     
         two.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {

                //Used to get selected Item
              String selectedFromList = (String) ( two.getItemAtPosition(myItemInt));
                //Navigation
               Intent  selectedItem  = new Intent (CurrentActivity, Navigation Activity);
              //Sending value to next screen
               selectedItem.putExtra("Choice", selectedFromList);
              startActivity( selectedItem);
            }              
      }
        );
    }
}

Custom Image and Text List adapter

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAndTextAdapter extends ArrayAdapter<String> {

private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;

public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);

mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);

mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}

@Override
public int getCount() {
return mStrings.length;
}

@Override
public String getItem(int position) {
return mStrings[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);

ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));

TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);

return convertView;
}
}

homescreen.xml file

<?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:orientation="vertical"
    android:background="@drawable/quickkal" >
 
 <RelativeLayout
        android:id="@+id/home_header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/background_gradient"
         >
        <Button
            android:id="@+id/map_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"          
            android:padding="5dp"
            android:text="Map"
            android:textColor="#fff"
            android:background="@drawable/btn_custom" />     
        <ImageView
      android:id="@+id/maestro_header"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
            android:layout_margin="5dp"
            android:src="@drawable/header"
      />
 </RelativeLayout>


<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dip"
    android:foreground="@null">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:tabStripEnabled="false"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp">
         
            <ListView android:id="@+id/animals"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:background="#000000"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

        <ListView android:id="@+id/birds"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:background="#000000"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>
       
        </FrameLayout>
    </LinearLayout>
</TabHost>

</LinearLayout>

Icons.xml file ------- Used to store typed array


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

<resources>
    <array name=" listTwoicons">
        <item>@drawable/icon</item>
        <item>@drawable/icon</item>
        <item>@drawable/icon</item>
     
    </array>
    <array name=" listOneicons">
        <item>@drawable/icon</item>
        <item>@drawable/icon</item>
        <item>@drawable/icon</item>
     
    </array>
</resources>

listrow.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
  android:id="@+id/option_text"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dp"
   android:textSize="16dp" >
</TextView>
</LinearLayout>

Monday, February 13, 2012

Plist Android

Plist in Android:
             Plist is the property list files extension for MAC, iPhone OS programming frame works which has serialized objects that has been stored in it. Hence it is especially for iPhone application only, here we are going to use that files in our Android Application.The following is the sample application which used to read and display plist file values.

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;

public class PlistSample extends Activity {
    /** Called when the activity is first created. */
    int i=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView plistValues = (TextView)findViewById(R.id.TextView01);
        String plistXmlContent;
        try
        {
             plistXmlContent = getValuesFromAnPlistXML(this);
            myXmlContent.setText(stringXmlContent);
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {   
            e.printStackTrace();
        }
    }
    private String  getValuesFromAnPlistXML (Activity activity) throws XmlPullParserException,IOException
    {
        StringBuffer stringBuffer = new StringBuffer();
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.layout.plist);
        xpp.next();
        int eventType = xpp.getEventType();
        int flag1 = 0, flag2 = 0, flag3 = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {       
            if(eventType == XmlPullParser.START_TAG)
            {           
                String mainTag = xpp.getName().toString();
                if(mainTag.equals("dict"))
                {
                    if(flag1==0)
                    {
                    flag1 = 1;
                    i=i+1;
                    stringBuffer.append("\n Dict Tag Starts Number := "+i+"\n");
                    }
                    else
                    {
                        stringBuffer.append("Inner Dict Starts\n");
                        flag2 = 1;
                    }
                }           
                else if(mainTag.equals("key"))
                {
                    flag3 = 1;
                }           
                else
                {
                    flag3 = 2;
                }
            }         
            else if(eventType == XmlPullParser.END_TAG)
            {
                String g = xpp.getName().toString();           
                if(g.equals("dict"))
                {
                    if(flag1==1 && flag2!=1)
                    {               
                    stringBuffer.append("\n Main Dicts Ends\n");
                    flag1=0;
                    }
                    else
                    {
                        stringBuffer.append("\nInner Dicts Ends\n");
                        flag2=0;
                    }
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                if(flag3 == 1)
                {
                    stringBuffer.append("\nkey:="+xpp.getText());
                    flag3 = 0;
                }
                else if(flag3 == 2)
                {
                    stringBuffer.append("\nValue:="+xpp.getText());
                    flag3 = 0;
                }
            }         
           eventType = xpp.next();
        }
        stringBuffer.append("\n--- End XML ---");
        stringBuffer.append("\n--- Total Number Of Dictionaries Are ---"+i);     
        return stringBuffer.toString();
    }
}

Create an XML file named plist.XML and copy the following plist code.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.google.keystone.user.agent</string>
        <key>LimitLoadToSessionType</key>
        <string>Aqua</string>
        <key>ProgramArguments</key>
        <array>
          <string>/Users/al//Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Resources/GoogleSoftwareUpdateAgent.app/Contents/MacOS/GoogleSoftwareUpdateAgent</string>
          <string>-runMode</string>
          <string>ifneeded</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>3523</integer>
        <key>StandardErrorPath</key>
        <string>/dev/null</string>
        <key>StandardOutPath</key>
        <string>/dev/null</string>
</dict>
<dict>
        <key>label</key>
        <string>com.devdaily.pingwebsites</string>

        <key>ProgramArguments</key>
        <array>
                <string>/Users/al/bin/crontab-test.sh</string>
        </array>
        <key>OnDemand</key>
        <false/>
        <key>Nice</key>
        <integer>1</integer>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>StandardErrorPath</key>
        <string>/tmp/AlTest1.err</string>
        <key>StandardOutPath</key>
        <string>/tmp/AlTest1.out</string>
</dict>
</plist> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

Friday, February 3, 2012

Flurry Agent

When we launch our applications we want to know about our application in current market that how it works, users comment, error reports,etc... Its a kinda getting feedback from users how they are evaluating our applications.It can be done with the help of Flurry.
Flurry ( actually Flurry Analytics ) gives you a lot more statistics on the users and on how they use your application.


Integration of Flurry:
  • Go to flurry.com and register for your app, which will generate a unique tracking code.
  • Download and add the FlurryAgent jar to your project libraries. If you're using Eclipse, right-click your project folder, select properties, select Java Build Path, and choose Add External JARs
  • Add android.permission.INTERNET to your AndroidManifest.xml (Required)  --  Required to send analytic data back to the flurry servers.
  • Set android.permission.ACCESS_COARSE_LOCATION orandroid.permission.ACCESS_FINE_LOCATION (optional) – If your application has location permissions, analytics will track where your application is being used. Without this, only country level location information will be available. (To disable detailed location reporting even when your app has permission, call FlurryAgent.setReportLocation(false) before calling FlurryAgent.onStartSession() and no detailed location information will be sent.)                                                                  
  •  Add a call to the Flurry agent from the onStart() and onStop methods of your activities.