Customize the size of hint text in EditText in Android

Source: Internet
Author: User

Method One:

After the text in the EditText is set to size, the hint text is too long to display in the EditText, so ask for an option to set the hint text size individually.

TextView Source code (EditText inherits from TextView), as follows:

Java
PublicFinalvoidSethint(Charsequence hint){Mhint= Textutils.Stringorspannedstring(Hint);If(Mlayout! = NULL){Checkforrelayout();}If(MText.Length()==0){Invalidate();}Invalidate Display list If hint is currently usedIf(Meditor! = NULL&& MText.length () == 0 && mHint != null) {meditor. Invalidatetextdisplaylist ( ) ; }} 
  At the beginning of the method is the conversion of the hint text. Since hint is a charsequence type, it is possible to add some custom properties, and we'll look at Textutils.stringorspannedstring this method:
PublicStatic charsequenceStringorspannedstring(Charsequence source){If(Source= = NULL)return null;If(SourceinstanceofSpannedstring)return source;if  (source instanceof spanned) return new Span class= "Prism-token prism-class-name" >spannedstring (Sourcereturn source. Tostring () ; }           /span>       

As long as the incoming hint is a spannedstring or spanned type, can you keep the custom properties of the text? The answer is YES! Directly on the code:

EditText EditText=(EditText) Rootview.Findviewbyid(R. ID. ET);Create a new text object that can add properties spannablestring SS=NewSpannablestring("Drinking is going to take a pound!");Create a new Property object, set the size of the text Absolutesizespan=NewAbsolutesizespan(8,True);Attach property to Text SS.Setspan(The,0Ss.Length () ) ; //set Hintedittext.sethint (new spannedstring  (Ss) ) ; //Be sure to convert, otherwise the property disappears          
< Span class= "Prism-token prism-operator" > Note The final step, be sure to convert, the type is not converted to a string object, so the custom amount property will be lost. 

Method Two:
<string name= "Edittext_hint" ><font size= "all" >HINT&NBSP;HERE!</FONT></STRING>

android:hint="@string/edittext_hint"
Method Three:
SpannableString span = new SpannableString(strHint);span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);editText.setHint(span);
  &NBSP;      

Customize the size of hint text in EditText in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.