scroll ListView inside a LinearLayout which is wrapped in ScrollView - android

I have an activity which should be scrollable, therefore i surrounded it with a ScrollView. I have two listViews in there, which i do not want to be as large as it would be necessary to display all items.
It would be nice if i could set a maxHeight property, so that in case there are only 2 items no empty space would be present, but if there are 50 items i would only want to see like 5 of them at a time. Unfortunately there is no such property, so i decided to just set the height to a fixed number. Any advice how to do that more properly would be much appreciated.
However the main problem is that, when i try to scroll one of the listViews the whole LinearLayout scrolls down. (I can avoid this if i use another finger to 'hold' the LinearLayout in place while scrolling the list, but that certainly not a solution.)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.ballmaschine.pages.SettingsPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Some other stuff here -->
<TextView
android:text="Eine Maschine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:layout_marginTop="8dp" />
<ListView
android:layout_width="match_parent"
android:id="#+id/lvCalibsM1"
android:layout_height="200dp" />
<TextView
android:text="Zwei Maschinen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"
android:layout_marginTop="8dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/lvCalibsM2"/>
<!-- Some more stuff here -->
</LinearLayout>

Add this to the ListView
android:scrollbars = "none"
and this in Java Code
listView.setNestedScrollingEnabled(false)

In these cases always try NestedScrollView and also add this line to it
app:layout_behavior="#string/appbar_scrolling_view_behavior"

Related

Listview and TextView alignment

I have a listView which takes data from a rest API server and displays it. I need to add a textView on the side of it describing the type of data.
The right half of the screen has the listView and the left half has the textView.
The problem arises when the textView moves to accommodate the different screen sizes. I need the textView to be fixed and to be shown in conjunction with listView, and be in a straight line with the listView, regardless of the screen size. Cheers.
Edit: Image added of the sketch
It sounds like a ListView is not what you want to use. You cannot align Views with the children of a ListView. You would need to either put the TextViews within the ListView's children or not use a ListView.
The simplest way to do this would be a vertical LinearLayout containing many horizontal LinearLayouts.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
...
</LinearLayout>
The ListView is really for scrolling and when you don't know how many items there will be.
Perhaps you already have an adapter in which you are handling the creation of the items and that's why you are using a ListView. You can still create the LinearLayout's children programmatically by inflating each item individually and adding it to the container view.
Original Answer:
You probably want the TextViews to scroll with the ListView as well. So I would think you should use one ListView, then add the TextView to the left side of the ListView item.
You should use weightSum,
Here is an example.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World"/>
</LinearLayout>

Setting maximum height on RecyclerView

I have a dialog fragment that contains linear layout that involves a titleText above a RecyclerView, and at the very bottom, there's a button below the recyclerView.
Since a recyclerView expands or collapses based on the number of items the adapter sets, the button sometimes gets truncated and no longer appears to be on screen, since the recyclerView just covers the entire screen.
My question is, is there a way to set the maximum height of the recyclerView without ever hiding the button underneath. I also don't want to just give the view a random height just in case the recyclerView contains no items, and it would just be a blank section.
Please let me know if you've ever run into this issue, and how you resolved this. Thanks!
UPDATED
You can achieve this easily using layout weights. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title"
android:textSize="21sp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Submit"/>
</FrameLayout>
The Title and RecyclerView will wrap content according to contents and button will always take up bottom place.
I suggest using RelativeLayout as it handles the positioning of views for cases like yours, so that you can actually focus on main design.
<?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">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Some title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title"
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_centerHorizontal="true"
android:gravity="center"/>
</RelativeLayout>
Above XML code is the skeleton code for what you need. you can add margins and dimensions to control the spacing. But in any case (until you provide negative margins) your views will never overlap each other.
Main trick of using RelativeLayout is the ability to use XML tags like
android:layout_below or android:layout_above or android:layout_start
or android:layout_end which perfectly aligns your view the way you
want.

Scroll ListView instead of his Items

