studio programacion para móviles libro edición desarrollo desarrollar curso aprende aplicaciones android fonts android-edittext typeface

programacion - ¿Cambiar fuente para EditText en Android?



manual de programacion android pdf (7)

Primer método

usa este código en tu clase de java

EditText et_brand=(EditText)findViewById(R.id.et_brand); et_brand.setTypeface(Typeface.createFromAsset(getAssets(),"Aspergit Bold.ttf")); //Aspergit Bold.ttf is Font style name of text

Segundo método

  1. Crea una nueva clase java y como quieras

    public class CustomTextView extends TextView { public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomTextView(Context context) { super(context); init(); } private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Aspergit Bold.ttf"); setTypeface(tf); } }

  2. Úsalo en tu archivo Xml

    <YourPackageNAme.CustomEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null"/>

¿Hay alguna manera de cambiar la fuente para un EditText en Android? Quiero que coincida con la fuente que configuré para todas mis vistas de texto.


Cree una carpeta de fuentes en la carpeta de recursos y ponga su archivo de fuentes .ttf, luego escriba en la función onCreate ():

EditText editText =(EditText)findViewById(R.id.insert_yors_edit_text_layout); Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf"); editText.setTypeface(type);


Puede crear una carpeta de fuentes en el directorio res en la última versión de Android studio 3. Luego, copie y pásele la fuente a la carpeta de fuentes . Ahora simplemente agregue fontFamily a la etiqueta EditText xml.

como abajo.

<EditText android:id="@+id/subject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/headerShare" android:layout_marginBottom="5dp" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:background="@drawable/edittext_bg" android:fontFamily="@font/ritaric" android:gravity="start" android:hint="Subject" android:inputType="text" android:maxHeight="50dp" android:padding="16dp" android:textColor="@color/black" android:textColorHint="@color/black" android:textSize="@dimen/colors_textview_size" />

Usamos android:fontFamily="@font/ritaric" para aplicar la fuente en EditText.


Solution1 :: Simplemente llame a este método pasando la vista principal como argumento.

private void overrideFonts(final Context context, final View v) { try { if (v instanceof ViewGroup) { ViewGroup vg = (ViewGroup) v; for (int i = 0; i < vg.getChildCount(); i++) { View child = vg.getChildAt(i); overrideFonts(context, child); } } else if (v instanceof EditText) { ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf")); } } catch (Exception e) { } }

Solution2 :: puede crear una subclase de la clase TextView con su fuente personalizada y utilizarla en lugar de la vista de texto.

public class MyEditView extends EditText{ public MyEditView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public MyEditView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyEditView(Context context) { super(context); init(); } private void init() { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf"); setTypeface(tf); } } }


Agregue este código en su archivo java. ¿Qué tipo de letra puede agregar como NORMAL, ITALIC, BOLD, BOLD_ITALIC?

edittext.setTypeface (null, Typeface.ITALIC);


void setFont(Context context, ViewGroup vg) { final String FONT_NAME = "lato_bold.ttf"; for (int i = 0; i < vg.getChildCount(); i++) { View v = vg.getChildAt(i); if (v instanceof ViewGroup) setFont(context, (ViewGroup) v); else if (v instanceof TextView) { ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME )); } else if (v instanceof EditText) { ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME )); } else if (v instanceof Button) { ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME )); } else if (v instanceof CheckBox) { ((CheckBox) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME )); } else if (v instanceof RadioButton) { ((RadioButton) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME )); } } }

En tu Actividad o Fragmento, obtén el diseño principal.

Inflater inflater = (Inflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //For Activity //Inflater inflater = (Inflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.main_fragment, container, false); //For Activity //View v = inflater.inflate(R.layout.signup_fragment, null, false); if (v instanceof ViewGroup) { setFont(getActivity(), (ViewGroup) v); //For Activity //setFont(this, (ViewGroup) v); }


editText.setTypeface(Typeface.SERIF);

Como en el caso de TextView.

<TextView ... android:typeface="serif" ... />

Edición: arriba está el XML