• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Custom Typface in Listview Row layout

uppal

Lurker
Aug 6, 2010
2
0
Hi Guys

I am running a sqlite query and binding the returned data to a ListAdapter.

I have used the following example

ListActivity | Android Developers

However, having defined a seperate layout for the rows I cannot change the typeface for textview text1 to a custom one from assets

Here is the row layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:eek:rientation="vertical">

<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


Here is the code from the andriod tutorial

public class MyListAdapter extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.custom_list_activity_view);

// Query for all people contacts using the Contacts.People convenience class.
// Put a managed wrapper around the retrieved cursor so we don't have to worry about
// requerying or closing it as the activity changes state.
mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);

// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor
rows).
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.

// Bind to our new adapter.
setListAdapter(adapter);
}
}



Any ideas ?
 
I don't think you can do custom fonts. You have to stick to the Droid family or use a bit-map.
In your textview, you add: android:typeface="serif" (or whatever family)

<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:typeface="serif"
</LinearLayout>


Or do something like this:

Typeface myFont = Typeface.createFromAsset(getAssets(), "assets/Arial.ttf");

TextView tv = (TextView) findViewById(R.id.texty);
tv.setTypeface(myFont);
 
Upvote 0
Thanks for your repsonse.

I added your code snippet to MyListAdapter below.

The code compiles fine, but throws an error at run time caused by :

TextView tv = (TextView) findViewById(R.id.text1);

text1 is defined in a separate view (android.R.layout.two_line_list_item) and is NOT defined in the same view as set by the following line:

setContentView(R.layout.custom_list_activity_view) ;

The error occurs as R.id.text1 cannot be found.

How can I reach the view for the rows to set the typeface (android.R.layout.two_line_list_item) ?

Thanks in advance




public class MyListAdapter extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.custom_list_activity_view) ;

// Query for all people contacts using the Contacts.People convenience
// Put a managed wrapper around the retrieved cursor so we don't ha
// requerying or closing it as the activity changes state.
mCursor = this.getContentResolver().query(People.CONTENT_URI , null, null, null, null);
startManagingCursor(mCursor);

// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use
rows).
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.

// Bind to our new adapter.
setListAdapter(adapter);

Typeface myFont = Typeface.createFromAsset(getAssets(), "assets/Arial.ttf");

TextView tv = (TextView) findViewById(R.id.text1);
tv.setTypeface(myFont);


}
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones