Monday, November 7, 2011

Sample CheckedTextView in listview , Reading contacts all emails

Here, you can find the example of checked textview in the listview and read all email address in the android phone

1. Design the main Layout & row Layout with Checked TextView
Main.xml
< ListView android:id="@+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff" />

Row.xml

< CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView1" android:paddingLeft="20dip"
android:paddingRight="20dip" android:paddingTop="10dip"
android:paddingBottom="10dip" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background"
android:textColor="#000000" />

2. Create the CursurAdpater

private class ContactListAdapter extends CursorAdapter {

public ContactListAdapter(Context context, Cursor c) {
super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
String o = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
CheckedTextView tt = (CheckedTextView) view
.findViewById(R.id.checkedTextView1);
tt.setText(o);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
return v;
}
}

3. set the Contact Cursor to the ContactListAdapter
ContentResolver cr = getContentResolver();
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,
null, null);
l.setAdapter(new ContactListAdapter(this, emails));



4. Change the State while select the Item
l.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView arg0, View v, int arg2,
long arg3) {

CheckedTextView tt = (CheckedTextView) v
.findViewById(R.id.checkedTextView1);
if (!tt.isChecked()) {
tt.setChecked(true);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
} else {
tt.setChecked(false);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}

}
});

5. Add the Contact Read Permission in androidmanifest.xml
< uses-permission android:name="android.permission.READ_CONTACTS" />


Sample Program :

public class ContactListingActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView l = (ListView) findViewById(R.id.listView1);
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null,
null, null);
l.setAdapter(new ContactListAdapter(this, emails));
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView arg0, View v, int arg2,
long arg3) {
CheckedTextView tt = (CheckedTextView) v
.findViewById(R.id.checkedTextView1);
if (!tt.isChecked()) {
tt.setChecked(true);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
} else {
tt.setChecked(false);
tt.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}

}
});

}

private class ContactListAdapter extends CursorAdapter {
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
String o = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
CheckedTextView tt = (CheckedTextView) view
.findViewById(R.id.checkedTextView1);
tt.setText(o);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
return v;
}
}
}



7 comments:

  1. Hey thanks for sharing this program i bookmarked this and will surely try this. Android Development

    ReplyDelete
  2. Dear Sir

    can give me the sample source code ?

    i can not re-do this sample .

    THX

    ReplyDelete
  3. Blazing Star softech team is known for the skills, made for creativity and innovation and provides the best software solution ever.

    Software Development Company in Lucknow
    Web Development Company in Lucknow
    Software Company in Lucknow

    ReplyDelete
  4. Excellent article. Thanks for this kind of article.
    Download Latest Movie

    ReplyDelete
  5. Its a great and very helpful article for me! Thanks for sharing it!

    Hire Magento developer from eCommerce Web Development Company

    ReplyDelete
  6. Thanks for sharing good post. Appconsultio offers ongoing maintenance and support services to keep the apps up-to-date and functioning smoothly as new versions of the Android OS are released or as the app's requirements evolve. Our Mobile Application Developers in Pune have crafted an innovative app that is revolutionizing the way people interact with technology.

    ReplyDelete