Thursday, February 23, 2012

Expense Manager

Cool App ... Check it out from Android Market


The Expense Manager helps you to organize your expenses in a proper way. This app reduces the overhead of maintaining our expenses in a diary and tracking at the end of the month. Using this app we can track the expenses based on different options like By Category, Date, Month, year and Payment Mode in a simple way.



Available in Android Market








Monday, October 3, 2011

PopUpWindow in Android

Android supports Popup Window like in IPhone. The difference between pop up window and dialog is we have to set the position (like center, top, bottom...) and also x and y positions. The below snippets explains the way.
 Home.java:
package com.popup.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class Home extends Activity
{
    private Button btnShowPopUp;
    private PopupWindow mpopup;
       
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnShowPopUp = (Button)findViewById(R.id.btnShowPopUp);
       
        btnShowPopUp.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View arg0)
            {
                View popUpView = getLayoutInflater().inflate(R.layout.popup, null); // inflating popup layout
                mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup
                mpopup.setAnimationStyle(android.R.style.Animation_Dialog);  
                mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);    // Displaying popup
               
                Button btnOk = (Button)popUpView.findViewById(R.id.btnOk);
                btnOk.setOnClickListener(new OnClickListener()
                {                   
                    @Override
                    public void onClick(View v)
                    {
                        mpopup.dismiss();  //dismissing the popup
                    }
                });
               
                Button btnCancel = (Button)popUpView.findViewById(R.id.btnCancel);
                btnCancel.setOnClickListener(new OnClickListener()
                {                   
                    @Override
                    public void onClick(View v)
                    {
                        mpopup.dismiss(); dismissing the popup
                    }
                });
            }
        }); 
    }
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/btnShowPopUp"
        android:text="ShowPopUp"
        android:layout_height="wrap_content">
    </Button>
  
</LinearLayout>
popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:background="#90FFFFFF"
  android:layout_height="wrap_content">
 
  <TextView
      android:layout_width="fill_parent"
      android:text="PopUp Window"
      android:textColor="#000000"
      android:gravity="center"
      android:textStyle="bold"
      android:layout_height="wrap_content">
  </TextView>
 
  <Button
      android:layout_width="fill_parent"
      android:id="@+id/btnOk"
      android:text="Ok"
      android:layout_height="wrap_content">
  </Button>
 
  <Button
      android:layout_width="fill_parent"
      android:id="@+id/btnCancel"
      android:text="Cancel"
      android:layout_height="wrap_content">
  </Button>
 
</LinearLayout>

Sunday, September 18, 2011

Showing Buttons on either side of a layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="#FFFFFF"
    android:layout_height="fill_parent">
   
    <!-- Header -->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:background="@drawable/top_header"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:layout_height="50dip">
       
        <Button
            android:layout_width="wrap_content"
            android:background="@drawable/back_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content">
        </Button>
       
        <Button
            android:layout_width="wrap_content"
            android:background="@drawable/cancle_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content">
        </Button>
       
    </RelativeLayout>
   
    <!-- Body -->
     
</LinearLayout>

Friday, September 16, 2011

Three Level Expandable list

The below snippet helps to implement three level list in android.
First Group
--Sub Group
   ---   Child1
   ---   Child 2
   ---   Child 3
in this pattern.....
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    <ExpandableListView
    android:layout_width="fill_parent"
    android:id="@+id/ParentLevel"
    android:groupIndicator="@null"
    android:layout_height="fill_parent">
    </ExpandableListView>

</LinearLayout>

Home.java
package com.threeelevellist.activities;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Home extends Activity 
{
    ExpandableListView explvlist;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
        explvlist.setAdapter(new ParentLevel());
        
    }
    
    public class ParentLevel extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int arg0, int arg1) 
{
return arg1;
}

@Override
public long getChildId(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 3;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 5;
}

@Override
public long getGroupId(int groupPosition) 
{
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("->FirstLevel");
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}

