ScrollView height 0 even when fill_parent - android

I have a linearlayout A containing a imageview and scrollview. The scrollview contains a second linearlayout B
The idea is to have a fixed header while the linearLayout B contains multiple views that match exactly the height of the scrollview. See it as a tabview where all tabs are after each other in a scrollview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/main_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical">
<ImageView android:id="#+id/logoImg"
android:layout_width="fill_parent"
android:gravity="center"
android:src="#drawable/logo"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/scrollView"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/scrollLinear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</ScrollView>
</LinearLayout>
I tried to achieve this by calling getHeight on the scrollView and then set this height to each view in the linearLayout B but getHeight always returns 0.
Anyone an idea what's the easiest way to achieve this?

Well, the question is very old, but I'll give it a try :)
Wouldn't it be lack of "fillViewport" in ScrollView?
<ScrollView
android:id="#+id/scrollView"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>

Related

Scroll view does not work

I have a scroll view that I fill dynamicly in my code. I expect if I put too much content to the parant view that the view becomes scrollable. This does not work.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:id="#+id/layoutScoreBoardHeader"
android:layout_marginTop="15dp">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutScoreBoard"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_below="#id/layoutScoreBoardHeader">
</LinearLayout>
</RelativeLayout>
</ScrollView>
Bad layout structure. RelativeLayout should wrap_content. In your case it will be same size as initial ScrollView and it will ignore child size.
I have it. Did not know that there is a special view like horizontal scroll view. But thank you guys for your feedback!

Blank area shows on top and bottom of the image in ScrollView

I have a problem that when i add ImageView in ScrollView it shows empty space on top and bottom both side i want to show image on activity on full screen with width of fill screen and if image is long enough then screen then image will be scrollable. this code some how solve the issue but also showing empty area on top and bottom of the image.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.alicon.digi.book.ScreenSlideActivity" >
<ImageView
android:id="#+id/bookImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/page_of_book"
android:src="#drawable/book_title" />
</ScrollView>
Please try this and pay attention to fillViewport property of ScrollView.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
**android:fillViewport="true"** //this is imp, see the link for a very good explanation
tools:context="com.alicon.digi.book.ScreenSlideActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/bookImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/page_of_book"
android:src="#drawable/book_title" />
</LinearLayout>
</ScrollView>
fillViewPort
add android:scaleType="fitXY" attribute to the ImageView XML and change android:layout_height and android:layout_width attributes' value from wrap_content to fill_parent. Use LinearLAyout instead of ScrollView as follows...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.alicon.digi.book.ScreenSlideActivity" >
<ImageView
android:id="#+id/bookImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/page_of_book"
android:src="#drawable/book_title" />
</LinearLayout>

Layout issue on Android with FrameLayout and ScrollView

I'm trying to split the screen in 2 areas, to the left an ImageView and to the right a ScrolView. I'm adding the ImageView and the content of the ScrollView programatically, so the xml file of the layout looks like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/scene_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
>
</FrameLayout>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="#dimen/scrollmenu_width"
android:layout_gravity="right"
>
<LinearLayout
android:id="#+id/scrollmenu"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="fill_parent"
>
</LinearLayout>
</ScrollView>
</FrameLayout>
What am I doing wrong? because I'm getting the ScrollView to the right, but the ImageView centered (relative to the screen) placed on the left of the screen. The resolution of the image exceeds the screen's resolution so I get black sp
I think you should make use of the LinearLayout and the weight parameter to solve this problem.
I have edited your snippet to give you an idea of how you should use it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/scene_view"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight=1>
</FrameLayout>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight=1
android:layout_gravity="right">
<LinearLayout
android:id="#+id/scrollmenu"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="fill_parent">
</LinearLayout>
</ScrollView>
</FrameLayout>
I hope it helps..

Android LinearLayout within LinearLayout?

I tried to make a simple layout like so
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="world" />
</LinearLayout>
Only the first TextView ("hello") is displayed. What am I doing wrong?
Make layout_height of inner layout wrap_content.
Your second LinearLayout is set to fill_parent in the layout's height. This is causing it to push out everything that's below it that was placed into the first LinearLayout. Change it to wrap_content and it should work.

Android ListView doesn't expand the whole screen

I've problem similar to the one here: Android ListView doesn't expand the whole screen?
The XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/ResultLayout">
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent" android:id="#+id/ListView"
android:layout_height="fill_parent"></ListView>
</ScrollView>
<LinearLayout android:id="#+id/pagingPanel"
android:gravity="center" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"></LinearLayout>
<Spinner android:layout_width="fill_parent" android:id="#+id/ManSpinner"
android:layout_height="fill_parent" />
</LinearLayout>
In this case, I should have a ScrollView or the PagingPanel LinearLayout will disappear.
Edit
The desired layout is to have all the elements stacked on top of each other. but if the elements exceed the page height, a scroll should be added to the ListView.
If you have a vertical LinearLayout, You can't have multiple fill_parent for layout_height of your LinearLayout elements. What layout you would like to achieve? Uniformly divide screen for all elements?
Another problem is that using ListView in ScrollView is not a good idea also because ListView itself is scrollable.
You should write what layout you would like to achieve with your XML. Also generally is a good practice to write your XML in multiple steps and iterate to woking solution layout by layout.
EDIT: Ok, try something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ResultLayout">
<ListView android:id="#+id/ListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout android:id="#+id/pagingPanel"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<Spinner android:id="#+id/ManSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Categories

Resources