android LinearLayout larger than screen height - android

I have a LinearLayout used by my Main Activity that looks as follows:
<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="wrap_content"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
I am dynamically adding fragments inside activity_main_container but the problem is, when my fragment height is larger than the screen height, the content at the bottom gets hidden. I am not able to scroll or do anything.
I have tried wrapping the fragment's layout inside ScrollView but it won't work for me because I have listview inside my fragments. I've also tried setting layout height to say 3000dp on every container but it didn't work either.
My fragment's sample layout that I am dynamically adding looks as follows. I am adding contents dynamically inside my fragment as well.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_listing_detail_linearLayout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="vertical"
android:paddingLeft="#dimen/fragment_padding_side"
android:paddingRight="#dimen/fragment_padding_side" >
<TextView
android:id="#+id/fragment_listing_detail_textview_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Review Title"
android:textSize="20sp"
android:textStyle="bold" />
<RatingBar
android:id="#+id/fragment_listing_detail_ratingbar_rating"
style="#style/customRatingBarSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"
android:isIndicator="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Photos" />
<View style="#style/divider" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp" >
<LinearLayout
android:id="#+id/fragment_listing_detail_linearlayout_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/fragment_listing_detail_textview_review"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reviews" />
<View style="#style/divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:paddingTop ="7dp"
android:paddingBottom="7dp"
android:layout_height="wrap_content"
android:contentDescription="review divisor"
android:src="#android:drawable/divider_horizontal_bright" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_listing_detail_reviews_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<Button
android:id="#+id/fragment_listing_detail_button_add_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Review" />
<ImageView
android:id="#+id/fragment_listing_detail_imageview_testimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
How do I make it so that when my fragment goes beyond screen height, I can still scroll to see the bottom views? Thanks.

I believe you would want the ScrollView to be the root View. Try this out for size:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewPort="true" >
<!-- ScrollViews can only have 1 child View, so the 1st LinearLayout below
will be the "container" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This is where you want to put everything scrollable -->
</LinearLayout>
</ScrollView>
I hope this helps, Happy Coding!

Related

ScrollView with LinearLayout child does not wrap_content correctly

So, I have the following .xml:
<?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="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.scopesystems.cityalert.ui.Despre"
tools:showIn="#layout/app_bar_despre"
android:background="#drawable/toolbar_incercare">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linear_parent">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1" />
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2.8" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--General-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/general"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/despre_general"/>
<!--Evaluti-ne-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/evaluatine"
/>
<!--Confidentialitate si licente-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/sectionHeader"
android:text="#string/confidentialitate"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/despre_politici"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1" />
</LinearLayout>
</ScrollView>
The issue is that the last ListView is on only 1 row, and it has to be scrolled to display all the items in the list. I tried to add to the linear_parent a height of match_parent but it wont let me, it says that I should use instead wrapcontent. How can i change the layout to display the items on all the screen, not just half?
In this case, On the ScrollView use android:fillViewport="true" and for child of ScrollView android:height="wrap_content".

Scrollview not showing last item in layout

Here is the code that i'm facing the problem i took 2 linear layout inside scroll view now the last button inside 2nd linear layout is hidden or i can say not showing properly.
Here is the screen shot.
you can check that i selected area is button but not working properly.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
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:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="#+id/ConcernedPortionofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/RestofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:orientation="vertical"
android:padding="20dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_button_shape_with_bg_primary"
android:minHeight="#dimen/minimum_height_all_object"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:text="SELECT"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
when i try to remove layout_weight my layout getting mass.
<android.support.constraint.ConstraintLayout 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:background="#android:color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="#+id/ConcernedPortionofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="vertical">
<!-- Parent FrameLayout 'FL' -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Conitans Tree Childs -->
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/RestofScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<!--Conitans Two Childs -->
</LinearLayout>
<EditText
style="#style/CustomEdittextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:hint="Enter Child Name"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object" />
<EditText
style="#style/CustomEdittextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:hint="Select City"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object" />
<TextView
style="#style/CustomEdittextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:hint="Select Birthdate"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<!--Conitans Two Childs -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<!--Conitans Two Childs -->
</LinearLayout>
<EditText
style="#style/CustomEdittextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:hint="Enter Mobile Number"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<!--Conitans Two Childs -->
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:fontFamily="#font/segoe_ui_bold"
android:paddingLeft="10dp"
android:text="Note / Message :"
android:textColor="#color/colorPrimaryDark"
android:textSize="18dp" />
<EditText
style="#style/CustomEdittextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:inputType="textMultiLine"
android:lines="8"
android:maxLines="8"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object"
android:minLines="8"
android:scrollbars="vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/add_child_rquest_margin_top"
android:background="#drawable/custom_button_shape_with_bg_primary"
android:fontFamily="#font/segoe_ui_bold"
android:minHeight="#dimen/add_child_rquest_minimum_height_all_object"
android:text="SUBMIT"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
This Happends Because of layout_height="match_parent" for ScrollView child LinearLayout
Change layout_height to wrap_content
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
//childs ..........
</LinearLayout>
<ScrollView>
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
Official Docs Says
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
Use nested Scroll View, scroll view will truncate the last item in the layout
add padding from bottom and check.
android:layout_marginBottom="5dp"
android:layout_paddingBottom="10dp"
The simplest solution is placing ConstraintLayout inside ScrollView like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical">
<!-- your other views could be here-->
<!-- your other views could be here-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

