Wednesday, November 16, 2011

Hide showed Toast Messages

In my App, there is a list, when a user clicks on an button, a toast message is being displayed, 10 items - 10 toast messages, so if the user clicks 10 times, then presses the button... he/she has to wait for some seconds, until he's able to read the menu option text.

I have found the below solution to hide the shows toast message.

public class SampleToastActivity extends Activity {
Toast myToast;
static int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
if(myToast!=null){
myToast.cancel();
myToast.setText("Count"+i);
}else{
myToast = Toast.makeText(SampleToastActivity.this, "Sample"+i, 10);
}
myToast.show();
}
});
}
}

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;
}
}
}