Android ListView cutting off items wider than the first item - android

I have a Listview inside of a HorizontalScrollView. The listview holds file paths, and the horizontal scroll view is intended to eliminate the need to wrap the longer paths.
Everything is working exactly as desired with the exception that any items longer than the first item in the listview are being cut of at the length of the first item (whatever the width of the first item, none of the other items will scroll past that width).
Here is the section of the layout that is giving me trouble:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layoutListView"
android:orientation="vertical"
android:layout_below="#+id/spFileTypes">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/lvItems"/>
</HorizontalScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:id="#+id/tvStatus"/>
</LinearLayout>
Here is a screenshot of the issue:
The image is scrolled all the way to the right. The third item is cutting off ".php". I have tried this with long and short file names, lists of many (upper double digits) and lists with just a few, in each case, it seems the width limit is dependent of the width of the first item.
Whenever the lists changes, it always limits its width to whatever the first element is.
The desired behavior is to have it match the width of the widest item.
I've googled my heart out, dug through the official documentation, and I'm stumped. Any help is greatly appreciated :)

Instead of wrapping the ListView in HorizontalScrollView, try this:
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"/>

Related

Android: ListView column sizing to first item

I have a ListView inside of a HorizontalScrollView which I'm using as a small panel overlaying a fragment. It all would work as expected if the longest item were the top item. When the shortest is, the ListView's width takes on the width of that item and then there's no horizontal scrolling. The rest of the items are just cropped.
Here is the pertinent portions of the layout:
<HorizontalScrollView
android:id="#+id/vendorListContainer"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#50FFFFFF"
android:visibility="gone" >
<ListView
android:id="#+id/vendorList"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</HorizontalScrollView>
I'm using the android.R.layout.simple_list_item_activated_1 as the item layout. Should I be using a custom layout with some special options chosen to fix this? I thought of just padding all the strings, but that's a hack I'd rather avoid. What am I doing wrong?

Android View to fill center area of Layout

I have this layout:
All views fill the entire space horizontally, and they're inside a LinearLayout oriented vertically. The blue parts have a fixed height (they have the wrap_content property).
The red part is a ListView. How can I make it fill that center space if there are not enough elements in the list and at the same time preventing it to push the last two elements down if it has more elements?
So far it doesn't push down the two views under it (with the layout_weight="1" property), but if it doesn't have enough elements, it shrinks and makes those two elements go up, leaving an ugly white space under them.
This is what happens:
This is what I expect:
Notice that even though the ListView is smaller, the two last views don't go up.
So far I've tried:
Giving all views a weight (ugly display but sort of works).
Giving each view a size (different results on different devices).
Giving the last view the android:gravity="bottom" property, but the view still goes up.
What may work
I've been messing around and I think a RelativeLayout may work, with a property like layout_alignBottom that instead of aligning to the end of the given view, it aligned to the start of it.
SOLUTION
The solution was to use a RelativeLayout and set the list's layout_above and layout_below properties to that of the elements I want to align it to.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.stackoverflow.app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="#string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="#string/hello_world" />
</LinearLayout>
Here's a couple of options that may work for you. Let me know how it goes.
Option 1:
Set the containing vertical orientated linear layout to fill_parent / match_parent (they are the same). Then set the gravity or layout gravity of the bottom 2 views to bottom.
Option 2:
Contain the list view in a linear layout with a fixed height. Set the list view to wrap_content.
EDIT
You could use relative layouts for this, this link here seems to do what you need
http://www.javacodegeeks.com/2013/10/android-fixed-header-and-footer-with-scrollable-content-layout-example.html
How about wrapping your list view inside the a layout and give the layout the fixed height.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="300dp" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>

Trying to get textView to stick to gridView scroll

