ScrollView with LinearLayout child does not wrap_content correctly - android

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".

Related

Some views don't show in RelativeLayout

I have the following RelativeView. I want
EditText at bottom
TextView on top - I have an issue here - it doesn't show but I don't understand why
2 ListViews between them - both taking half of the screen vertically
Please do you see why that top TextView doesn't show ?
<?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">
<EditText
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_alignParentBottom="true">
<requestFocus />
</EditText>
<View android:id="#+id/Placeholder"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_above="#id/Search"
android:layout_centerInParent="true"/>
<ListView
android:id="#+id/ResultsColumn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignRight="#id/Placeholder"
android:layout_above="#id/Search" />
<ListView
android:id="#+id/ResultsColumn2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#id/Placeholder"
android:layout_above="#id/Search"/>
<TextView
android:id="#+id/InformationTextTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/ResultsColumn1"
android:text="Why is this not visible ?"/>
</RelativeLayout>
It's simply because the ListViews have match_parent in the android:layout_height property and the TextView is defined after them in the xml. Since this is a pretty simple layout I would recommend to use the LinearLayout instead.
<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">
<TextView
android:id="#+id/InformationTextTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/ResultsColumn1"
android:text="Why is this not visible ?"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ListView
android:id="#+id/ResultsColumn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/ResultsColumn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<EditText
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text">
<requestFocus />
</EditText>
</LinearLayout>

How do you position views and other UI items in a fragment layout?

I have a message interface consisting of a listview, an edit text box and a button found in a tutorial online. In my implementation of the xml it lays out perfectly with the listview above the text box and button. However, when I try to implement the layout using two fragments (1) for an audio player at the top of the screen and (2) below that for the messaging, I cannot force the text box and button to display below the list view.
How can I do that below are the XMLs.
(1) Works as expected:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
>
<ListView
android:id ="#+id/message_list"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#null"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/new_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"
android:inputType="text">
<requestFocus />
</EditText>
<Button
android:id="#+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="#string/send_message"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
(2) Positions the text and button at the top of or near the top of the list view:
<?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">
<ListView
android:id="#+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/new_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="100"
android:inputType="text"
android:labelFor="#id/new_message" />
<Button
android:id="#+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="#string/send_message"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
Changing (2) to linear layout has not fixed my issue.
This somehow fixed my problem although it seemed not to have worked before.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.app.MessageFragment">
<ListView
android:id ="#+id/message_list"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#null"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/new_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="100"
android:inputType="text"
android:labelFor="#id/new_message" />
<Button
android:id="#+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="#string/send_message"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
Your second layout has no constraints. You should make sure that your LinearLayout is aligned bottom and the listview is above the LinearLayout. It should be something like this :
<?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">
<ListView
android:id="#+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llText"
android:divider="#null" />
<LinearLayout
android:id="#+id/llText"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<EditText
android:id="#+id/new_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="100"
android:inputType="text"
android:labelFor="#id/new_message" />
<Button
android:id="#+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="Hello"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>

Placing Fragment below Listview

I am struggling with my Android Layout.
I want to create a Linear Layout which contains a Button at the Top, next a List View and when all elements of the list view are displayed I want to show a Fragment which displays Google Maps.
This is my Layout xml file. In that case you can only see the Fragment and the Button but not the listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
tools:context="de.mypackage.MyActivity">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:id="#+id/button_back"
android:text="#string/back"
/>
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"/>
/>
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
tools:layout="#layout/fragment_maps"/>
</LinearLayout>
Any Idea how I can realize my layout?
Try this code
<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="10dp"
android:weightSum="1"
android:paddingRight="10dp">
<Button
android:id="#+id/button_back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<fragment
android:id="#+id/fragement"
android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
tools:layout="#layout/fragment_about_app" />
</LinearLayout>
this will set both in half screen you can change ration of weights in layout.
Advice :
Declare your root layout as RelativeLayout
RelativeLayout is a view group that displays child views in relative
positions. The position of each view can be specified as relative to
sibling elements .
Then use android:layout_below
Positions the top edge of this view below the given anchor view ID.
Accommodates top margin of this view and bottom margin of anchor view.
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#+id/birth"
tools:layout="#layout/list"/>
Try this code
<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="10dp"
android:paddingRight="10dp">
<Button
android:id="#+id/button_back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<fragment
android:id="#+id/fragement"
android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_about_app" />
</LinearLayout>
Try this :
<?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="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context="de.mypackage.MyActivity">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:id="#+id/button_back"
android:text="#string/back"
/>
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_below="#+id/button_back"/>
/>
<fragment
android:id="#+id/fragement"
android:name="de.mpackage.MapsFragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0"
android:layout_below="#+id/list"
tools:layout="#layout/fragment_maps"/>
</RelativeLayout>
I found a solution hope it helps.
In my ListView for the height instead of using 0dp I used wrap_content and adds layout_weight=1 for my Fragment
<LinearLayout
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"
android:background="#EFEFEF"
android:orientation="vertical">
<Button
android:id="#+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:divider="#null"/>
<fragment
android:name="de.mpackage.MapsFragment"
android:id="#+id/fragement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:layout_weight="1"
/>
</LinearLayout>

Adding a Button at the end of ListView

I want to add a button at the end of a list view. I have the following xml file. The list view appears with its contents, but the button does not show up.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myListView" />
<Button
android:text="Project Download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/download" />
</LinearLayout>
The problem is with the attribute android:layout_height="match_parent" in your ListView. match_parent means that the listview will fill all the space. The correct way is:
<?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"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight="1"
android:id="#+id/myListView" />
<Button
android:text="Project Download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/download" />
</LinearLayout>
Your button is not shown because your ListView took the whole space hiding your button as you set its layout_height to match_parent. match_parent will take the whole available space. So change it to wrap_content like this:
<?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"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_parent"
android:id="#+id/myListView" />
<Button
android:text="Project Download"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/download" />
</LinearLayout>
wrap_content will wrap your view to the least available height/width without hiding anything or making anything unclear.
i have the same problem, and solved.
you only need wrap that layout into ScrollView
<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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_transaction_history"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/btn_history_load_more"
style="?borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:paddingBottom="#dimen/margin_big"
android:paddingTop="#dimen/margin_large"
android:text="#string/text_btn_load_more"
android:textColor="#009CDE"
android:textSize="#dimen/text_size_medium" />
</LinearLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_alignParentBottom="true"
android:text="Project Download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/download" />
<ListView
android:layout_above="#id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myListView" /></RelativeLayout>

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>

Categories

Resources