Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a typeface in Android?

I have developed an Andriod RSS reader app.I have used custom listview to list RSS titles and its images.Now I want to change the font of RSS titles.How can I set typeface to my title textview?

Here is my adapter class in which I am setting the title Textview.

Adapter.java

public class InternationalAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)


            vi = inflater.inflate(R.layout.list_row, null);




        TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
        TextView date = (TextView)vi.findViewById(R.id.artist);

        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);

        // Setting all values in listview
        title.setText(International.Title[position]);
        date.setText(International.Date[position]);
        imageLoader.DisplayImage(International.image[position], thumb_image);
        return vi;
    }
}
like image 569
Basim Sherif Avatar asked May 19 '12 05:05

Basim Sherif


People also ask

What typeface does Android use?

Roboto is the default font on Android, and since 2013, other Google services such as Google Play, YouTube, Google Maps, and Google Images.

How do I set text font view?

If you are using Android Studio 3.5+, Changing the font is super simple. Select the text widget on the Design view and check the font family on Attribute Window. The value dropdown contains all the available fonts from which you can select one. If you are looking for Google Fonts, Click the More Fonts option.


2 Answers

You can use this,

        public class InternationalAdapter extends BaseAdapter {

            private Activity activity;
            private ArrayList<HashMap<String, String>> data;
            private static LayoutInflater inflater=null;
            public ImageLoader imageLoader; 

            public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
                activity = a;
                data=d;
                inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                imageLoader=new ImageLoader(activity.getApplicationContext());
            }

            public int getCount() {
                return data.size();
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                View vi=convertView;
                if(convertView==null)


                    vi = inflater.inflate(R.layout.list_row, null);




                TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
                TextView date = (TextView)vi.findViewById(R.id.artist);
//Added Here
        Typeface font = Typeface.createFromAsset(
        activity.getAssets(), 
        "fonts/androidnation.ttf");
    title .setTypeface(font);

                ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

                HashMap<String, String> news = new HashMap<String, String>();
                news = data.get(position);

                // Setting all values in listview
                title.setText(International.Title[position]);
                date.setText(International.Date[position]);
                imageLoader.DisplayImage(International.image[position], thumb_image);
                return vi;
            }
        }
like image 151
Aerrow Avatar answered Sep 19 '22 17:09

Aerrow


Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library

Setting typeface like the one below often does not work (Android Runtime Exception font asset not found)

Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/montserrat_regular.ttf");

and can result into error like this:

java.lang.RuntimeException: Font asset not found fonts/montserrat_regular.ttf

You can therefore use the new way as given here

Steps:

1. Make a folder in resources folder in your project like res/font and paste your fonts there

You can alternatively create a font family

Font family is something that was introduced in Android, and it is used to define a group of fonts and their corresponding style. So the system can determine what font resource to use for regular, bold and italic styles and weight configurations. A typical font family looks like this:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- regular -->
<font
    android:font="@font/josefinslab_regular"
    android:fontStyle="normal"
    android:fontWeight="400"

    app:font="@font/josefinslab_regular"
    app:fontStyle="normal"
    app:fontWeight="400" />

<!-- italic -->
<font
    android:font="@font/josefinslab_italic"
    android:fontStyle="italic"
    android:fontWeight="400"

    app:font="@font/josefinslab_italic"
    app:fontStyle="italic"
    app:fontWeight="400" />

</font-family>

Note

A really important thing to note is that we had to define attributes using both android and app namespaces. The app namespace is what ensures that the feature is backward compatible.

2. Using the font programmatically

Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
myTextView.setTypeface(typeface);

Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library

Hope this helps :)

like image 27
anirudh sharma Avatar answered Sep 23 '22 17:09

anirudh sharma