Minus paddingTop on Android ListView, is it possible? - android

I have top margin of 30dp inside some of my list view items (including always the first one). However I don't want this top margin on my first list view item.
I have tried offsetting this by adding the following to the ListView
android:paddingTop="-30dp"
However, it doesn't seem to have any effect and the first list view item is still 30dp below the top edge of the list view.
Is there a way of getting minus top padding to work on an Android ListView?
XML
<ListView
android:choiceMode="singleChoice"
android:divider="#null"
android:dividerHeight="0dp"
android:paddingTop="-30dp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
Then some of the list view items content is as follows:
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"/
android:layout_marginTop="30dp">
<!--other content here-->
</LinearLayout>
The first list view item always uses the above template and im trying to offset that 30dp top margin.

Try setting android:layout_marginBottom="30dp" in your linear layout, so that you'll have the top of list without any margin but between list items still with margin. Does it look like what you want? Because minus padding is normally possible but in your case its overridden by the margin in list items.

Related

Move layout up when layout above shrinks in height

Hi I have a fragment with two RecyclerViews in it, one above the other.
The first is a list of items for the user to take an action on and the second is where the items will be populated once the action is taken. So when an item is removed from the top list it is added to the bottom list.
The issue I am having is when I remove an item from the top RecyclerView all the remaining items in the top RecyclerView move up to fill in the space left by the removed item, but this leaves a gap between the top and bottom RecyclerViews.
How can I move the bottom recyclerview up to fill in the gap created once an item is removed from the top RecyclerView
Here is my layout xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_alignParentTop="true"
android:id="#+id/pending_tasks"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:layout_below="#id/pending_tasks"
android:id="#+id/completed_tasks"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
I have tried calling invalidate() on the bottom RecyclerView but that has not worked. Any help would be appreciated, thanks!
It's impossible to update layout size once it's been displayed on the screen. If you modify the content of your RecyclerView you can't just simply refresh the layout to shrink it.
I would suggest using single RecyclerView and storing lower bound of the first list in some variable. Then simply update it accordingly to modifications in your first list.
Another solution which may help you:
Change Relative layout width and height dynamically

RecyclerView with GridLayoutManager - layouting items

I have a simple layout with a recycler. I feed it with Adapter and set GridLayoutManager for the recycler.
<android.support.v7.widget.RecyclerView
android:id="#+id/scripts_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:verticalSpacing="20dp"
android:clipToPadding="false"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
tools:listitem="#layout/tmpl_script"
android:paddingBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingTop="30dp"
app:layout_gravity="center" />
Activity
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.scripts_recycler_view);
recyclerView.setAdapter(adapter);
int columns = getResources().getInteger(R.integer.scripts_columns);
recyclerView.setLayoutManager(new GridLayoutManager(this, columns));
recyclerView.setHasFixedSize(true);
Item
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/game_border"
android:gravity="center"
android:orientation="vertical">
I have two problems:
there is no space between rows
items are not centered within the row
I tried all properties in Android Studio but they do not have any effect. I googled, opened many questions but I still have no luck. What magic property shall I use?
As you can see items are positioned on left side of the screen. I want to move it to center. And there is no space between first and second row.
The easiest solution for (1) is to add top and/or bottom margin, like #suraj said.
Regarding (2) - the critical issue is that your items are square. To get it working, set android:layout_width="match_parent" and change the design of your items to use the actual smaller dimension (width or height) for both dimensions (maybe all you need to do in your case is to make the background square). Then display the content centered within the item's LinearLayout (you are already doing this). You have to do it this way because GridLayoutManager does not support layout_gravity at this time.
For 1.there is no space between rows
Try android:layout_marginBottom="10dp" in the linear layout of item.This should provide the spacing below each item.
For 2.items are not centered within the row
Looks like you have given right margin to your recyclerview and no left margin.
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
should give equal spacing on both sides.
or set
app:layout_gravity="center"
in linear layout of the item.

ListView in android adds padding to headers same size as dividerHeight

