This is my layout of the scrollView. It just scrolls infinitely. I need the scroll just for the elements that the user sees in the activity. I have tried all my efforts to it however, it didnt work out. Heres the layout..
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginLeft="-20dp"
android:layout_marginStart="-20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="100dp"
android:layout_marginStart="100dp"
android:gravity="center"
android:text="Create event" >
</Button>
</LinearLayout>
Set the height of the LinearLayout (id parentLayout) to wrap_content.
The ScrollView is set to wrap_content, but its child is set to fill_parent. This makes no sense, as something needs to provide a size.
Related
i have an app which has a main RelativeLayout and some LinearLayout within it.
My problem is that the LinearLayout doesn't fit to the full lenght of the screen:
this is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bgApp" >
<LinearLayout
android:id="#+id/mainView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/topBarHeight"
android:background="#drawable/top"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#android:color/transparent" >
<ImageView
android:id="#+id/scoreLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coin_icon"
android:contentDescription="#string/app_name"/>
<TextView
android:id="#+id/mainScreenTotalPoints"
style="#style/pointTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/btnPaddingV"
android:text="#string/startPoint"
android:textSize="#dimen/buttonTextSize" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/logPadding"
android:text="#string/app_name"
android:textColor="#d0105e"
android:textSize="27sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="#dimen/btnPadding"
android:paddingRight="#dimen/btnPadding" >
<Button
android:id="#+id/play"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:background="#drawable/button_blue"
android:gravity="center"
android:onClick="onButtonClickPlay"
android:text="#string/play"
android:textColor="#color/btnMain"
android:textSize="#dimen/buttonTextSize" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:orientation="vertical"
android:paddingTop="#dimen/mainbox" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/btn_how"
style="#style/btnCapital"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:layout_marginRight="#dimen/btnPaddingV"
android:layout_weight="1.0"
android:background="#drawable/button_green_how"
android:onClick="onButtonClickHowToPlay"
android:text="#string/howInfo" />
<Button
android:id="#+id/btn_rate"
style="#style/btnCapital"
android:layout_width="fill_parent"
android:layout_height="#dimen/buttonHeight"
android:layout_marginLeft="#dimen/btnPaddingV"
android:layout_weight="1.0"
android:background="#drawable/button_rate"
android:onClick="onButtonClickRate"
android:text="#string/rateInfo" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/sound"
style="#style/btnCapital"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:layout_marginTop="1.0dip"
android:layout_weight="1.0"
android:background="#drawable/mute_01"
android:gravity="center"
android:onClick="onButtonClickSound"
android:text="#string/mute" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/btnPadding" >
<Button
android:id="#+id/moreapp"
style="#style/btnCapital"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:layout_marginTop="1.0dip"
android:layout_weight="1.0"
android:background="#drawable/button_blue"
android:gravity="center"
android:onClick="onButtonClickMoreApp"
android:text="#string/moreapp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
android:gravity="bottom" />
</LinearLayout>
</LinearLayout>
</ScrollView>
here is a screenshot :
Screenshot after running app
and here a screenshot of layout before running app:
Screenshot before running app
Thank you in advance !
You need to use the ScrollView attribute called android:fillViewport="true". This attribute causes the scroll view’s child to expand to the height of the ScrollView if needed.
When the child is taller than the ScrollView, the attribute has no effect.
Check out this for detail explanation.
You have to use attribute android:fillViewport="true".in your scrollview.
I have two linear layouts and ScrollView in my layout.
I add a few views to ScrollView programmatically.
All is okay, if views in ScrollView are inside of the screen.
But if more views in ScrollView, the first view of scrollview
is under layout2 and partly invisible.
I tryed relative layout. But the same performance.
Of course I'd like to see top element of scrollview without truncating.
My XML is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backNormal"
android:orientation="vertical"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/backNormal"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageDriverWaitingBack"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_weight="1"
android:src="#drawable/back"
android:gravity="left"
/>
<TextView
android:id="#+id/textDriverWaitingTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:gravity="center"
android:textColor="#color/text1"
android:layout_weight="2.13"
/>
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:gravity="right"
android:layout_weight="1"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/backDark2"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/backNormal"
android:orientation="horizontal"
>
<Button
android:id="#+id/dButtonWaitMinus"
android:text='-'
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
style="#style/button_style_normal"
android:layout_weight="1"
/>
<TextView
android:id="#+id/dTextWaitDist"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Distance"
android:gravity="center"
android:textColor="#color/text1"
android:layout_weight="9"
/>
<Button
android:id="#+id/dButtonWaitPlus"
android:text='+'
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
style="#style/button_style_normal"
/>
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/driverWaitList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/backNormal"
android:orientation="vertical"
android:layout_gravity="center_horizontal|center_vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
Any ideas please! Thanks!
I need the following thing:
3 layouts:
Header
Content
Footer
Header must be on the top footer must be sticked to the bottom (android:layout_alignParentBottom="true").
But how to make middle to occupy the whole other screen? Thanks.
Here is my solution (with android:layout_weight and ScrollView):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#DDDDDD"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"
tools:ignore="HardcodedText" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Content"
tools:ignore="HardcodedText" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#DDDDDD"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer"
tools:ignore="HardcodedText" />
</LinearLayout></LinearLayout>
And the result picture :
Hi Use the following lines
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Button" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/linearfooter"
android:layout_below="#+id/button1"
android:background="#android:color/darker_gray"
>
</LinearLayout>
<LinearLayout
android:id="#+id/linearfooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer" />
</LinearLayout>
</RelativeLayout>
hope this helps you.
Use a height of 0dp and
android:layout_weight
set to 1
Finally I've got the next solution:
To the middle layout I added:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:layout_weight="1">
And it works!
I am trying to make my button centered horizontally and 2/3 down from the top vertically. I want it to remain 2/3 from the top no matter what the screen size/density. Here is the button I am trying to move:
<Button
android:id="#+id/btnResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="250dp" />
Thanks!
Working Code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<Button
android:id="#+id/btnResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<TextView
android:id="#+id/tvCounter2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
To archive this you need to use LineareLayout and explore weight feature. To get it work it must be something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<Button
android:id="#+id/btnResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
Hope it helps!
You can use the attribute "layout_weight"
So I want a linearLayout at the top of the screen that has 2 buttons, a linearLayout at the bottom that also has two buttons and a listView in the middle. The problem I'm running into is that the bottom linearLayout gets covered up if the listView gets too big. I'm telling the listView to wrap_content as it's layout_height because I want to do that...but I almost positive thats the reason the bottom linearLayout isn't showing.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/importClasses"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
</Button>
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/Title"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical|center"
>
</TextView>
<Button
android:id="#+id/addClasses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ListView
android:id="#+id/classList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/topLayout" >
</ListView>
<LinearLayout
android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/classList"
>
<Button
android:id="#+id/Assignments"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
</Button>
<Button
android:id="#+id/flashCards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
Any suggestions?
Set classList to be above bottomLayout and below topLayout.
Also set bottomLayout to layout_alignParentBottom="true", and topLayout layout_alignParentTop="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_alignParentTop="true">
<Button
android:id="#+id/importClasses"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
</Button>
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/Title"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical|center"
>
</TextView>
<Button
android:id="#+id/addClasses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ListView
android:id="#+id/classList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottomLayout"
android:layout_below="#+id/topLayout" >
</ListView>
<LinearLayout
android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/Assignments"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
</Button>
<Button
android:id="#+id/flashCards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
This should work, but you may need to change your RelativeLayout to LinearLayout.
And change the layout_height of your ListView to 0dp and add layout_weight="1" to it.