I have a ScrollView with a RelativeLayout as its child. This is all nested within a RelativeLayout.
<?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:background="#2fb136">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fpScrollView"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fpRelativeLayout">
Image of how it looks
Now if I add a margin of 50dp to the ScrollView, the RelativeLayout matches the height of the scroll view and cuts off the button rather than expanding and allowing for scrolling.
Any ideas on how to fix this? I would like the RelativeLayout to actually wrap to the content and not cut it off.
I found a solution. One of the views within the ScrollView was aligned to the horizontally/vertically center which doesn't make sense in the context of a view that changes it size dynamically. This was causing the RelativeLayout within the ScrollView to simply stay the size of the ScrollView rather than wrapping its content.
If your entire layout is scrollable I would suggest making the base layout a ScrollView rather than a RelativeLayout like I did above.
Related
The title of question might look similar to other questions but It's not. I have several strange problem that I am not being able to solve it.
I have a xml layout and the structure of that layout looks like below.
LinearLayout
|
ScrollView
|
LinearLayout
|
TextView
EditText(multiLine)
The xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"/>
<EditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Now let's discuss about problem.
If I keep adding new line to the editText, the editText itself scroll and the TextView which is located above EditText is not scroll. But what I want is EditText not to scroll and leave the job for scrollView so that the textView which is loacated above EditText also scroll along with EditText when new Line is added.
ScrollView behave ok when I press back(usually when the editText is not focused anymore).
Setting to
<ScrollView
...
layout_height="wrap_content">
fix the problem but the desired height then compromised. Same thing happen with Horizontal scrollView also.
Is there any way to solve this problem ? Detailed Answer will be helpful.
Set your EditText's height to wrap_content so it expands instead of scrolling. Set your LinearLayout's height to wrap_content so it's the size of its contents, i.e. the TextView and EditText.
The ScrollView should be whatever height you want for your "window" into the contents layout, the LinearLayout that moves up and down "behind" the ScrollView.
The scrolling happens if that contents layout is too big to display in the visible ScrollView, which is why that contents layout should always be wrap_content (the size of the contents) and never match_parent (the size of the scroll view "window"). If the contents are always the same size as the scroll view, they'll never need to scroll, right?
I have a Scrollview for the complete screen, so in small phones you can scroll and see the complete form.
For big screen/hdpi phones, the screen has enough size so it fits.
The problem is that since its a LinearLayout, all views are at the top, and there is white space at the bottom.
I set the weight on one of the items items inside the linear layout, but it does not grow.
my code:
<ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
HEADER STUFF
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
THIS PART I NEED TO GROW AS MUCH AS POSSIBLE SO THE FOOTER IS AT THE BOTTOM OF THE PAGE.
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/LightBlue"
android:orientation="vertical"
android:paddingBottom="10dp" >
FOOTER STUFF. NEED THIS TO BE AT THE FOOTER IF THE PHONE IS BIG ENOUGH.
</RelativeLayout>
<LinearLayout>
</ScrollView>
Simple answer: In the LinearLayout, change android:layout_height="wrap_content" to android:layout_height="match_parent".
Reason: By wrapping the content, you aren't giving the middle RelativeLayout with the most weight a chance to grow and fill the white space. Changing it to match the parent height gives it its space to blossom, so to speak.
Also, per Indiandroid's comment, you may want to put the ScrollView inside the LinearLayout and around the middle RelativeLayout that hosts the content so that the header and footer are fixed.
EDIT: If you intend to stretch the middle RelativeLayout by using weight in the LinearLayout, you will, after applying my previous part about switching the height to match_parent, have to move to ScrollView inside the LinearLayout around the middle RelativeLayout or it will grow indefinitely. This is because the ScrollView has no vertical bounds and by matching it's height with the LinearLayout, the LinearLayout also has no bounds.
I'm trying to find documentation or guidelines as to behaviour of a UI component with the layout_width/layout_height set to match_parent within a ViewHolder of layout_width/layout_height of wrap_content.
So for instance, what is the EXPECTED result of the following layout?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
/>
</RelativeLayout>
By the description here, the layout of your example would depend on what you put inside the LinearLayout. That content would expand to fill all available vertical space within the parent RelativeLayout. By default, the LinearLayout would be positioned at the top left of the parent RelativeLayout
I am dynamically adding children in LinearLayout horizontally. I am adding child on button click. But after adding some children, say 4, others children go out of screen as these are being added horizontally. How can I check that child is going beyond screen in LinearLayout, and I need to create new layout? Or what can I do with the LinearLayout so that when it goes out of screen, it wraps itself?
Any help? Thanks in advance.
Just put your LinearLayout inside HorizontalScrollView, it will do all the necessary work for you:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
EDIT if you want your views to be wrapped inside the parent view, please take a look here Line-breaking widget layout for Android
In my application, I have a layout which includes a LinearLayout--> Top, GridView--> Middle and a set of views which should be aligned to the bottom.
1. Layout when gridview is not having much content
2. Layout when grid view contents are increased
3. Layout as how it should be shown when Gridview content is very large
Presently my gridview is expanding when the contents in it is increasing that the lower level contents are not visible in the screen.
I don't want to assign specific height to the grid view as it is looking weird in screens with different dimensions.
Is there any way that the gridview expands only upto the bottom views? My parent layout is LinearLayout. I have tried out with relative layout, but it is not working.
I too faced the same problem and found the solution by modifying the respective xmls as below:
Use RelativeLayout as Parent Layout.
Place the Header Content in RelativeLayout
Use ScrollView for the GridView which changes dynamically.
Place the Footer Layout after ScrollView
as shown below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/txtTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/scro1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/txtTextView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/scro1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
android:layout_below attribute in ScrollView makes the difference.