Android Landscape mode cutting off my buttons on top
Asked Answered
S

4

14

I am having an issue with my application. I recently added ScrollView as the Parent of LinearLayout. When flip my device orientation to Landscape, my buttons get cut off on the top. I can see the last button just fine with extra "black space" at the end of it when I scroll down, only the top gets cut off. I looked around and I am not sure why this is happening. Here are some pictures so you can understand how it looks.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center_vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">
    <Button 
        android:id="@+id/BeefButton" 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/beefbutton"
        android:text="Beef"
        android:textColor="#FFFFFF"
        android:textSize="32px"
        android:textColorHighlight="#FF6600"/> 
    <Button 
        android:id="@+id/PoultryButton" 
        android:layout_height="wrap_content"
        android:layout_gravity="center" 
        android:layout_width="match_parent"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/chickenbutton"
        android:text="Poultry"
        android:textColor="#FFFFFF"
        android:textSize="32px"        
        android:textColorHighlight="#FF6600"/>
    <Button 
        android:id="@+id/FishButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/fishbutton"
        android:text="Fish"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
    <Button 
        android:id="@+id/PorkButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/porkbutton"
        android:text="Pork"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
    <Button 
        android:id="@+id/VegetablesButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/vegetablesbutton"
        android:text="Vegetables"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
</LinearLayout>
</ScrollView>
Starry answered 17/12, 2010 at 18:19 Comment(3)
Please post your layout. You have the same one for portrait and landscape modes?Tacklind
Also, is there a reason you aren't using a ListView, as opposed to a LinearLayout with a ScrollView? Are your list items always exactly the same, and only 5 of them?Tacklind
Yes they are exactly the same way, only five itemsStarry
F
23

remove the android:layout_gravity="center" on your linearlayout , when you change it to landscape mode the height of the scrollview is bigger than your screen size, so its centering the linearlayout in the ScrollView, although removing it may not be the solution, that is what is causing this,

you may consider revising your layout, it might make more sense to use a listview to be able to successfully support landscape

Fubsy answered 17/12, 2010 at 18:54 Comment(2)
There is no way to have my items listed in the center like that besides that though right? :-(Starry
You can add "layout_gravity="center"" on your ScrollView and have it wrap_content. This will result in the View being center in it's container when there is enough room and scrolling correctly when there isn't.Circumpolar
I
2

I know this is an old thread, but I spent two days on this problem. While removing the "layout_gravity" statement does help the cut off issue, the comment on not being able to center is also true. Ultimately what worked for me, which in my opinion is a hack, was to have the scrollview on top of a layout centered and TWO linearlayouts cascaded as children. Also the height and width had to be wrap content on the scrollview and linearlayouts. This is not the neatest solution, but I tried everything with no success, including setting the gravity in code with setlayoutparams if the objects were less than the screen size width. I hope this helps someone.

Incunabulum answered 6/10, 2011 at 3:45 Comment(0)
S
0

The hint (removing android:layout_gravity="center") that @slim said does work but with no central alignment.

My Solution:

I fixed it by wrapping the main long layout with a LinearLayout with its gravity set to center and letting android:layout_gravity="center" remain for the main layout:

<ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

          <!-- .... the main long layout ... -->

        </LinearLayout>

</ScrollView>
Sorghum answered 29/7, 2013 at 9:44 Comment(0)
C
0

You can use android:layout_gravitiy="center" with android:fillViewport="true"

Example a center box without cut off in landscape (Kotlin) xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fillViewport="true"
  android:layout_gravity="center">

   <androidx.constraintlayout.widget.ConstraintLayout
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

       <RelativeLayout
           android:id="@+id/layout"
           android:layout_width="340dp"
           android:layout_height="443dp">

         <!-- .... box perfect center do something in it... -->

       </RelativeLayout>
   </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Copula answered 18/11, 2022 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.