Saturday, February 26, 2011

Adjustresize issue on multiple editexts in a scroll view



OK i had a horrid time figuring out how to implement a scroll view in the above case. When the keyboard opened up it hardly space of about 2 fields to be viewed in an 320X480 (MDPI) device.
Also, whenever the user clicked on the views which arent in display, meaning which required to be scrolled. The scrollview never scrolled to that item. Try having the android:fillViewPort="true" for the scrollview it still dint work. I tried removing the title and it worked as a charm. But that was not the option to be used. Because the application i was building required the title to be present and be of exactly 160dip :( bad design but i guess i had to live with it. But i always wondered how did it work when it was full screen. My colleague Amit Chintawar gave a explanation that there isnt enuf space for the scrollview to scroll, which I dont completely agree, the scroll view has to scroll to that position by itself.

However finally i found a tweak and it made it possible.

Solution was to use the ever so charming "RelativeLayout" to draw the screen.
Have your title layout placed in the outer Relatvie Layout with a height of 160 dip.
Now place your scroll view also in the same outer layout as it is.

The first item in the Linear layout which is mandatory to have within the scrollView (by mandatory i mean a layout is mandatory to be kept) so coming back to the first item, have a android:layout_marginTop as 160dip. So eventually both the title layout and scroll view gets to start of from the top of the screen. Just that for the user it will appear after the title layout, and yes even within the small display it will scroll to even the last item in the screen.

Ok, before i forget one of the most important things. In the outer relative layout have the scrollView first and then the title relative layout, reason if your first item has some specific background then it will be drawn over the title layout causing the title never t be displayed ever.

So the layout file would look like below:

RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true"
LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
item 1 with margin of 160 dip
item 2
....
item 14
LinearLayout

ScrollView

RelativeLayout android:layout_width="fill_parent" android:layout_height="160dip"
Title
RelativeLayout

RelativeLayout

This will ensure that however long you items in the scroll view goes it will still get that item in display when it being tapped and the Keypad is opened.

I hope this would help some developer trying to build a scroll view with many text views in it and to get it scrolled on all the devices.

P.S. The problem occured in the emulator of Android 1.6 as well as the SonyEricsson device Xperia X8 at 1.6 maybe it doesnt happen on Android's latest version but still a problem on 1.6 for sure.

Signing Off,
J
(proud to be a Anddev)

No comments:

Post a Comment