Pages

Tuesday, 17 April 2012

Learning Content Provider

Following code show how to read contacts and sms from our phone.


package com.myphonebook;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MyPhoneBookActivity extends Activity implements OnClickListener {
Button btnPhone,btnSMS;
ListView lv;
SimpleCursorAdapter adp;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnSMS = (Button) findViewById(R.id.btnSMS);
       
        btnPhone = (Button) findViewById(R.id.btnPhone);
        btnPhone.setOnClickListener(this);
        btnSMS.setOnClickListener(this);
    }
@Override
public void onClick(View v) {

if(v==btnPhone)
{
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cur.getCount()>0)
{
while(cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0)
{
Cursor c = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while(c.moveToNext())
{
String phone = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("My Phone Book", " Name : "+name + " Phone : "+phone);
}
}
}
}


}
if(v==btnSMS)
{
Cursor c =  getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
String[] nm = new String[]{"body"};
int[] id = new int[]{R.id.lblMsg};

adp = new SimpleCursorAdapter(this, R.layout.row, c, nm, id);
lv.setAdapter(adp);
}
}
}

Expandable Listview In Andrpod

This may help u in your apps.it show expandable listview of android.



package com.expandablelistview;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class ExpandablelistviewActivity extends ExpandableListActivity
{
 /**
  * strings for group elements
  */
    static final String arrGroupelements[] =
    {
   "India",
   "Australia",
   "England",
   "South Africa"
 };

    /**
  * strings for child elements
  */
 static final String arrChildelements[][] =
 {
   {
  "Sachin Tendulkar",
  "Raina",
  "Dhoni",
  "Yuvi"
   },
   {
  "Ponting",
  "Adam Gilchrist",
  "Michael Clarke"
   },
   {
  "Andrew Strauss",
  "kevin Peterson",
  "Nasser Hussain"
   },
   {
  "Graeme Smith",
  "AB de villiers",
  "Jacques Kallis"
   }
    };

 DisplayMetrics metrics;
 int width;
 ExpandableListView expList;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        expList = getExpandableListView();
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        //this code for adjusting the group indicator into right side of the view
        expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
        expList.setAdapter(new ExpAdapter(this));
 
  expList.setOnGroupExpandListener(new OnGroupExpandListener()
  {
   public void onGroupExpand(int groupPosition)
   {
    Log.e("onGroupExpand", "OK");
   }
  });
 
  expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
  {
   public void onGroupCollapse(int groupPosition)
   {
    Log.e("onGroupCollapse", "OK");
   }
  });
 
  expList.setOnChildClickListener(new OnChildClickListener()
  {
   public boolean onChildClick(ExpandableListView parent, View v,
     int groupPosition, int childPosition, long id) {
    Log.e("OnChildClickListener", "OK");
    return false;
   }
  });
    }
 
    public int GetDipsFromPixel(float pixels)
    {
     // Get the screen's density scale
     final float scale = getResources().getDisplayMetrics().density;
     // Convert the dps to pixels, based on density scale
     return (int) (pixels * scale + 0.5f);
    }
}

Display All Folder Of Sdcard

This example display all folder of sdcard in listview.see this code..




package com.getallfolder;

import java.io.File;
import java.util.ArrayList;

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

public class GetallfolderActivity extends Activity
{
ArrayList<String> path = new ArrayList<String>();
ArrayList<String> item = new ArrayList<String>();
ListView lst;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        lst = (ListView)findViewById(R.id.listView1);
        File mfile=new File("/sdcard/");
        File[] list=mfile.listFiles();
     
            for(int i=0; i < list.length; i++)
            {
              File file = list[i];
              path.add(file.getPath());
              if(file.isDirectory())
                   item.add(file.getName()+"/");
              else
              item.add(file.getName());
            }
           
           lst.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,item));
    }
}

Custom Spinner Example

This example shows how to display spinner item with image or what u want.



package com.customspinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class CustomspinnerActivity extends Activity {
   
    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
            "Wednesday", "Thursday", "Friday", "Saturday"};
   
    Integer[] image = {
                 R.drawable.one,
                R.drawable.two,
                R.drawable.three,
                R.drawable.four,
                R.drawable.five,
                R.drawable.six,
                R.drawable.seven,
                R.drawable.eight };
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyCustomAdapter(CustomspinnerActivity.this, R.layout.row, DayOfWeek,image));
    }
   
    public class MyCustomAdapter extends ArrayAdapter<String>{

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects, Integer[] image) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
   
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.weekofday);
            label.setText(DayOfWeek[position]);
           
            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            icon.setImageResource(image[position]);
            return row;
        }  
    }
}

Thursday, 12 April 2012

Great Insult:

Girl: Meri ek ek saans per ek ek ladka marta hai...
Boy: Fir tum....











Brush kyu nhi karti...=))



Life Me Kabhi Kisiko Vishvas Dilane ki Zarurat nathi hoti,
Kyuki
"Dushman Kabhi Yakin Nahi Karega" OR
"Dost Kabhi Saq Nahi Karega".