Custom TextView:
Public classFirstboldtextviewextendstextview{Private BooleanFirstwordbold =false; PrivateString str; PublicFirstboldtextview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub } PublicFirstboldtextview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub } PublicFirstboldtextview (Context context) {Super(context); } @SuppressLint ("Drawallocation") @Overrideprotected voidOnDraw (canvas canvas) {str=GetText (). toString (). Trim (); if(! Textutils.isempty (str) &&!firstwordbold) {Spannablestringbuilder SSB=NewSpannablestringbuilder (str); Ssb.setspan (NewFirstwordboldspan (Typeface.normal), 0, Str.indexof (""), spanned.span_exclusive_exclusive); SetText (SSB); Setmovementmethod (Linkmovementmethod.getinstance ()); Firstwordbold=true; } Super. OnDraw (canvas); } @Overrideprotected voidOnTextChanged (charsequence text,intStartintLengthbefore,intlengthafter) { Super. ontextchanged (text, start, Lengthbefore, Lengthafter); Firstwordbold=false; }}
Where the Firstwordboldspan code is as follows:
Public classFirstwordboldspanextendsstylespan{ PublicFirstwordboldspan (intstyle) { Super(style); } @Override Public intdescribecontents () {//TODO auto-generated Method Stub return Super. describecontents (); } @Override Public intGetspantypeid () {return Super. Getspantypeid (); } @Override Public intGetStyle () {return Super. GetStyle (); } @Override Public voidupdatedrawstate (textpaint ds) {Ds.setfakeboldtext (true); Super. Updatedrawstate (DS); } @Override Public voidupdatemeasurestate (textpaint paint) {Paint.setfakeboldtext (true); Super. Updatemeasurestate (paint); } @Override Public voidWritetoparcel (Parcel dest,intflags) { //TODO auto-generated Method Stub Super. Writetoparcel (dest, flags); }}
Android--textview First word capitalization