I have a simple ListView in Android containing items, a headerView and a footerView. The headerView and footerView are added programatically to the ListView.
The xml for the ListView looks as follows:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="8dp"
android:divider="#color/gray"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
This gives a gray divider of 8dp between each listitem which is exactly what I want. The problem is that it also adds a padding between the headerView and the first listitem with the same color as the theme background (white in my case), the padding is also the same height as the dividerHeight (8dp).
I though that the property headerDividersEnabled="true" would take care of this, from the reference:
android:headerDividersEnabled
When set to false, the ListView will not draw the divider after each
header view. The default value is true.
If I set headerDividersEnabled="true", a grey 8dp divider between the listitem and headerView is drawn, as expected.
From what i got from your question is you are trying to hide the grey lines between items ,if so try this.
To remove the separator between items in the same ListView, here is the solution:
getListView().setDivider(null);
getListView().setDividerHeight(0);
developer.android.com # ListView
Or, if you want to do it in XML:
android:divider="#null"
android:dividerHeight="0dp"
Add this in ListView
android:clipToPadding="false"
ListView will recycle its children as soon as they enter the padding area. The attribute clipToPadding only affects drawing

Position textview below list & reposition it as size of list increases

In my layout, I have a textview which I need to be fixed at the top of the screen, a button to be fixed at the bottom of the screen.
Between these two, I have a list with a textview(with a drawable) below it. (please see image). I want the textview positioned below the list;i.e.; as list items grow, the textview should reposition itself. As I click on the textview, I add items to the list. I am able to achieve this, but as my list items increase, the textview goes below the list & is not visible. So what I see on screen is the top textview with the list below it & the bottom button. The textview to add items in list view is not visible, so I cannot add items to my list. I need that even if the list items increase, I want the list to be visible along with the textview.i.e.; as list items grow, the textview should reposition itself. I tried various approaches suggested with several combinations of linear & relative layouts & positioning techniques, but I am not able to achieve this.
Try 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc xyz"
android:gravity="center"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button"></ListView>
<TextView
android:layout_below="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Save"
android:id="#+id/button" />
</RelativeLayout>
You have to nest the layouts here. You could for example use a LinearLayout with vertical orientation as parent and place your textview and listview (or whatver you are using for your item list) inside the LinearLayout. Here's the idea:
-TopView: set to wrap content
-ListView: set to fill parent
-TextView: Set to wrap content
If I'm not mistaken this should set the top and bottom view to fixed sizes and make the ListView fill the remaining space.
You can try adding that button with image as footer to the listview it will work with less noumber of items when it exceed the screen hight remove footer and make the bottom layout visible(Have to add the same footer item in bottom layout also).

How can I make the footer of a ListView stretch to fill the remaining space?

I have a ListView which can contain any number of items. When it uses less than the whole height of the screen I want to show a footer which fills the remaining space.
The footer is just a tiled image. I can't just set it as the background of the ListView or its container, because the list items are partially transparent and it must be the main background and not the footer visible through them.
Adding the footer view works fine if the footer is of a fixed height, but if the fixed height is more than the remaining height then the ListView scrolls unnecessarily.
Making the footer view fill_parent seems to have no effect, it's just invisible.
Putting the footer view into the parent layout also has no effect, because the ListView seems to fill the whole height unless it's given a fixed layout_height value.
Is this possible?
Try setting the layout weight to one, or check out trying a merge view:
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
Put the ListView inside of another layout set the expand to fill the screen.
A LinearLayout works best for this.
Something like this:
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent" >
<ListView android:layout_height="match_parent"
android:layout_width="match_parent" >
...
</ListView>
</LinearLayout>
This should fill the LinearLayout to the bottom of the screen. You can use this behavior to control how your View looks.
Hope this helps!
Try this and tell me if it works:
Put the two in one parent layout (for example: Linear Layout)
For ListView: android:layout_hight="wrap_content"
For footer: android:layout_hight="fill_parent"

Categories

Resources