android - Cannot find if a view is not visible on screen in a ScrollView. Always returns true -
i have been trying determine if textview can seen on screen, if not want display small text saying "scroll read". have tried various methods questions this , many others too. in detail fragment of master-detail layout, have fragment, contains framelayout. framelayout has scrollview in , textview in front saying "scroll read" (which keep invisible until textview news article in scrollview not visible). eg, wont display "scroll read" textview here or here, display here. right now, visible testing purposes.
the layout here. scrollview scroller
. when image available viewstub article_header_stub_import
used given in bottom of file. want check if textview body
visible or not. added wastedtext
below body checking shown visible in cases.
i have tried different things solutions, getting gethitrect scrollinglinearlayout
or scroller
or detailframelayout
, detail fragment. code similar question linked , using log.d(...) see check if-else case.
i using picasso load image , doing coding in success callback of picasso, eg this:
picassoinstance.load(mimageurl).into(mmainimageview, new callback() { @override public void onsuccess() { log.d(tag_async, "image loaded"); mtesttextview.setvisibility(view.visible); rect scrollbounds = new rect(); mscrollview.gethitrect(scrollbounds); if (mtesttextview.getlocalvisiblerect(scrollbounds)) { // portion of imageview, single pixel, // within visible window log.d(tag_async, "is visible"); } else { // none of imageview within visible window log.d(tag_async, "not visible"); } } @override public void onerror() { // todo auto-generated method stub } });
i have tried getheight, gettop, getbottom etc. returns 0.
i not able use rect. searched "view getheight 0". although still dont understand why height returning 0 (although happening in onpostexecute of asynctask , after image loaded picasso), used globallistener height , compare screen.
picassoinstance.load(mimageurl).into(mmainimageview, new callback() { @override public void onsuccess() { log.d(tag_async, "image loaded"); mmainimageview.getviewtreeobserver().addongloballayoutlistener( new viewtreeobserver.ongloballayoutlistener() { @suppresswarnings("deprecation") @suppresslint("newapi") @override public void ongloballayout() { //get display metrics according sdk display display = getwindowmanager().getdefaultdisplay(); point screen = new point(); if (build.version.sdk_int >= build.version_codes.honeycomb_mr2) { display.getsize(screen); } else { screen.x = display.getwidth(); screen.y = display.getheight(); } //statusbar height int statusbarheight = 0; int resid = getresources().getidentifier("status_bar_height", "dimen", "android"); if (resid > 0) { statusbarheight = getresources().getdimensionpixelsize(resid); } //actionbar height typedvalue tv = new typedvalue(); int actionbarheight = 0; if (gettheme().resolveattribute(android.r.attr.actionbarsize, tv, true)) { actionbarheight = typedvalue.complextodimensionpixelsize(tv.data, getresources().getdisplaymetrics()); } //check log.d(tag_async, "msubtitlelayout " + msubtitlelayout.getheight()); log.d(tag_async, "mmainimageview " + mmainimageview.getheight()); log.d(tag_async, "marticletextview " + marticletextview.gettop()); log.d(tag_async, "total height " + screen.y); log.d(tag_async, "status bar " + statusbarheight); log.d(tag_async, "action bar " + actionbarheight); //boolean check if image+subtitle large enough. //if yes, display "scroll read" displayscrolltoread = (screen.y - statusbarheight - actionbarheight) < (marticletextview .gettop()) * 1.08; log.d(tag_async, "displayscrolltoread " + displayscrolltoread); if (displayscrolltoread) { mscrolltoreadlayout.setvisibility(view.visible); log.d(tag_async, "visiblity - " + mscrolltoreadlayout.getvisibility()); } //remove globallayoutlistener according sdk if (build.version.sdk_int > build.version_codes.ice_cream_sandwich_mr1) { mmainimageview.getviewtreeobserver().removeongloballayoutlistener(this); } else { mmainimageview.getviewtreeobserver().removeglobalonlayoutlistener(this); } } }); } @override public void onerror() { log.d(tag_async, "image not loaded!!!"); } });
Comments
Post a Comment