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

Java Development

Reply
Trusted Contributor
bbdev12345
Posts: 117
Registered: ‎09-06-2011
My Device: 9900

Looking for SliderField expert! - How can i get the sliderfield to reset when released?

hello,

 

 

I am trying to figure out how to modify the example of SliderField so it turns into a kind of lock which goes back to beginning if the user doesn't swipe to the other side in one swipe.

 

Does anyone have any example of doing this or can help me with my problem which is that I can insert the following into the SliderField class and it seems to send the slider back to the beginning fine on the storm 2 simulator (when release mouse button) but on my 9900 device the slider just sticks wherever i release my thumb.

 



    protected void fieldChangeNotify(int context) {
       
        if (_currentState  != _numStates)
        {
                        _currentState = 0;
        }
        
        invalidate();
        super.fieldChangeNotify(context);
    }

protected boolean touchEvent(TouchEvent message)
    {
        boolean isConsumed = false;
        boolean isOutOfBounds = false;
        int x = message.getX(1);
        int y = message.getY(1);
        // Check to ensure point is within this field
        if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
            isOutOfBounds = true;
        }
        switch(message.getEvent()) {
            case TouchEvent.CLICK:
            case TouchEvent.MOVE:
                if(isOutOfBounds) return true; // consume
                _selected = true; // Pressed effect
                
                // update state
                int stateWidth = getExtent().width / _numStates;
                int numerator = x / stateWidth;
                int denominator = x % stateWidth;
                if( denominator > stateWidth / 2 ) {
                    numerator++;
                }
                _currentState = numerator;
                invalidate();
                
                isConsumed = true;
                break;
            case TouchEvent.UNCLICK:
                if(isOutOfBounds) {
                    _selected = false; // Reset presssed effect
                    return true;
                }
                
                // A field change notification is only sent on UNCLICK to allow for recovery
                // should the user cancel, i.e. click and move off the button
    
                _selected = false; // Reset pressed effect
                
                // Update state
                stateWidth = getExtent().width / _numStates;
                numerator = x / stateWidth;
                denominator = x % stateWidth;
                if( denominator > stateWidth / 2 ) {
                    numerator++;
                }
                _currentState = numerator;
                invalidate();
                
                fieldChangeNotify(0);
                
                isConsumed = true;
                break;
        }
        return isConsumed;
    }

 

 

thanks.

Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Looking for SliderField expert! - How can i get the sliderfield to reset when released?

It's not working on the 9900 because you are doing your release in the unclick handler which works fine on the Storm series with the surepress screens, but the 9900 has a proper touch screen and there are any instances where unclick will never get called. You will need to move it to the up handler of find a way to make sure unclick gets called.
Please use plain text.
Trusted Contributor
bbdev12345
Posts: 117
Registered: ‎09-06-2011
My Device: 9900

Re: Looking for SliderField expert! - How can i get the sliderfield to reset when released?

Thanks CMY for taking the time to answer my question.

 

I have actually gone down the route you suggested and now have the slider returning to 0 when released on 9900 and 9550.

 

I am just concerned now by the fact that the trackpad on 9900 doesn't do any sliding with the new code so for non touch screen devices this code actually won't work.

 

Any solutions? 

 

protected boolean touchEvent(TouchEvent message)
    {
        boolean isConsumed = false;
        boolean isOutOfBounds = false;
        int x = message.getX(1);
        int y = message.getY(1);
        // Check to ensure point is within this field
        if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
            isOutOfBounds = true;
        }
        switch(message.getEvent()) {
            case TouchEvent.CLICK:
               Log.info("CLICK");
                
                case TouchEvent.UP:
                    Log.info("up");
                     fieldChangeNotify(0);
                invalidate();
                dontDoClick = true;
                    break;
            case TouchEvent.MOVE:
                
                   Log.info("move");
                   if(dontDoClick)
                   {
                       dontDoClick = false;
                       break;
                   }
                if(isOutOfBounds) return true; // consume
                _selected = true; // Pressed effect
                
                // update state
                int stateWidth = getExtent().width / _numStates;
                int numerator = x / stateWidth;
                int denominator = x % stateWidth;
                if( denominator > stateWidth / 2 ) {
                    numerator++;
                }
                _currentState = numerator;
                invalidate();
                
                isConsumed = true;
                break;
            case TouchEvent.UNCLICK:
                   Log.info("unCLICK");
                if(isOutOfBounds) {
                        _selected = false; // Reset presssed effect
                    return true;
                }
                
                // A field change notification is only sent on UNCLICK to allow for recovery
                // should the user cancel, i.e. click and move off the button
    
                _selected = false; // Reset pressed effect
                
                // Update state
                stateWidth = getExtent().width / _numStates;
                numerator = x / stateWidth;
                denominator = x % stateWidth;
                if( denominator > stateWidth / 2 ) {
                    numerator++;
                }
                _currentState = numerator;
                 fieldChangeNotify(0);
                invalidate();
                
               
                
                isConsumed = true;
                break;
        }
        return isConsumed;
    }

 

Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Looking for SliderField expert! - How can i get the sliderfield to reset when released?

What you could (and what I do in several situations is have the up/unclick and down/click handlers do the same thing but then you have to check to make sure the event isn't fired twice. Or you could just change the code to make it work in the navigation move method.
Please use plain text.