Pages

January 8, 2010

Andoroid : View Contact Photo & Detail

  /* Find the Contact photo and details from Android databse
   * for given input phone number
   * and display it on screen
   **/

  Class Variables:
    SimpleCursorAdapter myAdapter;
    private EditText edittext;
    private Button find_bt;
    private String prsn_name,ph_no;
    Collator mc;
    private long id;
    private TextView phone_number, contact_name;
  
    onStart() Method: 
        super.onStart();
        prsn_name=null;
        ph_no=null;
        id=0L;
      
        edittext=(EditText)findViewById(R.id.edittext);
        phone_number=(TextView)findViewById(R.id.phone_number);
        contact_name=(TextView)findViewById(R.id.contact_name);
        find_bt=(Button)findViewById(R.id.ok);
        find_bt.setOnClickListener(this);
        mc=Collator.getInstance();
       
  
     onClick Button Listener :
         /* find person details from contact databse for a given number */
       
         prsn_name=edittext.getText().toString();
         Cursor c= getContentResolver().query(People.CONTENT_URI, null,null,null,null);
       
        while(c.moveToNext())
        {
       
         String name=c.getString(c.getColumnIndexOrThrow(People.NAME));
        
         if(mc.equals(prsn_name,name))
         {
           
             id=c.getLong(c.getColumnIndexOrThrow(People.PRIMARY_PHONE_ID));
           
             ph_no=c.getString(c.getColumnIndexOrThrow(People.NUMBER));
           
             contact_name.setText(ph_no);
           
             ImageView im= (ImageView)findViewById(R.id.contact_image);
           
             Uri uri=ContentUris.withAppendedId(People.CONTENT_URI, id);

        /* retrive contact image  into Bitmap */

             Bitmap bitmap=People.loadContactPhoto(this, uri, R.drawable.icon, null);  
           
             im.setImageBitmap(bitmap);                     /* set image for display */               
           
             break;
           }
         }
  
        Happy Android Coding !!!