""

Tag Archive: Java

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.

I am tweeting: Android

Someone might be looking for integrating twitter to their android application. I found few alternatives(more than enough) on the web. I’l be talking about few of them on my upcoming posts. All this libraries are unofficial, not provided by twitter itself as there isn’t any official twitter sdk available for android. In this post i’l be talking about one library that i recently used, its Twitter4J. I am again not writing any tutorial for using it. I am here to share some pitfall i fell into, which many people can face for first time. But don’t worry, there is THIS BLOG which has full detail on setting up Twitter4J with your project and get you started tweeting in no time(techically 30 minutes :P).

So i suppose now you have setup the project as detailed in that blog. Now if everything is working than Good Job, if its not working for you yet then go through the steps again and see if you are not missing something. But if you are having problem that i faced and which i will be talking about it here then “GOOD JOB” too 😛

I faced two problems which are very basic but critical.

“oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.”

This error gave me lotsa pain since it was very difficult to find the reason. I spent many hours figuring out what was happening. Error is very specific which states that consumer key might not be matching. But i cross checked keys and everything, everything was in place, so what could be the possible reason ? Then all of a sudden, somehow i just fixed the issue like hitting my hand everywhere or shooting the arrow in dark. I was so desperate. So thing that was causing this issue was, during resgistration of app on dev.twitter.com, Callback URL value wasn’t provided. Yeah i didn’t provide any callback url because it was optional, so i skipped it. So i provided a callback url, you can provide anything there. Now you’l find that above error doesn’t happens anymore and you can start tweeting from your app.

“error – Read-only application cannot POST”

Second issue i faced was this, and this one is very easy to figure out and fix it. It means that your registered twitter app is read only. So go to settings page of your application and make it “read and write”. Thats it.

I am showing you the screenshot of how settings will look like.

Adios.

gl_java_ubuntu1

Here are the detailed steps to set up or install JOGL on your ubuntu. First of all you have to download the jogl library(according to your machine) from here . I am using Ubuntu 8.04 on intel board so i downloaded jogl-1.1.1-linux-i586.zip.

So extract the zip file. Now open the folder that you have just now extracted. Go to lib folder.

STEP 1: Now you have to copy four .so files i.e libjogl.so, libjogl_awt.so, libjogl_cg.so, libgluegen-rt.so to [javainstalation directory]/jre/lib/i386

Open the terminal window and type: sudo cp libjogl.so /usr/lib/java/jdk1.6.0_13/jre/lib/i386

similarly you have to copy other three .so files.

*you have to provide you java installation path*

jogl1

Now you can check the destination if files has been copied.

jogl21

STEP 2: Now copy jogl.jar and gluegen-rt.jar to [java installation di]/jre/lib/ext

run this command to copy: sudo cp jogl.jar /usr/lib/java/jdk1.6.0_13/jre/lib/ext

simillarly copy gluegen-rt.jar

*you have to provide your java installation path*

jogl3

You can check:

jogl4

STEP 3: SO thats it. Now lets test the simple jogl code.

import javax.media.opengl.GLCapabilities;

public class Hello{
public static void main (String args[]) {
try {
System.loadLibrary(“jogl”);
System.out.println(
“Hello World! (The native libraries are installed.)”
);
GLCapabilities caps = new GLCapabilities();
System.out.println(
“Hello JOGL! (The jar appears to be available.)”
);
} catch (Exception e) {
System.out.println(e);
}
}
}

Now compile this Hello.java and the run it. Here is the output:

jogl-final

So thats it. Now your are off to fly and write your jogl apps.

Thanks.