I have a layout which is divided in other two layouts: a LinearLayout for the "Header" and a RelativeLayout for the "Content".
In the content layout I have a ListView that needs to grown when the user scrolls, and hide this same ListView behind the header layout.
Basically, I need something like this:
What would be the best aproach to do something like this? This is my layout.xml right now:
<LinearLayout
android:id="#+id/layout_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_base"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:weightSum="1"
tools:context=".app.Main">
<!-- Header -->
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".55"
android:background="#drawable/background_header_small"
android:gravity="center_horizontal">
</LinearLayout>
<!-- Content -->
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight=".45">
<ListView
android:id="#+id/listview_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/text_add"
android:layout_alignEnd="#+id/text_add"
android:layout_alignStart="#+id/text_add"
android:divider="#null"
android:dividerHeight="0dp"
android:showDividers="none"
android:layout_marginBottom="10dp"
android:footerDividersEnabled="false"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
</LinearLayout>
Can I somehow remove the elevator component of the ListView, and scroll the ListView instead of the Items inside the ListView?
Apologies, I can't comment yet. But you probably want to use the CoordinatorLayout as your parent, instead of the LinearLayout you're using now. From there you have two options: the hackier way is to use a transparent CollapsingToolbar that is placed underneath the header, the other way is to create your own Behavior that will increase the high/top of the List.
This is under the assumption that the ListView will continue to scroll as normal when the the top of it reaches the top of the screen?
I can throw something together when I get home if this seems like it's on the right track.

Android: ListView pushes out everything else below it

In my app, there is a tabbed activity with three fragments. The first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. The XML layout of this third fragment is as follows:
<?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:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
As you can see, there is a TextView and a ListView and below that is another LinearLayout. Here is how it should look (the purple bar is the TextView):
And here is how it actually looks:
The TextView shows up above the ListView, but the LinearLayout does not. It is there, though. If I do android:layout_marginBottom="20dp" on the outermost LinearLayout, it does show up, but the ActionBar scrolls up and overlaps with the notification bar so that the title on the ActionBar and the notifications on the notification bar are both visible simultaneously and neither is legible.
I searched a lot and this seemed a common issue, but none of the solutions helped me. Believe me, I tried everything I could find. I tried wrapping the whole thing in a FrameLayout, using a RelativeLayout in place of the outermost LinearLayout, using two LinearLayouts--one to wrap the TextView and the ListView and the other to wrap the EditText and the ImageButton, etc., but nothing was able to show the bottom LinearLayout below the ListView. I even tried to set focus on the EditText when the fragment launches so that the keyboard would show and I can type, but even that doesn't help.
Note: On the other hand, if I use the exact same layout on an activity, the bottom LinearLayout displays exactly as it should.
I am unable to find the bug. Please help!
It looks like your toolbar pushes fragment layout down without decreasing its height. I have no idea why this happens (there are no root layout code here).
As a workaround you can set fragments layout bottom margin to ?attr/actionBarSize
As you have given layout_weight 1 in listview layout, so it occupies the whole space available. In order to get rid of you have to give some static height or use layout_weight in right manner
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:transcriptMode="normal" />
or try like this
<?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:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white">
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
All,
Thank you for all your responses. I found a workaround by trial and error now and thought I should post the answer for others who face the same issue. I set android:layout_marginBottom="50dp" on the inner LinearLayout (the one wrapping the comments bar--EditText and ImageButton). Somehow, this sets the layout correctly and the fragment functions properly in both Lollipop and Jellybean OS's. I haven't tested on other OS versions.
First of all you should read this, what is layout_weight and how it works.
You assigned layout_weight to ListView as 1, it means it covers whole height. just change it to 0.7 or any proportion you want (less than 1) will solve the problem.
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:transcriptMode="normal" />

Android, Horizontal and Vertical Scroll for GridLayout

I'm having trouble getting a GridLayout to scroll horizontally.
I found a similar question Gridlayout + ScrollView. I tried that method, but it didn't work.
It cuts out many tables (because it was supposed to go display all tables from 1 to 20).
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="16dp" >
<android.support.v7.widget.GridLayout
android:id="#+id/table_mapGrid"
android:layout_width="250dp"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<include layout="#layout/cell_list_loading" />
<TextView
android:id="#+id/table_errorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="#string/message_error_connection"
android:visibility="invisible" />
</FrameLayout>
I want to have dynamic content displayed, varying the number of columns and rows possibly with empty spaces between tables.
This I have accomplished, but the problem is when the width of the GridLayout becomes greater than its container's, I wanted to solve that using horizontal scroll, but it doesn't seem to work...
Any suggestion?
Well I found the solution
It seems like the android ScrollView works as a VerticalScrollView and only that (the name is not so intuitive as HorizontalScrollView).
So to make something scrollable vertically and horizontally, you need to nest a (Vertical)ScrollView inside a HorizontalScrollView, or the other way around, like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<!-- Your content here -->
</HorizontalScrollView>
</ScrollView>
The nested HorizontalScrollView / ScrollView will not allow you to scroll both directions at the same time.
I had this problem and created a custom component for that, here is the link if it can help anybody :
https://gist.github.com/androidseb/9902093

Categories

Resources