ListView in Fragment not covering full Parent - java

I have a ListView in a Fragment which isn't covering the whole Parent when the List isn't fully populated.
The Problem is the scrolling behaviour of the ListView, I can only browse in the small piece of window the ListView is initially loaded in.
(4 Entries or more do occupy the entire Parent, with less entries the problem happens)
Fragment:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
/> </LinearLayout>
Main XML:
<android.support.design.widget.CoordinatorLayout
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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemBackground="#color/bgBottomNavigation"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:menu="#menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
The ListView gets populated via Volley + JSON with an Adapter for the ListView. Comment if you need the Code.
When I'm trying to put the ListViews height on "match_parent" the whole screen is occupied by the ListView, but it isn't scrollable anymore.
What can I do?
EDIT:
Coordinator Layout - This is the Code of the Frame Coordinator with the Bottom Navigation in it:
<android.support.design.widget.CoordinatorLayout 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemBackground="#color/bgBottomNavigation"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:menu="#menu/navigation" />
</android.support.design.widget.CoordinatorLayout>

You could set a minimum height attribute to your listView and adjust to what suits you.
Eg. to set it to 20dp, add android:minHeight="20dp" to your listView attributes.
Best solution would be to wrap all the contents of the CoordinatorLayout inside a layout you can control yourself, like a ConstraintLayout. In that case you would align the FrameLayout (and the fragments it handles) between your other components and everything would be scrollable and clickable. A mock of this solution would be:
<CoordinatorLayout>
<ConstraintLayout height="match_parent">
<BottomNavigation bottomToBottomOf="parent" />
<FrameLayout height="0dp"
topToTopOf="parent"
bottomToTopOf="bottomNavigation" />
</ConstraintLayout>
</CoordinatorLayout>

Related

Issue while setting Footer layout in ListView

Below is my Footer Layout for setting it for my ListView :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<TextView
android:text="footer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
It's working fine. But, What if I want to set an emptyview with some background color in my ListView's footer ?
If I am doing it as below, ListView not showing footer view. :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
</RelativeLayout>
I am setting footer as below :
View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_for_gym_list, null);
listGymSuggestions.addFooterView(footerView);
Please guide me How can I set empty footer with some height and background color in ListView ?
The View is there but it has NO height so you must add your own height value may be 24dp or something and set it as a footer you can even add a width if you want. For instance here I am adding a height of 24dp:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#color/gray_cell_bg">
</RelativeLayout>
If there is no child in that layout, It will not be shown. If you have problem with setting specific height(when you are adding child dynamically), you can use padding like,
android:padding="20dp"
I added a button as a footer in one of my layout.
Here is my code. You may add anything you want.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/search_toolbar"
layout="#layout/search_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/emp_button"
android:layout_below="#+id/search_toolbar" />
<TextView
android:id="#+id/tvsample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_toolbar"
android:layout_marginTop="200dp"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/emp_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/Red"
android:src="#drawable/ic_add_employee"
android:text="#string/emp_button"
android:textSize="14sp" />
Issue is solved by adding an ImageView in Footer view as below :
footer.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/com_facebook_profilepictureview_preset_size_normal"
android:scaleType="fitXY"
android:src="#color/gray_cell_bg" />
</RelativeLayout>

unable to scroll the screen which has grid view and listview below it

I have the following xml which has Gridview and framelayout below it.
My requirement is if I scroll the screen upwards whole screen should scroll, but for me Only listview scrolls
Below is the xml file:
<?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"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time_card_header"
android:id="#+id/scrollView">
<GridView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</ScrollView>
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
Actually GridView and Listview have already scroll. No need to put the above widgets in scrollview. Firstly, remove the scrollview. I think you need the FrameLayout to be fixed at the bottom and show gridview above. For that first use FrameLayout in the linear layout with align_parentbottom true. after that please place the gridview above FrameLayout. That will fix the problem.
Just put linearlayout below scroll view and then gridview.
<scrollview>
<linearlayout >
<gridview />
</linearlayout >
</scrollview>
Sorry i am giving the ans from the mobile but the concept is same.
You can use RecyclerView with GridLayoutManager and set nestedScrollingEnabled of recyclerView to false.
and instead of ScrollView use NestedScrollView as parent of RecyclerView:
private void setUpRecyclerView(View view) {
recyclerView = view.findViewById(id.repliesList);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),numberOfColumns));
}
and xml file:
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<RecyclerView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
</android.support.v4.widget.NestedScrollView>

Can't scroll completely down in ViewPager with NestedScrollView in CollapsedToolBarLayout

