My scrollview doesn't work at all - android

I can't make a ScrollView properly scrolling. It always cut off the content on the bottom, as if it were a normal LinearLayout.
My code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.android.dadiperpus.MainActivity">
I put LinearLayout inside ScrollView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo_stan"
android:orientation="vertical"
android:weightSum="10">
Then I put RelativeLayout inside LinearLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="CONTOH BRO"
android:textSize="32sp" />
</RelativeLayout>
Here I use Gridlayout
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
Then I put couple of this code inside GridLayout
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo_stan" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pengantar Kepabeanan"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
I already tried to wrap ScrollView inside LinearLayout and it didn't change anything at all.

I put LinearLayout inside ScrollView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo_stan"
android:orientation="vertical"
android:weightSum="10">
This linearlayout needs to use wrap_content for its height. By using match_parent, you're saying you want the linearlayout to only be as tall as the scrollview that's hosting it... which means it will cut off all the "scrollable" content. By using wrap_content (or some fixed height that's larger than the scrollview) you will have more content in the linearlayout than is visible on one screen, and then scrolling will work as expected.

On removing weightsum from Linear Layout, layout_weight from Relative Layout and Grid Layout , your scrollview will scroll (only in case if content is more than screen size). Give height to all these layouts ( Relative Layout and Grid Layout) wrap_content instead of 0dp.
Actually what happen in your code is : You have given Linear Layout's height match_parent (equal to screen size) and weightsum 10. And Then you give Relative Layout layout_weight = 2 and grid layout's layout_weight = 8 which means relative layout is taking 2 parts of screen and grid layout is taking 8 parts of screen. In this case, your content will never grow more than scree size and grid layout try to fit in screen due to which, your content get cut off at bottom.
Hope, It will help you.
Thanks

Related

Right way to have a scrollable bottom view

What is the right way to have a scrollable bottom view?
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/wrnTextP1"
android:text="Ok , batatas "/>
<android.support.v4.widget.Space
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
<Button
style="#style/FullWidthButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Chega por hoje!" />
</LinearLayout>
at the preview its looke fine, but when I run it on the emulator the view(in this case: the button) its not at bottom at all.
I think you are misunderstanding how ScrollView is meant to be used. The LinearLayout child of your ScrollView has a match_parent height... if your ScrollView is the same height as the screen, and the LinearLayout is the same height as the ScrollView, then there's nothing to scroll (all the content is the size of the screen).
The direct child of a ScrollView should either have a fixed height (like 2000dp) or use wrap_content.
If you use a fixed height, then the rest of your code "works"; you can have a Space element that uses layout_weight to push the last view down to the bottom of your LinearLayout. However, if you're using wrap_content, this is impossible (since there's no "extra" space for the layout_weight to consume).
Instead, you could consider putting the "bottom" view outside of the ScrollView. Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
...
</ScrollView>
<Button
style="#style/FullWidthButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chega por hoje!" />
</LinearLayout>

Android: nested linearlayouts error

I am trying to create, as a test, a row of three images (evenly spaced with a vertical orientation) in the top half of my screen. For some reason, android:layout_height="0dp" is giving me an error, saying it will make the view invisible. It is right of course, I can't see a thing. Why is that the case? I changed the weight to 1, so why isn't it expanding to take up the space available? Thanks!
<LinearLayout 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:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/arches_utah"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/blue_sea_mountains"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/china_jungle"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
The layout_weight on an element applies to the parent of said element. Since you parent element has a horizontal orientation, you can only use layout_weight paired with layout_width. If your root LinearLayout was a vertical then you could set the layout_height to be 0dp and use layout_weight instead.
Judging by your question, it sounds like you should be setting weights and height on the three elements within the first child LinearLayout.
The weight attribute needs to be specified on the same axis as the orientation of the parent. Since your parent LinearLayout has a horizontal orientation, the weight attribute works on the width, so you need layout_height="wrap_content" and layout_width="0dp" on the child LinearLayout.
So the problem is between your 2 LinearLayouts.
You have not closed your parent Linear Layout and layout width relate weight not height...You can also use weightSum like this...
<LinearLayout 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:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/arches_utah" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/blue_sea_mountains" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/china_jungle" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Height of child element as percentage of parent element

I have an EditText tag inside the LinearLayout with horizontal orientation, I want to make height of my EditText as percentage of parent LinearLayout's height. How can this be achieved?
Please don't suggest layout_weight attribute as it will be used to control child elements width not height.
You could write a custom layout that resizes its children to the correct percentages, which is probably the best solution.
Or you could use layout_weight and wrap the EditText in another LinearLayout, something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="one"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
</LinearLayout>
Percentages within a LinearLayout are very easy.
Give your view a weight
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="one"/>
</LinearLayout>
I've set the height of the two textViews to 0, but the layout_weight says to make one 50% of the LinearLayout and the other 25% the height of the layout.

Android button spacing layout uneven

I have an issue with my app where the button layout is not evenly spaced when used on different devices.
As shown, the first screenshot shows the layout evenally spaced out on an older, smaller screened android smartphone.
The second screenshot from a Nexus 4, shows the large white space under the bottom button that is uneven.
I thought I had set my layout to be even but it seems it isnt.
UPDATE:
How to set a scroll view with the two linear layouts in it.
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgLink"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/appointmentmanimenuicon" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center"
android:text="Appointments"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<Button
android:id="#+id/btnaddAppoint"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="50dp"
android:drawableLeft="#drawable/appointmentmenu"
android:src="#drawable/appointmentmenu"
android:text="Add Appointment"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<Button
android:id="#+id/btnviewAppoint"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="50dp"
android:drawableLeft="#drawable/appointviewicon"
android:src="#drawable/appointviewicon"
android:text="View Appointments"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Your nested linear layout with the buttons has a fixed height of 400dp. On the small device this leaves a smaller amount of space at the bottom then it does on the larger nexus4.
If you change your linear layout with the buttons to use a height value of fill_parent it should place the buttons evenly in the space remaining in the parent layout underneath the header ( contacts).
Your second LinearLayout has a fixed height of 400dp. The bottom edge of that layout will always be at 484dp (taking into account the first LinearLayout), and will leave extra space after it on any screen larger than 484dp tall.
I'm not entirely clear on what your desired layout is, but believe what you want is to center the second LinearLayout within the parent LinearLayout.
To do so, add android:layout_gravity="center_vertical" like so:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical"
android:layout_gravity="center_vertical" >
You should also consider removing the ScrollView, as that content should never need to scroll.

HorizontallScrollView with auto-fitting pages

I am trying to create a HorizontalScrollView which will be exactly 3 times the screen width. I have tried the following code, but the scrollview will not scroll. How can I set it so that the 3 LinearLayouts inside the main LinearLayout are each the width of the screen?
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- the container for all the pages -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF0000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF0000"/>
</LinearLayout>
Note that if I give the LinearLayouts a specific width, the scrollview does scroll, but I need each LinearLayout to have the width of the screen.
You will need to use wrap_content on your top LinearLayout, and set the width of the child LinearLayouts programmatically.

Categories

Resources