How to center RecyclerView items horizontally with vertical GridLayoutManager - android

I have a vertical RecyclerView using a GridLayoutManager. I want each column to be centered, but the columns begin all the way on the left. In the following picture you can see what I'm talking about. I used the ugly color scheme to illustrate the columns and background. The green is the background for each item in the RecyclerView, the red is the background of the RecyclerView itself:
http://imgur.com/a/J3HtF
I'm setting it up with:
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
Here's the column_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="#+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"/>
<TextView
android:id="#+id/movie_column_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here's the recyclerview xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/company_details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

Try letting the column item fill the width of the column while centering everything inside:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="#+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/movie_column_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>

To restate #kris larson's answer;
If you are using ConstraintLayout, adding android:layout_gravity="center" or android:layout_gravity="center_horizontal" to the parent layout in item's XML would be enough (the first parent in hierarchy).
In your case, the codes would be something similar to this:
<android.support.constraint.ConstraintLayout
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/movie_column_photo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="80dp"
android:layout_height="120dp"/>
<TextView
android:id="#+id/movie_column_title"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>

Simply set the width of the root layout for your column_item.xml to match_parent will center RecyclerView items horizontally.

Wrap your recyclerview inside the constraintlayout, something like this:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/book_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="2" />
Hope it helps!!

Related

RecyclerView inside of NestedScrollView only showing 1 element at a time

I have a RecyclerView inside of a NestedScrollView, my problem is that the RecyclerView is very short, only large enough to show 1 element at a time. The dataset is small (3-5 elements) so my goal is to show all elements at once.
I've tried disabling nested scrolling, changing the layout_height to wrap_content/match_parent, etc. but to no avail.
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="#color/white"
android:paddingBottom="52dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<!-- A bunch of other layouts -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/documents_rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingBottom="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/title"
app:layout_constraintTop_toBottomOf="#id/doc1"
tools:visibility="visible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Update, item xml (I know it could be replaced with just 1 TextView, but for now it will stay as is):
<?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="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/doc_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_doc"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/doc_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:gravity="center_vertical" />
</LinearLayout>
Not an ideal solution since it uses nested layouts, but I was able to display all items by wrapping my RecyclerView in a RelativeLayout:
<RelativeLayout
android:id="#+id/list_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/title"
app:layout_constraintTop_toBottomOf="#id/title">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/documents_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

RecyclerView card items not centering, when RecyclerView parent is ConstraintLayout?

I have a list of card views which has the flag of the country and name of it. The CardView has a ConstraintLayout as a child for aligning the views inside, and has a LinearLayout as parent.
The RecyclerView in MainActivity has a ConstraintLayout as Parent(which is the parent view by default created by AndroidStudio when doing a new project)
<?xml version="1.0" encoding="utf-8"?> <!-- MAIN ACTIVITY XML-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="#dimen/card_view_item_main_width"
android:layout_height="#dimen/card_view_item_main_height">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/countryCardImage"
android:layout_width="match_parent"
android:layout_height="128dp"
android:contentDescription="#string/imagen_pais"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<TextView
android:id="#+id/countryCardText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="1"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <!-- ITEMMAIN.XML -->
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.ui.main.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/listMain"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_weight="0"
/>
</android.support.constraint.ConstraintLayout>
In your item root view, the layout_gravity="center" is not going to work on LinearLayout with layout_width="wrap_content: on a RecyclerView, so you could try changing its width to match_parent, and either change the gravity to android:gravity="center" or move android:layout_gravity="center" to its child view:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="10dp"
android:layout_width=“match_parent”
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="#dimen/card_view_item_main_width"
android:layout_height="#dimen/card_view_item_main_height"
android:layout_gravity="center">
The problem is in layout item, you set the layout_width="wrap_content" in LinearLayout, and in main layout, layout_width in Recyclerview is 0dp (must be match_parent) so it's not center in main layout because the main layout depend on item layout => wrong with this case.
In your layout item, change like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

Constraint Layout does'nt constraint nested elements of other layouts

I'm trying to create a layout of a list which starts on the bottom of a button and it's 3/4 of screen's width.
Just like this:
First, I used nested Linear layouts a horizonal and a vertical one, but then I read here that Constraint layouts are more responsive and I was impressed and gave it a try.
So, I tried to replace the outer layout to a constraint layout but now, I can't constraint the list to start at the bottom of the button and it starts at top of the screen.
I tried to see what would happen if I replaced the whole linear layout with a button just to see that the attributes are correctly implemented and it worked, so I understood that the Constraint layout does'nt effect the sons of the Linear layout although it should be effecting the whole layout (as I see it).
my code is the following:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:src="#drawable/ic_menu_black_32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/list_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#+id/settings_button"
app:layout_constraintTop_toBottomOf="#+id/settings_button">
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
My current layout looks like this:
I really appriciate the helpers!
You need to change layout height of linear layout to 0dp, I will put here the layout for you. Also consider migrating to androidx :)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<ImageButton
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:src="#drawable/ic_menu_black_32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/list_linear_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/settings_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/settings_button">
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I think, that using match_parent as width or height of child and adding any constraint is wrong here.
Edited xml (change margins if you want):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:backgroundTint="#FFFFFF"
android:src="#drawable/ic_menu_black_32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/list_linear_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/settings_button"
app:layout_constraintTop_toBottomOf="#+id/settings_button"
app:layout_constraintWidth_percent="0.75">
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>

