Tahir Jilani
Tahir Jilani

Reputation: 200

how to set custom font to one of the TextView's in ListView item?

I have code like:

SpecialAdapter adapter = new SpecialAdapter(
                                this, 
                                list, 
                                R.layout.list_display_item, 
                                from, 
                                to );

        myTextListView.setAdapter(adapter); 

.
.
.
.

public class SpecialAdapter extends SimpleAdapter
 {
        public SpecialAdapter(Context context, List<HashMap<String, 
String>> items, int resource, String[] from, int[] to) {
            super(context, items, resource, from, to);
        }

        @Override

        public View getView(
int position, View convertView, ViewGroup parent)
 {
          View view = super.getView(position, convertView, parent);
                }
    }

and list_display_item.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myDisplayItemLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="vertical">

        <TextView
            android:id="@+id/arabicTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:lineSpacingMultiplier="3"
            android:text="Arabic"
            android:textSize="30dp"/>

        <TextView
            android:id="@+id/translationTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="English" 
            android:gravity="left|center_vertical"
            android:textSize="30dp"/>

    </LinearLayout> 

I just want to set my own arabic font to android:id="@+id/arabicTextView" and english font to android:id="@+id/translationTextView".

In getView() function of SpecialAdapter class i get View view = super.getView(position, convertView, parent) and view.getId() is actually the id of linearlayout android:id="@+id/myDisplayItemLinearLayout"

I just want to get inner textviews of this linearLayout to set seperate fonts to each using typeface here... how can i get these textViews there??

Is it correct way to set my fonts here or it should be done in someother way???? please help me..

Upvotes: 0

Views: 3291

Answers (1)

Sam Dozor
Sam Dozor

Reputation: 40734

Use the findViewById method of View and the setTypeface method of TextView

public View getView(int position, View convertView, ViewGroup parent)
{
    View view = super.getView(position, convertView, parent);

    //assuming you've put your arabic font file in your assets directory
    ((TextView)view.findViewById(R.id.arabicTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourarabicfont.otf"));

    //assuming you've put some custom english font in your assets directory
    ((TextView)view.findViewById(R.id.translationTextView)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "yourenglishfont.otf"));

    return view;
}

Upvotes: 3

Related Questions