1. Are you ready for the Galaxy S20? Here is everything we know so far!

Custom Typface in Listview Row layout

Discussion in 'Android Lounge' started by uppal, Aug 6, 2010.

  1. uppal

    uppal Lurker
    Thread Starter

    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 ?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. mrspeedmaster

    mrspeedmaster Android Expert

    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);
     
  3. uppal

    uppal Lurker
    Thread Starter

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


    }
    }
     
Loading...
Similar Threads - Custom Typface Listview
  1. SpanDragon18
    Replies:
    2
    Views:
    1,054
  2. Nathan Sokalski
    Replies:
    0
    Views:
    480
  3. samaspotion
    Replies:
    4
    Views:
    988
  4. mangoboy92
    Replies:
    2
    Views:
    611
  5. stymiee
    Replies:
    5
    Views:
    4,740
  6. hacksoldier98
    Replies:
    0
    Views:
    932
  7. LuckyBit33
    Replies:
    4
    Views:
    1,598
  8. kisame010588
    Replies:
    5
    Views:
    1,254
  9. iamashrafulbd
    Replies:
    0
    Views:
    930
  10. Vii Cards
    Replies:
    2
    Views:
    2,422

Share This Page

Loading...