-3

How to add custom font to it? This is my code:

public void run() {
    ListAdapter adapter = new SimpleAdapter(DiaListActivity.this, diaList, R.layout.list_item, new String[]{TAG_SRNO, TAG_NAME}, new int[]{R.id.srno, R.id.name});
    setListAdapter(adapter);
}

any help will be appreciated.

Ziem
  • 6,579
  • 8
  • 53
  • 86
  • You will have to create a custom adapter, and set the font of the items in the adapter. See this for an example: http://stackoverflow.com/questions/4576441/custom-font-in-android-listview – manDroid May 30 '15 at 07:27

3 Answers3

0
    In R.layout.list_item  layout file if you have any textview then you can set this below code::-

    <com.example.TextViewPlus
            android:id="@+id/textViewPlus1"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/showingOffTheNewTypeface"
            foo:customFont="saxmono.ttf">
        </com.example.TextViewPlus>


    Put these class file into your package: 

    TextViewPlus.java

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.TextView;

    public class TextViewPlus extends TextView {
        private static final String TAG = "TextView";

        public TextViewPlus(Context context) {
            super(context);
        }

        public TextViewPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            setCustomFont(context, attrs);
        }

        public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }

        private void setCustomFont(Context ctx, AttributeSet attrs) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
            String customFont = a.getString(R.styleable.TextViewPlus_customFont);
            setCustomFont(ctx, customFont);
            a.recycle();
        }

        public boolean setCustomFont(Context ctx, String asset) {
            Typeface tf = null;
            try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface: "+e.getMessage());
                return false;
            }

            setTypeface(tf);  
            return true;
        }

    }



attrs.xml: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>
Dakshesh Khatri
  • 639
  • 7
  • 12
0

What I usually do is, create a custom TextView and set it as the view of my contents,

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextView(Context context) {
        super(context);
    }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        } else {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"));
        }
    }
}

Now simply replace your regular TextView with the Custom one like this,

<TextView
 android:id="@+id/R.id.srno"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 />

Into,

 <com.example.CustomTextView
  android:id="@+id/R.id.srno"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

This will change the normal TextView with the custom one and hence will add the font.

Note: Make sure you have crated a folder called "fonts" and put the fonts file there.

If can also customize and add different fonts just by adding a single line,

e.g. If you use,

android:textStyle="bold"

It will set the font type Roboto-Regular.ttf. Which is defined here in CustomTextView class,

if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        }
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
0

Got it...

@Override
public View getView(int pos, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item, null);
    }

    HashMap<String,String> value = diaList.get(pos);

    TextView tv = (TextView)v.findViewById(R.id.name);
    Typeface custom_fontG = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
    tv.setTypeface(custom_fontG);
    tv.setText(value.get(TAG_NAME));

    TextView tv2 = (TextView)v.findViewById(R.id.srno);
    Typeface custom_fontH = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
    tv2.setTypeface(custom_fontH);
    tv2.setText(value.get(TAG_SRNO));

    return v;
}
};

// updating listview

setListAdapter(adapter);
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64