Reputation: 7051
So I have this issue in a couple of parts of my app, but I'll stick to one:
I have a preference screen that updates font size/typeface across the app. It works wonderfully except for on my contact picker. This is because of the way I'm creating it:
private void populateContactList() {
Cursor cursor = getContacts();
fields = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
adapter = new SimpleCursorAdapter(this, R.layout.entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
}
Where R.id.contactEntryText is in a different .xml than the one that is currently inflated (it just adapts to that layout).
Naturally, I can't impose a setTextColor();
on it because when I try to do a findViewById
, I get a null pointer exception.
How would I go about changing the font style on that layout so my listview picks it up?
Upvotes: 1
Views: 121
Reputation: 7387
instead of just using SimpleCursorAdapter, extend it. Then in getView you can change the UI (text color, font size, or whatever). You could get the color or size set in your settings activity by looking up the preferences via the preference manager in either the adapter, or somewhere in your activity (onCreate...).
You can't use a handler to communicate between activites.
Update, sorry for the lack of clarity, you need to override your adapter, not (or not just) the ListActivity. here is some code:
In your activity:
private void populateContactList() {
Cursor cursor = getContacts();
fields = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
adapter = new MyAdapter(this, R.layout.entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
}
MyAdapter is your own custom adapter that extends simple cursor adapter. If all you need to do is find a text view in each row and change some properties (which was my understanding), call super.getView to get the ViewGroup for each row, then you can call findViewById on that ViewGroup to get your TextView. Once you have that, you can change the properties as you wish. Below is only the most basic implementation outline:
private static class MyAdapter extends SimpleCursorAdapter{
public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
//you could use context to get PreferenceManager and find the
//colors/sizes set in your settings activity here
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewGroup rowView = (ViewGroup) super.getView(position, convertView, parent);
final TextView yourText = (TextView) rowView.findViewById(R.id.yourTextViewId);
yourText.setTextColor(...);
yourText.setTextSize(...);
}
}
Upvotes: 2