android - How to know if there is available space in TextView? -
because make same, whatsapp, puts message , hour in same line
or in line.
sometimes there space , whatsapp puts hour in same line. there not space , whatsapp puts hour in other line.
inside or outside...
any idea?
public static final string tag = "mainactivity"; private textview mtext; private relativelayout relativelayout; private boolean mfirsttime = true; private static final int widh_hour = 382; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final int width = getscreenswidh(); mtext = (textview) findviewbyid(r.id.activity_main_text); relativelayout = (relativelayout) findviewbyid(r.id.activity_main_relative); mtext.settext("aaaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa dsa aaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa"); viewtreeobserver vto = mtext.getviewtreeobserver(); vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { if (mfirsttime) { layout layout = mtext.getlayout(); int lines = layout.getlinecount(); int offset = layout.layout.getlinewidth(lines - 1); int freespace = width - offset; textview hour = new textview(mainactivity.this); hour.settext("12:20"); relativelayout.layoutparams params = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); params.addrule(relativelayout.align_parent_right); if (freespace > widh_hour) { params.addrule(relativelayout.align_bottom, r.id.activity_main_text); } else { params.addrule(relativelayout.below, r.id.activity_main_text); } hour.setlayoutparams(params); relativelayout.addview(hour); log.d(tag, string.valueof(freespace)); mfirsttime = false; } } }); } public int getscreenswidh() { display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); return size.x; }
two public methods
- public abstract int getlinecount ()
return number of lines of text in layout.
- public int getlinewidth(int line)
gets unsigned horizontal extent of specified line, including leading margin indent , trailing whitespace.
Comments
Post a Comment