0

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..

Tahir Jilani
  • 200
  • 1
  • 17

1 Answers1

3

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;
}
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
  • So how does this work when working with Fragments? getContext comes back null – TheLettuceMaster Oct 19 '12 at 17:48
  • use getActivity() if are inside a Fragment so you get the Activity's context. Or you can do getActivity.getApplicationContext() if you need to access resources. – Martin Marconcini Aug 16 '13 at 19:40
  • be careful with getActivity() from within a Fragment, it may return null if the Fragment isn't currently attached. I prefer to use view.getContext(), assuming I have a view object. – Sam Dozor Aug 16 '13 at 19:59