Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
Posts: 1
Registered: ‎01-27-2010
My Device: blackberry
My Carrier: all

X and Y coordinate offsets when scrolled?

Hello,

 

I know in other mobile devices window.pageXOffset and window.pageYOffset can be accessed with Javascript to find the amount of pixels the entire page has been scrolled. When I try to access these on a Blackberry, all I get for both values is 0.

 

Can anyone help me find out these offset values?

 

Thank you very much.

Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: X and Y coordinate offsets when scrolled?

Hello there, thanks for your question.

 

The pageXOffset and pageYOffset JavaScript methods are not formally supported by the BlackBerry browser.  They are treated as constants and return 0 when called.  They reflect the current horizontal and vertical pixel location of the top-left corner of the document within the window.

 

 

Please see the following JavaScript Reference guide for a complete list of supported objects:

http://docs.blackberry.com/en/developers/deliverables/11849/JS_objects_568399_11.jsp

 

Sincerely,

Adam

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Contributor
Posts: 17
Registered: ‎04-13-2010
My Device: 9550
My Carrier: Airtel

Re: X and Y coordinate offsets when scrolled?

I am also stucked in the same problem. i also need to store the value of scroll position.

 

Have any one found any alternate idea?

Please Post it?

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: X and Y coordinate offsets when scrolled?

If you are looking for a way to handle scroll position in a BlackBerry Widget, you could create a JavaScript extension that would read what the scroll position of the Manager is that contains the web content.  This would involve writing Java code and looking at the BlackBerry widget template source code found in the "device_templates" directory of the Widget SDK

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
Posts: 17
Registered: ‎04-13-2010
My Device: 9550
My Carrier: Airtel

Re: X and Y coordinate offsets when scrolled?

Hi Neil,

 

Thanks for your quick reply. can you suggest me the sample code of this? and also how to imbed it with widget SDK,

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: X and Y coordinate offsets when scrolled?

Hi mishra1982,

 

A video explaining how to create a JavaScript extension can be found here:

http://www.blackberry.com/DevMediaLibrary/view.do?name=WritingJavaScriptExtensions

 

I will see if I can dig up an example of how you would detect the scroll position in Java

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
Posts: 17
Registered: ‎04-13-2010
My Device: 9550
My Carrier: Airtel

Re: X and Y coordinate offsets when scrolled?

Hi Neil,

 

I have create instance of WidgetScreen;

like below:

 

public final class ScrollSample extends Scriptable {
    public static final String FIELD_SCROLL_SUPPORTED = "scrollValue";
    WidgetScreen ws;

    public ScrollSample() {
    }

    public Object getField(String name) throws Exception {
        if (name.equals(FIELD_SCROLL_SUPPORTED)) {
            return new Integer(ws.getVerticalScroll());
        }        
        return super.getField(name);
    }
}

then after i maintain features list as : 

public final class SampleExtension implements WidgetExtension {

    public String[] getFeatureList() {
        String[] result = new String[1];
        result[0] = "sample";
        return result;
    }

    public void loadFeature(String feature, String version, Document doc,
            ScriptEngine scriptEngine) throws Exception {

        if (feature == "sample") {
            scriptEngine.addExtension("sample.scroll", new ScrollSample());
        }
    }

    public void register(WidgetConfig arg0, BrowserField arg1) {
        // TODO Auto-generated method stub
    }

    public void unloadFeatures(Document arg0) {
        // TODO Auto-generated method stub
    }
}

 

But still my problem is not resolved. I am getting nothing in javascript.

 

Please guide to resolved this problem.

 

Thanks

Sunil

 

 

Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: X and Y coordinate offsets when scrolled?

Sunil,

 

Great work in building your own JavaScript extension!  Can you clarify what your "WidgetScreen" object is and does?

 

In your SampleExtension class, you need to save a reference to the BrowserField object that is provided as a parameter to the "register" method.  Then pass this reference into the ScrollSample constructor within the loadFeature method.  Like this:

 

 

