java - Rounded QuickContactBadge with text -
i have listview
quickcontactbadge
, contact's name. problem badge square, , if contact not have image, blank. want circular picture or first initial of contacts in android lollipop:
i tried solution this question, couldn't work. maybe implemented wrong? have:
contactstab.java:
public class contactstab extends listfragment implements loadercallbacks<cursor> { private customadapter madapter; private cursor mcursor; private string currentquery; private int midcolumn; private int mlookupkeycolumn; private uri mcontacturi; private quickcontactbadge mbadge; private static final string[] projection = { contacts._id, contacts.display_name_primary, contacts.lookup_key, contacts.photo_thumbnail_uri, contacts.has_phone_number, }; private static final string[] = { contacts.photo_thumbnail_uri, contacts.display_name_primary }; private static final int[] = { r.id.badge, r.id.contact_text }; private static final string selection = "(" + contacts.in_visible_group + " = 1) , (" + contacts.has_phone_number + " != 0 )"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); madapter = new customadapter(getactivity(), r.layout.contacts_list_item, null, from, to, 0) } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); sethasoptionsmenu(true); setlistadapter(madapter); getloadermanager().initloader(0, null, this); getlistview().setdivider(null); getlistview().setdividerheight(0); } @override public loader<cursor> oncreateloader(int id, bundle args) { uri contenturi = contacts.content_uri; return new cursorloader(getactivity(), contenturi, projection, selection, null, contacts.display_name_primary + " asc"); } @override public void onloadfinished(loader<cursor> loader, cursor data) { madapter.swapcursor(data); } @override public void onloaderreset(loader<cursor> loader) { madapter.swapcursor(null); } }
customadapter.java:
public class customadapter extends simplecursoradapter { private cursor _cursor; private context _context; private string[] _from; private int[] _to; private quickcontactbadge badge; private string lookupkey; public customadapter(context context, int layout, cursor c, string[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); _cursor = c; _context = context; _from = from; _to = to; } @override public void bindview(view view, context context, cursor cursor) { int id = cursor.getcolumnindex(contactscontract.data._id); lookupkey = cursor.getstring(cursor.getcolumnindex(contactscontract.data.lookup_key)); badge = (quickcontactbadge) view.findviewbyid(r.id.badge); badge.assigncontacturi(contactscontract.contacts.getlookupuri(id, lookupkey)); badge.setimagebitmap(generatecirclebitmap(r.color.accent, 42, contactscontract.contacts.display_name_primary.substring(0,1))); } public static bitmap generatecirclebitmap(int circlecolor, float diameterdp, string text){ final int textcolor = 0xffffffff; displaymetrics metrics = resources.getsystem().getdisplaymetrics(); float diameterpixels = diameterdp * (metrics.densitydpi / 160f); float radiuspixels = diameterpixels/2; // create bitmap bitmap output = bitmap.createbitmap((int) diameterpixels, (int) diameterpixels, bitmap.config.argb_8888); // create canvas draw on canvas canvas = new canvas(output); canvas.drawargb(0, 0, 0, 0); // draw circle final paint paintc = new paint(); paintc.setantialias(true); paintc.setcolor(circlecolor); canvas.drawcircle(radiuspixels, radiuspixels, radiuspixels, paintc); // draw text if (text != null && text.length() > 0) { final paint paintt = new paint(); paintt.setcolor(textcolor); paintt.setantialias(true); paintt.settextsize(radiuspixels * 2); paintt.settypeface(typeface.sans_serif); final rect textbounds = new rect(); paintt.gettextbounds(text, 0, text.length(), textbounds); canvas.drawtext(text, radiuspixels - textbounds.exactcenterx(), radiuspixels - textbounds.exactcentery(), paintt); } return output; } }
create custom drawable , can it
file : drawable/contact_badge_round.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="32dp" android:height="32dp"/> <gradient android:centercolor="#123456" android:startcolor="#123456" android:endcolor="#123456" /> </shape>
then in layout create button , set background contact_badge_round
<button android:id="@+id/roundcontact" android:layout_width="32dp" android:layout_height="32dp" android:text="a" android:gravity="center" android:background="@drawable/background_new_entity_symbol" />
done cheers!
Comments
Post a Comment