@Override
public boolean hasStableIds() 
{
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
{
return true;
}    
    }
    
    public class CustExpListview extends ExpandableListView
    {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) 
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
    }
    
    public class SecondLevelAdapter extends BaseExpandableListAdapter
    {

@Override
public Object getChild(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) 
{
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}

@Override
public int getChildrenCount(int groupPosition) 
{
return 5;
}

@Override
public Object getGroup(int groupPosition) 
{
return groupPosition;
}

@Override
public int getGroupCount() 
{
return 1;
}

@Override
public long getGroupId(int groupPosition) 
{
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) 
{
TextView tv = new TextView(Home.this);
tv.setText("-->Second Level");
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.RED);
return tv;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
   
    }
}

Route Map in Android

The following snippet shows the route between two Geo points in google map using default maps application.

Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=slat,slon&daddr=dlat,dlon");

Here saddr -> source address
        daddr -> destination address
        slat    -> source latitude
        slon   -> source longitude
        dlat   -> destination latitude
        dlon  -> destination longitude

 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);
       

Thursday, September 8, 2011

InputStream to String

public String convertBrToString(InputStream in)
{
       
        BufferedReader br;
       
        StringBuffer outString = new StringBuffer();
       
        br = new BufferedReader(new InputStreamReader(in));
       
        String read = br.readLine();
       
        while(read != null)
        {
            outString.append(read);
            read =br.readLine();
        }
       
        return outString.toString();     
}

Thursday, August 25, 2011

Converting Pixel To Dip

The below snippet converts pixel to dip(Device Independent Pixel) . We have to pass pixel value as an argument then it returns dip value for that.

public int getDip(int pixel)
{
        float scale = getBaseContext().getResources().getDisplayMetrics().density;
        return (int) (pixel * scale + 0.5f);
 }

Wednesday, August 24, 2011

Maps on Android

      This Tutorial helps to intigrate google maps to Application. Default android package did not provide google maps library. So we have to add exteranlly or it can be downloadable with android sdk it self.

Step 1: (If you already did this then ignore this step)
Adding Google Maps library to Android SDK :
In Eclipse : Window -> Android Sdk and AVD Manager
Then the following window will appear.
In that window Select Available Packages
Click On Third Party Add-ons. Then you will find a list like below.

In that select Google Inc. In that select Google Api with the versions that you want and click on Install selected. Then It will install.
Step 2:
In order to display google maps in you app you have to genarate a MAPKEY .
Obtaining Map Key:
Generating MD5 FingerPrint for your system:
  • First we have to find default keystore existing in the system.
  • Go to C: ->  users -> Your User Name -> .android
  • There you find debug.keystore. Copy that keystore by creating a folder in c drive namely keystore.
  • Open Command Prompt and follow the image.


Then you get MD5 Finger print.

Registering Finger Print certificate with the Google Maps service:
Then you will get Map Key.

Step 3:
Adding Map Key to Application
If you want add maps in xml do like this

<com.google.android.maps.MapView
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="ApiKey"/>

Through coding then do like this
mMapView = new MapView(this, "ApiKey");

Step 4:
Change the Manifast.xml
<application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
       < uses-library android:name="com.google.android.maps" />
      <activity
           .................
      </activity>
</application>

For detail information about google maps go to http://code.google.com/android/add-ons/google-apis/mapkey.html

Step 5:

Displying Google Map
manifast.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.googlmaps.activities"
    android:versionCode="1"
    android:versionName="1.0">

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
       
        <uses-library android:name="com.google.android.maps" />       
       
        <activity
            android:name=".Home"
            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>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
main.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="0QpCkU-U_D--eTiGs8j5jCQ8LnJpyBgyoo7ilqQ"/>

Home.java:
package com.googlmaps.activities;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class Home extends MapActivity
{  
    private MapView mapView;
    @Override   
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //Getting Id's
        mapView = (MapView)findViewById(R.id.MapView);
       
        //Getting Zoom Buttons
        mapView.setBuiltInZoomControls(true);
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }
}

