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.
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));
}
}
}
Answer:
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.
Answer:
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));
}
}
Answer:
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.
Answer:
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);
}
}
}
}
Answer:
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"
Tags: androidandroid