""

Tag Archive: webview

Source: http://osx.wdfiles.com/local--files/icon:iruler/iRuler.png

Source: http://osx.wdfiles.com/local–files/icon:iruler/iRuler.png


I was supposed to detect the event when scroller/page of a webview reaches bottom so that i can show one decent translate animation of another small view(like an ad banner). That view will be overlayed definitely and when web page scrolls above center of page then view will hide via translate animation. I came up with a simple solution to detect scroll position. I made an subclass of webview and override onScrollChanged method. Inside this method i am checking scrollY position with the height of content height.

@Override
	public void  onScrollChanged (int l, int t, int oldl, int oldt){
		int height = (int) Math.floor(this.getContentHeight() * this.getScale());
		if(this.getScrollY() >= height){
			Log.i("THE END", "reached");
		}
	}

I could get content height from getContentHeight but i need to keep zoom level of page in mind too so i have multiplied scale factor. Now whenever scrollY reaches height then page end has been reached. It is not so, because we haven’t checked for offset value. That offset value is height of the scroller according to theory if scrollY is the exact Y coordinates of scroller :P. I tried many ways to calculate the height but still i am missing something. I checked the reference documentation and found lots of helpful stuffs. I’l cover everything that i used where i failed and had success. First i used getBottom and getTop, according to doc it returns top and bottom position relative to parent, since parent on my case was “relative layout” with “match_parent”. I would calculate the heght of scroller:

int webViewHeight = this.getBottom() - this.getTop();

Now i have height of content and height of web view so i calculated the height of the scroller from this:

int scrollerHeight = (webViewHeight * webViewHeight) / height; //height is content height

I calculated detection of end reach with this condition:

if(this.getScrollY() + scrollHeight >= height){
	Log.i("THE END", "reached");
}

But it wasn’t any way near to perfection 😛 I logged the values and saw that calculation for view of webview was getting wrong output. Even getBottom was no way near accurate since it was returning way low value than expected with respect to my 7 inch nexus tablet density.

Next, i used getMeasuredHeight() and this one gave me accurate value. So i applied same above calculation with new height of web view and result still not very accurate. After all this i did small change to my formula and BINGO!!

public void  onScrollChanged (int l, int t, int oldl, int oldt){
	int height = (int) Math.floor(this.getContentHeight() * this.getScale());
	int webViewHeight = this.getMeasuredHeight();
	if(this.getScrollY() + webViewHeight >= height){
		Log.i("THE END", "reached");
	}
}

Yes above code worked absolutely accurate and am still analyzing the formula on above condition :P, i.e offset here is height of the webview but not height of scroll [sighs]. I would love to hear your opinion on this. You can optimize the above code though i just wanted to share the idea behind.

RTL support on android

Hola everyone,
Long time no see 😀 I was making an app based on hebrew language. Its true that hebrew is not fully supported on all android phones. On some device it may work RTL and on some hebrew won’t be supported at all. Anyway here i am again to share something that i learned. On devices that supported hebrew(rendering) had a minor issue, lets say there is a number 52 on a hebrew sentence and when u try to display this sentence then 52 number is displayed reverse i.e 25. Now just imagine how messed up will whole sentence be because of this reverse number. Thus i found loophole in new android RTL support. So i had to do something about it as this was full hebrew language based app. I was using textview to display this hebrew sentence and this sentence was coming from xml web service. So i came up with my awesome solution. I chose to use webview to render/display hebrew sentence with html formatting. SO my code looked like this to prepare html:

public static String BuildHtml(String hebrewText)
{       
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append("");
sb.append("");
sb.append("");
sb.append(hebrewText.trim());
sb.append("");
sb.append("");
	
return sb.toString();
}

Now i load my html on a webview by calling my method that returns my hebrew string encoded in html format.

webView.loadData(BuildHtml(myHebrewText), "text/html", "utf-8");

This code worked flawlessly. The number 56 was getting displayed as 56 in a hebrew sentence.
I hope this helps those who faced simillar problem.

Gracias.

Dude where is my javascript ?

Hola everyone,
a very hectic schedule going on this season. Working my ass off in my upcoming ambitious project. Anyway i am here and writing about just very basic problem that i faced. For a game project i wanted to show leaderboards, for this part i decided to use web view to show leaderboards page optimized for device. It has some javascript function that is doing its stuff of retrieving leaderboards details and all that. So i wrote a line to load the url pointing to my leaderboard page and guess what it didn’t rendered fully and i was again left scratching my head 😀 . Then after going through many testing i found that stuffs that were dependent on javascript were not getting rendered on my web view so i got the root of the problem i.e dude where is my javascript ? . Then i did a peroid(.) operator on my webview reference variable and found a method setJavaScriptEnabled(), so i did something like this:

webView.getSettings().setJavaScriptEnabled(true);

and yeah u guessed it right, it rendered fully after calling that method. So i guess by default javascript is disabled on webview. Phew.

Gracias.