Device orientation does not reflect scrollview content resizing.

You must Login before you can answer or comment on any questions.

I have scrollview resize problem when device orientation changed.

Check the code which I use for scrollView and orientation:

function CreateLibraryControl(){

var libraryView = Titanium.UI.createWindow(root.combine($$.flexibleWindow, { title: 'Library', backgroundColor: '#fff', navBarHidden: false, top: 0, left: 0 })); var libraryScroll = Ti.UI.createScrollView({ contentHeight: 'auto', backgroundColor: '#FF0000', showVerticalScrollIndicator: true, showHorizontalScrollIndicator: true, top: 0, left: 0 });

var topPosition = 0; for (var i = 0; i < 10; i++) { // Create section var section = Ti.UI.createView({ backgroundColor: '#0099AA', height: 210, top: topPosition, backgroundImage: 'images/librarybg.jpg' });

    libraryScroll.add(section);

    var sectionLabel = Ti.UI.createLabel({
        text: 'Hello',
        height: 20,
        width: '100%',
        backgroundColor: '#445566',
        backgroundImage: 'images/analysis_button_bg.jpg',
        top: 0,
        font:{fontWeight:'bold',fontSize:12},
        color: '#000000'
    });
    section.add(sectionLabel);
    topPosition = topPosition + 210;
}

libraryView.add(libraryScroll);

Titanium.Gesture.addEventListener('orientationchange', function(e){ if (e.orientation >= 1 && e.orientation <= 4) {

        if (e.orientation == 1 || e.orientation == 2) {
    libraryScroll.height = 367;
            libraryScroll.width = 320;
    libraryScroll.contentHeight = topPosition;
            libraryScroll.contentWidth = 320;
        }
        else 
           if (e.orientation == 3 || e.orientation == 4) {
        libraryScroll.height = 219;
                libraryScroll.width = 480;
        libraryScroll.contentHeight = topPosition;
                libraryScroll.contentWidth = 480;
            }
    }
});

return libraryView; }

In the above function 'libraryView' is the window which we'll create calling the function and open. In the above function we add few titles and views in scrollview, so, scrollview gets expanded. We have also maintained orientation function, so, on device orientation change, scrollview's content height/width can be set. Please check the orientation function carefully.

Please see here as I state the problem: When I open the window, it displays perfect. Scrolls only vertically because there's no horizontal scroll. And when I change orientation to Landscape, then also it displays perfect, only vertical scroll, no horizontal. But when I change back to Portrait orientation mode, scrollview displays both vertical as well as horizontal scrolls. Even I have manually set the content width to 320, It displays same. I don't know how horizontal scroll is being enabled.

Can someone please suggest me any solution..?

— asked 2 years ago by Paresh Thakor
2 Comments
  • sorry for improper style..!

    — commented 2 years ago by Paresh Thakor

  • Use three "~" at beginning and at the end of the source code to format it! :)

    — commented 2 years ago by Ivan Škugor

1 Answer

Accepted Answer

Not sure if this helps but I had a similar problem and solved it using the code below.

// make sure we resize on rotate
Ti.Gesture.addEventListener('orientationchange', function(e){
    Ti.API.info('test gest func');
    masterScrollView.left=0;
    masterScrollView.right=0;
 
});
Basically when the device is rotated it ensures that my masterScrollView resized.

Your Answer

Think you can help? Login to answer this question!