Output:
Step 6:
Adding Overlay Items
In order to add overlays on the map, create another class with the name as MapOverlays with the following options.
While creation mention super class as "com.google.android.maps.ItemizedOverlay"
MapOverlay.java:
package com.googlmaps.activities;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class MapOverlays extends ItemizedOverlay
{
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
   
    public MapOverlays(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
    }

    @Override
    protected OverlayItem createItem(int arg0)
    {
        return mOverlays.get(arg0);
    }

    @Override
    public int size()
    {
        return mOverlays.size();
    }
   
    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();
    }
}

For the Home.java add these steps:

List<Overlay> mapOverlays;
Drawable drawable;
MapOverlays itemizedoverlay;
MapController mc;

//Creating Overlay Items
mapOverlays = mapView.getOverlays();
//Overlay marker
drawable = this.getResources().getDrawable(R.drawable.icon);
itemizedoverlay = new MapOverlays(drawable, Home.this);      

//Creation of Geo Point
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "", "");
       
mc.setZoom(10);
mc.animateTo(point);
       
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);

Output:

Monday, August 22, 2011

Multiple Choice List Android

In Android List View provides an Option to allow Multiple selection of items. The following example shows the way to achieve this.
Example:


package com.lvcheck.activities;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Home extends Activity
{
    private ListView lvCheckBox;
    private Button btnCheckAll, btnClearALl;
    private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};
    ArrayList arrList;
       
     @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        arrList = new ArrayList();
       
        btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
        btnClearALl = (Button)findViewById(R.id.btnClearAll);
       
        lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
        lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvCheckBox.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, arr));
       
        lvCheckBox.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
            {
                if(arrList.contains(arg2))
                {
                    arrList.remove((Integer)arg2);
                }
                else
                {
                    arrList.add(arg2);
                }
               
                Collections.sort(arrList);
                String strText = "";
               
                for(int i=0 ; i
arrList.size(); i++)                   
                       strText += arrList.get(i) + ",";
               
               
                Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();               
            }
           
        });
       
        btnCheckAll.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View arg0)
            {
                arrList.clear();
                               

                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, true);
                    arrList.add(i);
                }
               
                String strText = "";
              
for(int i=0 ; i<arrList.size(); i++)   
                     strText += arrList.get(i) + ","
                Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
            }
        });
       
        btnClearALl.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View v)
            {
                arrList.clear();
                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, false);
                }
               
                String strText = "";
              for(int i=0 ; i<arrList.size(); i++)

                  strText += arrList.get(i) + ",";
               
                Toast.makeText(Home.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
            }
        });
    }    
}


Click Here To Download Complete Source

Tuesday, August 16, 2011

How To Move Applications to SDCARD in Android

From Android 2.2 Froyo onwards we can allow the application has to be installed on the external storage(SDCARD)

This feature is enabled by using this tag android:installLocation. By declaring this permission in the android manifast file.

If we do not declare this attribute, the application will be installed on the internal storage only and it cannot be moved to the external storage.

To allow the system to install your application on the external storage, modify the manifest file to include the android:installLocation attribute in the <manifest> element, with a value of either "preferExternal" or "auto". For example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="preferExternal" or android:installLocation="auto"
    ... >


preferExternal : The App installs on the sdcard.

auto : There is no preference. The system will decide where to install based on several Factors.

However,  if your application is designed to support an API Level lower than 8, you can choose to support this feature for devices with API Level 8 or greater and still be compatible with devices using an API Level lower than 8.

To allow installation on external storage and remain compatible with versions lower than API Level 8:
  • Include the android:installLocation attribute with a value of "auto" or   "preferExternal" in the <manifest> element.
  • Leave your android:minSdkVersion attribute as is (something less than "8") and be certain that your application code uses only APIs compatible with that.
  • In order to compile your application, change your build target to API Level 8 (In default.properties). This is necessary because older Android libraries don't understand the android:installLocation attribute and will not compile your application when it's present.

Note: For More Information http://developer.android.com/guide/appendix/install-location.html