Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting typeFace to NumberPicker

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.

like image 710
once2go Avatar asked Sep 11 '14 18:09

once2go


People also ask

How can change number picker center text color in Android?

setOnValueChangedListener(new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { EditText et = ((EditText) npMonth. getChildAt(0)); et. setTextColor(getResources(). getColor(R.

How can I get value from number picker?

Use picker. getValue() to get the current value of the picker.


1 Answers

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"
like image 197
Deepak Sachdeva Avatar answered Sep 19 '22 08:09

Deepak Sachdeva