I have a fragment with a textView giving instructions, and below a gridView. I wanted for the textView to scroll upwards with the gridView. In other words: for textView to disappear off screen when gridView is scrolling.
My idea of how to accomplish this is to give the gridView a fixed height that would make the entire layout scrollable - including the textView. However, up until I cannot achieve this. Is there a way to do this?
Layout no scroll:
When scrolled:
Any help/advice would be appreciated.
My layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/LightGrey"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/chooseLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:text="#string/chooseLine"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/gridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="130dp"
android:fitsSystemWindows="false"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingTop="0dp"
android:stretchMode="columnWidth"
android:verticalSpacing="25dp" />
</LinearLayout>
Achieving this is is not going to be trivial. What you need is ListView's capability to add a header view, but unfortunately, GridView doesn't offer that functionality.
What I have done in the past to solve this problem is to convert the GridView to a ListView, and then add the header to the ListView. I created a wrapper adapter that takes the original adapter and combines a horizontal row's worth of grid cells into a single list row.
The tricky parts include: dynamically adapting the number of columns in a row based on the width of the screen, accounting for all combinations of view types within a row and remaining empty columns in the last row, and handling click interactions properly.
Put the whole LinearLayout in a ScrollView (How to use ScrollView in Android?) and for the GridView set android:layout_height="wrap_content"

Mimicking the Gmail app label listing

I'm trying to build a layout for my ListView items similar to the Gmail app's listing of labels, where it has the label text on the left and the count to the right. What I have mostly works, except with long text. What I have results in the text overlapping the count.
This is what I'm trying to mimic:
As you can see, the bottom label isn't wrapping where the top label is (read below for more details on this).
When both labels have a count, the count lineup on each row:
With what I have there should be a 3 showing for the last item but it's getting overlapped. Also, I'd like the "count" TextView to lineup on each row the same way the Gmail app is (center, right aligned, I guess?):
Layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="left"
android:layout_weight=".75"
/>
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right|center_vertical"
android:padding="10dp"
android:layout_weight=".25"
/>
</LinearLayout>
I know when you're using layout_weight you're supposed to set the layout_width to 0px, however, this results in every row being cutoff at the same spot. I only want the text wrapping if it goes longer than the count TextView.
I am not good with Android layouts, I think they are extremely frustrating to work with, so this may be something easy. Thanks.
You should be able to prevent the views from overlapping by using a RelativeLayout instead of your LinearLayout, using the layout_toRightOf or layout_toLeftOf XML attributes to position the views relative to one another.
Figured it out. I needed to add android:layout_toLeftOf="#+id/count" to the first TextViewwhich Ernir answered above

Preventing an Android ListView from taking up almost all of the screen height

I am trying to setup an activity in Android that has 50% of the screen hight for a ScrollView and the other 50% for a ListView.
To do this I have set the weight of each to "1".
Like This:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip">
<ScrollView android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<TableLayout android:id="#+id/my_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow android:padding="5dip"
android:id="#+id/fault_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/table_title"
android:paddingRight="20dip"/>
</TableRow>
<!-- More rows are added at run time. -->
</TableLayout>
</ScrollView>
<ListView android:id="#+id/comments"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"></ListView>
</LinearLayout>
When both the ScrollView and the ListView have too much content to display without scrolling this works perfectly.
When the ScrollView only needs 20% of the screen and the ListView needs 70% the ListView grows to 70% so it can display without scrolling. This is not what I expected to happen but is good.
The problem I have is when the ListView has so much data that it would need the whole screen to display AND the Scroll view only 1 or 2 rows of data in it. In this case the ListView grows to take up almost the full height and leaves space equivalent to about one line of text for the scroll view.
I can't seem to get my head around how to reserve a certain proportion of the screen for the ScrollView no matter how big the ListView gets.
The amount of space the ScrollView can grab seems directly proportional to the number of rows in it's child table. When this is only 1 the scroll view is only about half a cm in height.
What about changing android:layout_height for to be a minimum value instead of 0dip?
UPDATED: As per the comments below the solution was to set the outer LinearLayout to have height "fill_parent"
Use these parameters in the ListView section
android:layout_height="0dp"
android:layout_weight="1"

Categories

Resources