public final class SampleExtension implements WidgetExtension {

private BrowserField _bf = null;

public String[] getFeatureList() {
String[] result = new String[1];
result[0] = "sample";
return result;
}

public void loadFeature(String feature, String version, Document doc,
ScriptEngine scriptEngine) throws Exception {

if (feature == "sample") {
scriptEngine.addExtension("sample.scroll", new ScrollSample(_bf));
}
}

public void register(WidgetConfig arg0, BrowserField arg1) {
// TODO Auto-generated method stub
this._bf = arg1;
}

public void unloadFeatures(Document arg0) {
// TODO Auto-generated method stub
}
}

 

 

 

Then, within any ScriptableFunction class you create within your extension, you can access the necessary properties from the browserField object that you are interested in returning.

 

 

It is important to note that the browserField object is embedded within a field manager, which performs the actuall scrolling (the browserField itself does not scroll content).  So when you are retrieving the scroll position, make sure to retrieve it from the proper instance.

 

For example, call getHorizontalScroll on the browser field object's manager, not the browser field instance itself, like this:

 

 

return new Integer(_bf.getManager().getHorizontalScroll());

 

 

Good luck,

Adam

 

 

 

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: X and Y coordinate offsets when scrolled?

Just wanted to post some more source code, that completes the proposed widget extension I described:

 

Here's a Scriptable class named "ScrollSample".  This is registered as an extension from the SampleExtension class:

 

package widgetpackage;

import net.rim.device.api.script.Scriptable;
import net.rim.device.api.browser.field2.BrowserField;

public final class ScrollSample extends Scriptable 
{	
	public static final String FIELD_GET_POSITION = "getScrollPosition";

	private getScrollPosition callGetScrollPosition;

	public ScreenInfo(BrowserField bfRef) 
	{
		//browserField instance will be referenced by the following objects:
		this.callGetScrollPosition = new getScrollPosition(bfRef);
	}
	
	public Object getField(String name) throws Exception 
	{
		if (name.equals(FIELD_GET_POSITION)) 
		{
			return this.callGetScrollPosition;
		}
		
		return super.getField(name);
	}
	
}

 

Here is a Scriptable Function class that evaluates the manager of the browser field object and returns the horizontal or vertical scroll position :

 

 

package widgetpackage;

import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.browser.field2.BrowserField;

public final class getScrollPosition extends ScriptableFunction {

	private BrowserField field = null;
	
	//Constructor saves reference to browserField instance;
	public getScrollPosition(BrowserField bfRef)
	{
		this.field = bfRef;
	}
	
	//return the current scroll position (default = vertical)
	public Object invoke(Object obj, Object[] args) throws Exception
	{
		String axis = "y";
		
		if (this.field == null) return UNDEFINED;
		
		if (args.length == 1) axis = (String)args[0];
		
		if (axis.toLowerCase().equals("x")) 
			return new Integer(this.field.getManager().getHorizontalScroll());

		return new Integer(this.field.getManager().getVerticalScroll());
	}
}

 

 

 

And finally, here is some sample JavaScript that shows how to use this in your BlackBerry Widget:

 

 

<script type="text/javascript">
function displayScreenInfo() {
   alert("Horizontal Scroll: " + sample.scroll.getScrollPosition("x"));
   alert("Vertical Scroll: " + sample.scroll.getScrollPosition("y"));
}
</script>

 

 

Cheers,

Adam

 

 

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Contributor
Posts: 17
Registered: ‎04-13-2010
My Device: 9550
My Carrier: Airtel

Re: X and Y coordinate offsets when scrolled?

Hi Adam,

 

Thanks a lot for your help. It's working fine. it's quite useful for my project.

 

I was also looking around the alternate i.e:" Blackberry Phonegap". but it also not working.

 

Thanks Again.

 

-Rgards

Sunil Mishra