android TextView: How to avoid line breaks?

I'm trying to figure out how to avoid line breaks within a TextView.
If the lines are too long for the width of TextView, a horizontal scroll bar should appear. Everything I tryed with android:scrollbars (and so on) failed, there are always line breaks. This is my layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="de.zpit2u.maingate.MainGate_Logs">
<TextView
android:id="#+id/tvTitle_Logs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="#string/app_logs" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/historyRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tvTitle_Logs" >
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false" >
<RelativeLayout 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"
tools:context="de.zpit2u.maingate.MainGate_Logs">
<TextView
android:id="#+id/tvTitle_Logs_Clients"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/logs_clients" />
<TextView
android:id="#+id/tvText_Logs_Clients"
android:layout_below="#+id/tvTitle_Logs_Clients"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="10dp"
android:textSize="8sp"
android:fontFamily="monospace" />
<ProgressBar
android:id="#+id/progressBarLogs"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true" />
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Second TextView (tvText_Logs_Clients) within the RelativeLayout should be horizontally scrollable.
the way to make textview scroll horizontally.
<HorizontalScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="40dp"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:text="Horizontal scroll view will work now"/>
</HorizontalScrollView>

Scroll layout wouldn't show one of my views?

I want to have the following layout :
But when I run the code the first linear layout which contains a relative layout and an image is shown correctly.
Then there is the "view pager" but it is not on the screen.
and then there is a text box and a button which are there too.
So why my view pager is missing?
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
tools:ignore="NestedWeights,ContentDescription" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5" >
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#drawable/sp_page_name"
tools:ignore="ContentDescription" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="#dimen/product_views_horizontal_padding"
tools:ignore="NestedWeights" >
</RelativeLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager_sharepoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/TODO"/>
<TextView
android:id="#+id/sharepoint_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sharepoint_description"
tools:ignore="SelectableText" />
<ImageButton
android:id="#+id/sp_more_info"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#drawable/more_info_icon"
android:layout_gravity="left"
android:onClick="sp_more"/>
</LinearLayout>
</ScrollView>
The height of your ViewPager is android:layout_height="wrap_content".
So if your ViewPager doesn't contain other views, it won't be visible.
Well, I found out that scrolling views do not work very good together.
For my layout I solve the problem by changing the height and width of the viewpager to static sizes. Actually I defined some different sizes in my dimen.xml files for different screen sizes and use them for the height and width of my view pager.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
tools:ignore="NestedWeights,ContentDescription" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5" >
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#drawable/sp_page_name"
tools:ignore="ContentDescription" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="#dimen/product_views_horizontal_padding"
tools:ignore="NestedWeights" >
</RelativeLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager_sharepoint"
android:layout_width="#dimen/viewpager_width"
android:layout_height="#dimen/viewpager_height"
android:contentDescription="#string/TODO"/>
<TextView
android:id="#+id/sharepoint_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sharepoint_description"
tools:ignore="SelectableText" />
<ImageButton
android:id="#+id/sp_more_info"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#drawable/more_info_icon"
android:layout_gravity="left"
android:onClick="sp_more"/>
</LinearLayout>
</ScrollView>

How to position a ListView in Android?

I want to put a ListView at the top of my activity, and something else on the bottom without the ListView "swallowing" the widgets on the bottom? I tried this (with the "something else" being a RelativeLayout with a single button in it), but it doesn't work.
<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="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
I looked for some hints here, which is very useful, but doesn't say anything about ListViews. I can place widgets above a ListView without problem, but not below.
Use a RelativeLayout as the root layout then position the ListView with layout_above and the Button with layout_alignParentBottom:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
</RelativeLayout>
I'm sure this is already answered several times on SO...
Change your layout 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="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="bottom" >
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>

Categories

Resources