Installation Type: Face to NumberPicker

How to change font type in NumberPicker. I try to do so, but the font does not change. Any ideas? PS: color and textSize are changing.

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); } } } 

The font and path are working correctly. I use it for my custom text views.

+9
source share
5 answers

I fix the same problem by setting Typeface data in addView as the 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)); } } } 
+2
source

In your method updateView(View view)

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 fonts, see here .

Decision 2

If you have your own .ttf file for the font, create a font folder in the resource folder and place your .ttf font .ttf , and then inside the onCreate() function write:

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

Decision 3

Refer to this SO question for a great way to achieve what you want. Hope this helps.

0
source

This is not a very neat solution, but if you set TypeFace to 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)); } } 
0
source

Sorry for the late reply, but this is how I got what you were looking for. I used this to use MaterialNumberPickers, not stock NumberPickers. There are instructions on how to add Gradle dependencies in the ReadMe of this github repository, so I won’t include them here.

Then I set the font MaterialNumberPicker to 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"/> 

In addition, I placed monsterrat-normal.ttf under app> src> main> assets> fonts, as indicated here .

0
source
 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); } } } } 
0
source

Source: https://habr.com/ru/post/975151/


All Articles