0

I am trying to add a icon to my textview. My java code for the corresponding fragment is:

public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/weathericons-regular-webfont.ttf");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       ....
       TextView name_sunrise = rootView.findViewById(R.id.tv_sunr);
       //name_sunrise.setText();  Here I want to put an icon from tf

My xml for tv_sunr is defined as:

    <TextView
        android:id="@+id/tv_sunr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_sunrt"
        android:layout_alignBottom="@+id/tv_sunrt"
        android:layout_toEndOf="@+id/iv_image"
        android:paddingLeft="10sp"
        android:text="@string/sunrise"
        android:textSize="20sp"></TextView>

And the text I am trying to replace is android:text="@string/sunrise" defined in strings as:

<string name="sunrise">SunRise</string>

The question is twofold:

  1. How to add the ttf to the textview
  2. How to find the actual item in the ttf file I am looking to use (e.g. in this case, how to find the font for sunrise)

I am using this ttf from github.

I am following this tutorial, but stuck here.

UPDATE Hi, from the weather icon, I have found that the icon I want to use is named as wi-sunset. The problem is how I will use that icon. Kindly help.

BaRud
  • 3,055
  • 7
  • 41
  • 89
  • https://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android/48642116#48642116 – duggu Feb 20 '18 at 05:57

3 Answers3

0

inside your onCreateView , you can set the font like this

Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/weathericons-regular-webfont.ttf");
TextView name_sunrise = rootView.findViewById(R.id.tv_sunr);
name_sunrise.setTypeface(tf);
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
  • Yes...thats what I have done, but that does not solve my 2nd question. WHat is my icon name? how to set that up? – BaRud Feb 20 '18 at 06:38
0

If you are using Android Studio 3.0, then You need to create the directory with font. in font directory you can put your .ttf file

After that use the below code:

<TextView
    android:id="@+id/tv_shop_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:fontFamily="@font/montserrat_semibold"
    android:text="Hair Cut + Hair Spa"/>
Nikunj
  • 3,937
  • 19
  • 33
0

create font folder inside your res folder and put your string files on font folder.

You can define font for your views like below.

XML :

   android:fontFamily="@font/your_font" //no need to mention your file type (ttf,otf)

Java :

Typeface t =ResourcesCompat.getFont(activity, R.font.your_font);

Thanks

Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30