TextDrawable to display emojis and Unicode characters in ...

文章推薦指數: 80 %
投票人數:10人

TextDrawable to display emojis and Unicode characters in Android ... Aligning the text to the text in the TextView/EditText would be tricky. GetstartedOpeninappAliMuzaffarSigninGetstarted3.7KFollowersAboutGetstartedOpeninappTextDrawabletodisplayemojisandUnicodecharactersinAndroidThebestwaytoaddemojisandgraphicstoyourappwithoutbundlinggraphicsistouseUnicodecharacters.Unfortunately,settingtheseasdrawablescanberathertricky.AliMuzaffarMar5,2016·3minreadLastweek,ItalkedwithafewpeopleaboutthepossibilityofusingTextDrawabletoaddafixedprefixtoanEditText.Thereareunfortunatelyafewdifferentchallenges.Whichmadethisdifficultforme:CannotbedonewithXML’s(thereissomeliteratureaboutit,butafewobjectsIneededwereprivateandIcouldn’tgetholdofthem.Sincewecan’tcreatethisinXMLorsetitasacompounddrawableorbackgroundinXML,wegetnopreviewintheeditor.Drawablewouldhavetobepassedapaintobject.AligningthetexttothetextintheTextView/EditTextwouldbetricky.Well,thiswasn'tashardasIthoughtitwouldhavebeen.Asamatteroffactitworkedoutrathernicely.Notonlycanyouuseitasacompounddrawable,youcansetitasabackgroundaswell.I'vealsoimplementtheabilitytoaskittoresizethetextdrawntofillaTextView’sbackground.WhydoIneedaTextDrawablePossibleusecasesforanEditTextcanbe:ShowaprefixonEditTextthatcan’tbeeditable.Showacharactercounteronrightorleftdrawablesposition.Showindicationusinguni-codecharactersthatinputis,short,long,correctorincorrectinanEditText.UsetotoshowanellipsisonasinglelineEditTextifthetextislongasanindicatorthatthereissomethingoffscreen.Ifyouwanttoknowmoreaboutthis,pingmeandI’llgiveyouanexample.MostoftheaboveispossiblewithanimagesorbystackingViewstogether,thisishopefullyacleanersolutionifUnicodecharactersandtextaregoodenough(andwhywon’tthisbe☺).ShowmeademoItsworthpointingoutthatsincethedrawablereservesitsspacewhensetasacompounddrawable(left,right,toporbottom)thatchangingthetextinthisscenariodoesn'tworkasexpected.Justlookatthedemobelow.The+61andthe★thatyouseeintheEditTextaredrawntherewiththeTextDrawable.The☺thatyouseeisalsodonewithaTextDrawable,setasthebackgroundofaTextView,itisthenbeingupdatedusingacountdowntimer.Asyoucansee,itcanresizethetexttoensurethatitfitsintheView’swidthwhichis200dp.ThecodeThecodeisongist.FinallyInordertobuildgreatAndroidapps,readmoreofmyarticles.Yay!youmadeittotheend!Weshouldhangout!feelfreetofollowmeonMedium,LinkedIn,Google+orTwitter.AliMuzaffarAsoftwareengineer,anAndroid,andarayofhopeforyourdarkestcode.ResidinginSydney.Follow70270 702AndroidAppDevelopmentAndroidAndroidDevMorefromAliMuzaffarFollowAsoftwareengineer,anAndroid,andarayofhopeforyourdarkestcode.ResidinginSydney.



請為這篇文章評分?