RecyclerView does not show all items inside Fragment

The problem
In my Activity I initialize a Fragment, which contains a RecyclerView. But I've recognized that it is not displaying all items. That means instead of 14 it only shows 11 items. In addition the last visible item (number 12) is cutted off
My Implementation
The Activity contains an empty FrameLayout that acts as the Fragment container:
<?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:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/layout_border_white"
android:paddingBottom="#dimen/footer_padding">
<include layout="#layout/footer"></include>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/layout_footer"/>
</RelativeLayout>
And here is the RecyclerView in the Fragment:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="#dimen/startpage_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<include layout="#layout/header"></include>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="#dimen/startpage_progressbar"
android:layout_height="#dimen/startpage_progressbar"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="#dimen/startpage_text_padding"
android:text="#string/error_message"
android:textSize="#dimen/startpage_text"
android:visibility="invisible"/>
</LinearLayout>
Solving Approaches
I've already tried it with match_parent in the RecyclerView and also added a paddingBottom to it. With paddingBottom="30dp" one more element is visible but that's not a good solution. Furthermore my Activity is using the AlertDialog-Theme which is setted in the Manifest. Removing it shows the same result.
Any help would be greatly appreciated.
I finally fixed it. The problem was the wrap_content in my RecyclerView inside ConstraintLayout. By using wrap_content for the height of the RecyclerView the parent cuts the bottom of it off. So you have to use 0dp for the height and a constraintBottom.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>
try to change your xml to following, as constraint layout wraps spaces when used in incorrect way:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<include layout="#layout/support_simple_spinner_dropdown_item"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
android:layout_below="+id/layout_header"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Error"
android:textSize="20sp"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>
I used wrap_content for the height but the bottom of the recycleview still scrolled off bottom of the screen so that the last item was not visible. I resorted to using a finagled solution of adding a bottom padding on the recyclerview until the last item was visible

Android Textview Scrolling Issue

Firstly, I am new to android so if I have missed something basic I do apologise.
I have created a GridView to store some information. The GridView consists of a 3x6 grid. In each cell, there is an image and text directly under the image. As the GridView is so large I have implemented a scrollbar.
This is how the page should look normally and is when first loaded/selected :
Everything looks wonderful, however, when I scroll down to the bottom and then back up, the text seems to wrap up and then push the image out of place. I am not sure why this is?
This is how the page looks after I have scrolled down and they scroll back up :
For the GridView, I have an XML file with the GridView itself along with a TextView at the very top called content_rewards.xml. I have also create an XML file for each picture/text in the grid itself called relics_gridlayout.xml. I believe I am probably missing a property of the TextViews in either one of these files, however, I am not sure which one or which property. I don't think it has anything to do with the grid or adapter itself hence I will not include that code.
content_rewards.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">
<GridView
android:id="#+id/customgrid"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/os_texts"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="35dp"
android:scrollbars="horizontal"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="116dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="B = 0 S = 0 G = 0"
android:textSize="24sp"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/customgrid"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
relics_gridlayout.xml
<?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:padding="3dp">
<ImageView
android:id="#+id/os_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_below="#+id/os_images"
android:id="#+id/os_texts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is Just Dummy Text"
android:textAlignment="center"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
Any help would be great. Thanks.
<?xml version="1.0" encoding="utf-8"?>
<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:background="#ECEFF1"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<GridView
android:id="#+id/grd1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:horizontalSpacing="6dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="6dp">
</GridView>
</RelativeLayout>
This should solve your problem.
The scrolling view behavior will handle the scrolling irrespective of the size
Now add the Text view out of the Relative layout to view it always.
Or have a parent as Nested scrolling View and allow the nested scrolling enabled as false
setNestedScrollingEnabled(false)
A sample from my code that I already have used successfully to resolve such issue
Use your Grid view instead of Recycler View.
If you want the Item Text View to be always of 2 lines then set munLines="2" in the item Layout
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_container_reports"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/tab_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="#string/user_reports"
android:textAllCaps="true"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/tab_label"
app:textAllCaps="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/reports_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:nestedScrollingEnabled="false"
android:padding="4dp" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
This is a problem that i faced as well , the only solution that i could come up with was hardcoding the height of the grid item.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="3dp">
A better option would be to use recycler view with a grid layout manager.
you can use LinearLayout and assign weights so that every item in your grid view occupy same space
Update the relics_gridlayout.xml with below code ( you may need to adjust gravity and layout weights)Try 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:id="#+id/linearLayout_root_single_home_option"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/os_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_weight="9" />
<TextView
android:id="#+id/os_texts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Just Dummy Text"
android:textSize="18dp"
android:textStyle="bold"
android:layout_weight="1" />
</LinearLayout>

Categories

Resources