14

How change font type in NumberPicker. I try to do like this, but font not changing. Any idea? P.S: color and textSize are change.

public class NumberPicker extends android.widget.NumberPicker {
        private Context context;
        private Typeface tfs;
       public NumberPicker(Context context, AttributeSet attrs) {
         super(context, attrs);
         this.context = context;
         tfs = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
       }

       @Override
       public void addView(View child) {
         super.addView(child);
         updateView(child);
       }

       @Override
       public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, index, params);
         updateView(child);
       }

       @Override
       public void addView(View child, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, params);
         updateView(child);
       }

       private void updateView(View view) {
         if(view instanceof EditText){
           ((EditText) view).setTypeface(tfs);
           ((EditText) view).setTextSize(25);
           ((EditText) view).setTextColor(Color.RED);
         }
       }

     }

Font and path work correctly. I use it for my custom text views.

once2go
  • 1,452
  • 1
  • 13
  • 21
  • yes, number picker is a view group, so thats why, first of all you need override you custom class which extend android.widget.NumberPicker, than look code of NumberPicker, which embedded to android and divide all components to the groups and find it dynamically in code, than get components from separate view groups. picker->viewGroups->EditText – once2go Jul 06 '15 at 13:25
  • 1
    I know this was asked a long time ago but I just looked looked into implementing this for my project. `addView` is called before the constructor, therefore, your typeface is null when you try to set it. – Nick Sep 25 '19 at 20:08

6 Answers6

5

Add below style in your style.xml.

<style name="NumberPickerCustomText">
    <item name="android:textSize">22sp</item> // add if you want to change size
    <item name="android:fontFamily">@font/lato_regular</item> // add font (this font folder is present in res folder)
</style>

Directly add this style to your Number Picker in XML.

android:theme="@style/NumberPickerCustomText"
Deepak Sachdeva
  • 950
  • 8
  • 16
4

I fix the same problem by set Typeface data in addView like code below:

public class CustomNumberPicker extends android.widget.NumberPicker {

Typeface type;

public CustomNumberPicker(Context context, AttributeSet attrs) {
    super(context, attrs);

}

@Override
public void addView(View child) {
    super.addView(child);
    updateView(child);
}

@Override
public void addView(View child, int index,
        android.view.ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    type = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/b_yekan.ttf");
    updateView(child);
}

@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
    super.addView(child, params);

    type = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/b_yekan.ttf");
    updateView(child);
}

private void updateView(View view) {

    if (view instanceof EditText) {
        ((EditText) view).setTypeface(type);
        ((EditText) view).setTextSize(25);
        ((EditText) view).setTextColor(getResources().getColor(
                R.color.text_dark_grey));
    }

}

}
Sasa
  • 565
  • 5
  • 15
0

In your updateView(View view) method

Solution 1

private void updateView(View view) {
     if(view instanceof EditText){
       ((EditText) view).setTypeface(Typeface.SERIF);
       ((EditText) view).setTextSize(25);
       ((EditText) view).setTextColor(Color.RED);
     }

For other Typefaces see here.

Solution 2

If you have your own .ttf file for font, create a fonts folder on assets folder and put your .ttf font file, then inside onCreate() function write:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); 
((EditText) view).setTypeface(type);

Solution 3

Refer this SO question for a wonderful way of achieving what you want.Hope this helps.

Community
  • 1
  • 1
Sash_KP
  • 5,551
  • 2
  • 25
  • 34
0

It is not very neat solution, but if you set the TypeFace in updateView, it works :-|

private void updateView(View view) {
    Typeface  tfs = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf");
    if (view instanceof EditText) {
        ((EditText) view).setTypeface(tfs);
        ((EditText) view).setTextSize(25);
        ((EditText) view).setTextColor(getResources().getColor(
            R.color.text_dark_grey));
    }
}
Parissa Kalaee
  • 606
  • 1
  • 9
  • 17
0

sorry for the late response but this is how I achieved what you were looking for. I used this to use MaterialNumberPickers rather than stock NumberPickers. There are instructions in the ReadMe of that github repository on how to add Gradle dependencies, so I won't include them here.

I then set the font of the MaterialNumberPicker in xml as follows:

<com.github.stephenvinouze.materialnumberpickercore.MaterialNumberPicker
        android:id="@+id/np_timer_hour"
        .
        .
        .
        app:mnpFontname="monsterrat_regular.ttf"
        app:mnpTextColor="@color/primaryTextColor"
        app:mnpTextSize="28sp"/>

Additionally I placed monsterrat-regular.ttf under app>src>main>assets>fonts as instructed here.

Kevin Barron
  • 88
  • 2
  • 9
0
package com.harman.settings.dateandtime;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.harman.settings.R;

public class XNumberPicker extends android.widget.NumberPicker {
    public XNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        updateView(child);
    }

    @Override
    public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        updateView(child);
    }

    private void updateView(View view) {
        if (view instanceof EditText) {
            Typeface typeface = null;
            try {
                typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + getContext().getString(R.string.lato_bold));
            } catch (Exception e) {
                Log.e("XNumberPicker", "Unable to load typeface: " + e.getMessage());
            }
            if (typeface != null) {
                ((EditText) view).setTypeface(typeface);
            }
        }
    }
}
Vinayak
  • 6,056
  • 1
  • 32
  • 30