1

I am trying to set EditText Hint Typeface I found few solutions from android but can not implement them in Xamarin Android

One of them is following:

LayoutInflater inflater = LayoutInflater.From(Application.Context); 
TextView hintTextView = (TextView)inflater.Inflate(com.android.internal.R.layout.textview_hint, null);
hintTextView.SetTypeface(typeface);

But I can not find the internal resource

com.android.internal.R.layout.textview_hint

As I see the hint is just TextView. Is it possible to set its style in app Theme? https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/textview_hint.xml

Another solution can be reflection from this topic Android EditText hint uses the same font that the EditText has

But i can not get mHintLayout

var mail = FindViewById<EditText>(Resource.Id.email_input);
Field hintLayoutField = mail.Class.GetDeclaredField("mHintLayout");
Community
  • 1
  • 1
Dima
  • 464
  • 8
  • 19

1 Answers1

2

I was using reflection to set the hint typeface, but I been using the Calligraphy project for awhile now and it works great and the best thing is, it handles it all automatically... ;-)

Once you install the Calligraphy package, all you have to do is assign the new fontPath attribute:

<TextView
    android:editable="true"
    fontPath="BalooBhaina-Regular.ttf"
    android:hint="StackOverflow Hint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>  

enter image description here

There is a public/Nuget-based Xamarin binding of Calligraphy:

Getting Internal Resource Ids:

In your reflection example, you can obtain the resource id of internal items like com.android.internal.R.layout.textview_hint by using Resources.GetIdentifier

Example:

int id = Resources.GetIdentifier("textview_hint", "layout", "android");
TextView hintTextView = (TextView)inflater.Inflate(id, null);
hintTextView.SetTypeface(typeface);
Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Calligraphy works well. Just for information last example with hintTextView.SetTypeface(typeface) doesn't change a font but you showed me how to fin an internal resource. – Dima Mar 27 '17 at 20:26
  • 1
    @DimaBabich All the various reflection methods for changing hint text require that you do it after the view has been inflated, I was using the `GetDeclaredField` on the HintLayout field, setting it accessible and getting the layout of the textview in question via that reflected class field and setting its TextPaint's typeface, this is after the view is inflated and usually via posting this work to the main Looper (via a Runnable is the easiest).... a real pain, thus the reason I have been using Calligraphy... ;-) – SushiHangover Mar 27 '17 at 20:40