I'm creating a fragment with a collapsing Toolbar Layout using a ViewPager. This is the XML for the Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="#+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.AppActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/htab_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:backgroundTint="#color/completeWhite">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="420dp"
android:fitsSystemWindows="true"
app:contentScrim="#color/completeWhite"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">
<RelativeLayout
android:id="#+id/brand_page"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax"
android:paddingTop="24dp"
android:layout_marginTop="?attr/actionBarSize">
//Lots of images and texts that are collapsed
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/htab_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:layout_marginBottom="75dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="#+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabIndicatorColor="#android:color/white"
app:tabSelectedTextColor="#color/lightGreen"
app:tabTextColor="#color/grey"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<io.brandie.brandie.custom.NoSwipeViewPager
android:id="#+id/htab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginBottom="#dimen/spacing_giant"
android:background="#color/white"/>
</android.support.design.widget.CoordinatorLayout>
The viewPager is filled with fragments that use a gridview to display different cards, this is the xml for the Fragment of the first tab:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="#+id/NestedScroll"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:layout_gravity="fill_vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/all_campaigns_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.brandie.brandie.fragments.AllCampaignsFragment">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/all_campaigns_grid"
android:descendantFocusability="blocksDescendants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
With this I can scroll down anywhere on the screen to make the toolbar collapse and the view-pager expand. The problem is, when the tool-bar is completely collapsed and only the tabs are showing, I can't scroll the viewPager anymore. Since it holds a gridView with a lot of items, it results in that I can't scroll through them all, since the scroll just "stops".
I want it to look like this
But instead, when the toolbar is collapsed, I can't scroll the viewPager down anymore. I can however scroll it up to reveal the toolbar again, that works without issues. I am using Android API 16, is that an issue?
How do I fix this very irritating problem?
EDIT:
If I remove the NestedScrollView from the fragment XML, the collapsed Toolbar only collapses if I scroll on the toolbar. If I scroll the ViewPager, I can scroll through all content correctly, but the toolbar doesn't collapse.
Answer for your EDIT: Try using setNestedScrollingEnabled(false) on your GridView

Use RelativeLayout outside CoordinatorView

I have a CoordinatorLayout, in where a RecyclerView displays some CardViews, defined in an other layout. Under this scrollable view, i am trying to add a RelativeLayout, which inherits fields for text input, buttons for sending it, etc.
The problem: CoordinatorLayout seems to block the whole screen, although i told to wrap contents height. Nevertheless, RelativeLayout is added (tested it by setting Coordinator-height = 50dp), but off-screen. What is wrong here?
PS: I know how to solve it with android:layout_weight=xx. But thats not what i want to achieve, because it blocks my EditText to expand, if bigger texts are entered.
<RelativeLayout 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.support.design.widget.CoordinatorLayout
android:id="#+id/data_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="#+id/delete_data_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/data_coordinator_layout">
<EditText/>
<Button/>
<Button/>
<Button/>
</RelativeLayout>
</RelativeLayout>
It's actually pretty simple. You need to align your Relative Layout (delete_data_layout) to bottom of your parent. Then you make the Coordinator layout above the Relative Layout. This causes, that Coordinator layout will always stay above the Relative Layout - even when the Edittext expands.
Here's the XML.
<RelativeLayout 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.support.design.widget.CoordinatorLayout
android:id="#+id/data_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/delete_data_layout"> <---- Change here
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="#+id/delete_data_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"> <---- Change here
<EditText/>
<Button/>
<Button/>
<Button/>
</RelativeLayout>
</RelativeLayout>

RecyclerView takes up all screenspace

So I'm having some trouble with a RecyclerView in a layout file
Here it is:
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:orientation="horizontal"
>
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"
/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"
/>
</LinearLayout>
The RecyclerView is visible and scrollable but the LinearLayout and the Views inside are not visible... I tried quite a few things but nothing has worked so far.
Can any of you please point me into the right direction?
Many thanks in advance!
When RecyclerView is drawn, it calculates all the remaining size on screen to itself before drawing the next elements and don't recalculate after the other elements are drawn, leaving them outside the screen.
The trick is to draw all other elements first, and leave RecyclerView for last. FrameLayout does not work anymore so use a relative layout and put the RecyclerView last on the XML layout file.
Example to add a bar with buttons below the RecyclerView:
<?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"
android:padding="16dp"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/pagination_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/previous_btn_label"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/next_btn_label"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/items_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_above="#id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR
</RelativeLayout>
Change the root view to a FrameLayout. Set the gravity of the LinearLayout to bottom
<FrameLayout 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.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"/>
</LinearLayout>
</FrameLayout>
Alternatively, you can wrap the linear layout in a FrameLayout
and set it to align_parentBottom = true if it is a relative layout or a linear layout at your root and you have other things in your view.

Categories