Sunday, 15 May 2011

How to check if TextView is within the Visible Screen Android -



How to check if TextView is within the Visible Screen Android -

i wanted know if textview within visible screen part or not..

i have looked @ links as: android - visible area of view? android: how check if view within of scrollview visible?

but nil seems work me.

code used check is

rect rect = new rect(); //textview intialized in oncreate text1 text1.gethitrect(rect); text1.measure(0, 0); //layout parent layout (linear) in adding text view rect acctualview = new rect(math.round(layout.getx()), math.round(layout.gety()), math.round(layout.getx() + layout.getwidth()), math.round(layout.gety() +layout.getheight())); if(rect.intersects(acctualview, rect)) { toast.maketext(context, "visible", toast.length_long).show(); }else { toast.maketext(context, "not visible ", toast.length_long).show(); }

every width , height of textview , layout giving me 0 "zero",

please allow me know how prepare , know whether view within visible part or not

here finish activity code

public class mainactivity extends activity { private textview text1, text2, text3, text4, text5, text6; private context context; private linearlayout layout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); context = getlayoutinflater().getcontext(); layout = (linearlayout) findviewbyid(r.id.parentlayout); settextviews(); checkviewandupdate(); } private void checkviewandupdate() { rect rect = new rect(); layout.gethitrect(rect); if (text1.getlocalvisiblerect(rect)) { toast.maketext(context, "visible", toast.length_long).show(); } else { toast.maketext(context, "not visible", toast.length_long).show(); } } private void settextviews() { text1 = new textview(context); text2 = new textview(context); text3 = new textview(context); text4 = new textview(context); text5 = new textview(context); text6 = new textview(context); text1.settext("text1"); text2.settext("text2"); text3.settext("text3"); text4.settext("text4"); text5.settext("text5"); text6.settext("text6"); text1.settextsize(30); text2.settextsize(30); text3.settextsize(30); text4.settextsize(30); text5.settextsize(30); text6.settextsize(30); //layout.removeallviews(); layout.addview(text1); layout.addview(text2); layout.addview(text3); layout.addview(text4); layout.addview(text5); layout.addview(text6); }

}

and layout contains linearlayout (empty)

as wrote on comment, checking visible rect @ oncreate won't work. seek code @ end of oncreate (instead of phone call checkandupdate() directly).

layout.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { checkviewandupdate(); } });

view layout not finish though if added view @ oncreate. yes, time user cannot see actual activity , view's actual size not set. ongloballayout phone call when view layout ended (and activity visible). @ timing, can size , position of them!

fyi, can remove listener calling layout.getviewtreeobserver().removeongloballayoutlistener(this) in ongloballayout()

android textview

No comments:

Post a Comment