RecyclerView item ConstraintLayout messing up while using GridLayoutManager









up vote
2
down vote

favorite












I'm using GridLayoutManager in my RecyclerView with a spanCount of 2.
Following is the layout of RecyclerView item. It is a ConstraintLayout which is wrapped up in a CardView:



<android.support.v7.widget.CardView
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="wrap_content"
android:layout_margin="@dimen/cardview_margin"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Title"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_sub_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Subtitle"
android:textAllCaps="false"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="@+id/tv_title"
app:layout_constraintStart_toStartOf="@+id/tv_title"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />

<ImageView
android:id="@+id/iv_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:scaleX="1.07"
android:scaleY="1.07"
android:src="@drawable/ic_star_blank_grey"
app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/iv_locked"/>

<ImageView
android:id="@+id/iv_locked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:scaleX="1.0"
android:scaleY="1.0"
android:src="@drawable/ic_lock_open_grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>


The problem arises when GridLayoutManager tends to increase the height of a RecyclerView item according to the height of the item in second column. Both the ImageView icons leave the parent bottom and tend to stick to the TextView tv_sub_title as in the screenshot below:





Kindly let me know how to fix this such that both the icons stay at the bottom when the height of the item layout is increased by the GridLayoutManager.



Thanks










share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm using GridLayoutManager in my RecyclerView with a spanCount of 2.
    Following is the layout of RecyclerView item. It is a ConstraintLayout which is wrapped up in a CardView:



    <android.support.v7.widget.CardView
    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="wrap_content"
    android:layout_margin="@dimen/cardview_margin"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="4dp">

    <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
    android:id="@+id/tv_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Title"
    android:textAllCaps="true"
    android:textColor="@color/colorPrimary"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <TextView
    android:id="@+id/tv_sub_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Subtitle"
    android:textAllCaps="false"
    android:textSize="14sp"
    app:layout_constraintEnd_toEndOf="@+id/tv_title"
    app:layout_constraintStart_toStartOf="@+id/tv_title"
    app:layout_constraintTop_toBottomOf="@+id/tv_title" />

    <ImageView
    android:id="@+id/iv_favorite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:scaleX="1.07"
    android:scaleY="1.07"
    android:src="@drawable/ic_star_blank_grey"
    app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/iv_locked"/>

    <ImageView
    android:id="@+id/iv_locked"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:scaleX="1.0"
    android:scaleY="1.0"
    android:src="@drawable/ic_lock_open_grey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
    app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
    </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>


    The problem arises when GridLayoutManager tends to increase the height of a RecyclerView item according to the height of the item in second column. Both the ImageView icons leave the parent bottom and tend to stick to the TextView tv_sub_title as in the screenshot below:





    Kindly let me know how to fix this such that both the icons stay at the bottom when the height of the item layout is increased by the GridLayoutManager.



    Thanks










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm using GridLayoutManager in my RecyclerView with a spanCount of 2.
      Following is the layout of RecyclerView item. It is a ConstraintLayout which is wrapped up in a CardView:



      <android.support.v7.widget.CardView
      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="wrap_content"
      android:layout_margin="@dimen/cardview_margin"
      android:foreground="?android:attr/selectableItemBackground"
      app:cardBackgroundColor="@android:color/white"
      app:cardCornerRadius="4dp">

      <android.support.constraint.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <TextView
      android:id="@+id/tv_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:layout_marginEnd="8dp"
      android:text="Title"
      android:textAllCaps="true"
      android:textColor="@color/colorPrimary"
      android:textSize="16sp"
      android:textStyle="bold"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

      <TextView
      android:id="@+id/tv_sub_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:text="Subtitle"
      android:textAllCaps="false"
      android:textSize="14sp"
      app:layout_constraintEnd_toEndOf="@+id/tv_title"
      app:layout_constraintStart_toStartOf="@+id/tv_title"
      app:layout_constraintTop_toBottomOf="@+id/tv_title" />

      <ImageView
      android:id="@+id/iv_favorite"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginEnd="8dp"
      android:scaleX="1.07"
      android:scaleY="1.07"
      android:src="@drawable/ic_star_blank_grey"
      app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="@+id/iv_locked"/>

      <ImageView
      android:id="@+id/iv_locked"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="24dp"
      android:layout_marginEnd="8dp"
      android:layout_marginBottom="8dp"
      android:scaleX="1.0"
      android:scaleY="1.0"
      android:src="@drawable/ic_lock_open_grey"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
      app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
      </android.support.constraint.ConstraintLayout>
      </android.support.v7.widget.CardView>


      The problem arises when GridLayoutManager tends to increase the height of a RecyclerView item according to the height of the item in second column. Both the ImageView icons leave the parent bottom and tend to stick to the TextView tv_sub_title as in the screenshot below:





      Kindly let me know how to fix this such that both the icons stay at the bottom when the height of the item layout is increased by the GridLayoutManager.



      Thanks










      share|improve this question















      I'm using GridLayoutManager in my RecyclerView with a spanCount of 2.
      Following is the layout of RecyclerView item. It is a ConstraintLayout which is wrapped up in a CardView:



      <android.support.v7.widget.CardView
      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="wrap_content"
      android:layout_margin="@dimen/cardview_margin"
      android:foreground="?android:attr/selectableItemBackground"
      app:cardBackgroundColor="@android:color/white"
      app:cardCornerRadius="4dp">

      <android.support.constraint.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <TextView
      android:id="@+id/tv_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:layout_marginEnd="8dp"
      android:text="Title"
      android:textAllCaps="true"
      android:textColor="@color/colorPrimary"
      android:textSize="16sp"
      android:textStyle="bold"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

      <TextView
      android:id="@+id/tv_sub_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:text="Subtitle"
      android:textAllCaps="false"
      android:textSize="14sp"
      app:layout_constraintEnd_toEndOf="@+id/tv_title"
      app:layout_constraintStart_toStartOf="@+id/tv_title"
      app:layout_constraintTop_toBottomOf="@+id/tv_title" />

      <ImageView
      android:id="@+id/iv_favorite"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginEnd="8dp"
      android:scaleX="1.07"
      android:scaleY="1.07"
      android:src="@drawable/ic_star_blank_grey"
      app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="@+id/iv_locked"/>

      <ImageView
      android:id="@+id/iv_locked"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="24dp"
      android:layout_marginEnd="8dp"
      android:layout_marginBottom="8dp"
      android:scaleX="1.0"
      android:scaleY="1.0"
      android:src="@drawable/ic_lock_open_grey"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
      app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
      </android.support.constraint.ConstraintLayout>
      </android.support.v7.widget.CardView>


      The problem arises when GridLayoutManager tends to increase the height of a RecyclerView item according to the height of the item in second column. Both the ImageView icons leave the parent bottom and tend to stick to the TextView tv_sub_title as in the screenshot below:





      Kindly let me know how to fix this such that both the icons stay at the bottom when the height of the item layout is increased by the GridLayoutManager.



      Thanks







      android android-recyclerview android-constraintlayout gridlayoutmanager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 19:19

























      asked Nov 10 at 7:11









      Shahood ul Hassan

      409




      409






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          To keep the iv_locked aligned to the bottom between its vertical constraints you need to set the maximum vertical bias by adding the app:layout_constraintVertical_bias="1" attribute
          to it. Since the iv_favorite is vertically constrained to the iv_locked, it will automatically be in the appropriate position.






          share|improve this answer




















          • I have already tried using vertical bias but somehow it doesn't work either.
            – Shahood ul Hassan
            Nov 10 at 10:29

















          up vote
          1
          down vote













          Ok, with some hit & trial and taking @Pawel Laskowski answer a bit further, here is what solved the issue:



          1. Add app:layout_constraintVertical_bias="1" attribute to iv_locked

          2. Change android:layout_height of the ConstraintLayout from wrap_content to match_parent.

          ConstraintLayout is pretty easy to get started with but quite hard to master. Thanks for the help @Pawel Laskowski.






          share|improve this answer
















          • 1




            That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
            – Pawel Laskowski
            Nov 10 at 10:56










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236799%2frecyclerview-item-constraintlayout-messing-up-while-using-gridlayoutmanager%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          To keep the iv_locked aligned to the bottom between its vertical constraints you need to set the maximum vertical bias by adding the app:layout_constraintVertical_bias="1" attribute
          to it. Since the iv_favorite is vertically constrained to the iv_locked, it will automatically be in the appropriate position.






          share|improve this answer




















          • I have already tried using vertical bias but somehow it doesn't work either.
            – Shahood ul Hassan
            Nov 10 at 10:29














          up vote
          1
          down vote













          To keep the iv_locked aligned to the bottom between its vertical constraints you need to set the maximum vertical bias by adding the app:layout_constraintVertical_bias="1" attribute
          to it. Since the iv_favorite is vertically constrained to the iv_locked, it will automatically be in the appropriate position.






          share|improve this answer




















          • I have already tried using vertical bias but somehow it doesn't work either.
            – Shahood ul Hassan
            Nov 10 at 10:29












          up vote
          1
          down vote










          up vote
          1
          down vote









          To keep the iv_locked aligned to the bottom between its vertical constraints you need to set the maximum vertical bias by adding the app:layout_constraintVertical_bias="1" attribute
          to it. Since the iv_favorite is vertically constrained to the iv_locked, it will automatically be in the appropriate position.






          share|improve this answer












          To keep the iv_locked aligned to the bottom between its vertical constraints you need to set the maximum vertical bias by adding the app:layout_constraintVertical_bias="1" attribute
          to it. Since the iv_favorite is vertically constrained to the iv_locked, it will automatically be in the appropriate position.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 9:31









          Pawel Laskowski

          1,4801512




          1,4801512











          • I have already tried using vertical bias but somehow it doesn't work either.
            – Shahood ul Hassan
            Nov 10 at 10:29
















          • I have already tried using vertical bias but somehow it doesn't work either.
            – Shahood ul Hassan
            Nov 10 at 10:29















          I have already tried using vertical bias but somehow it doesn't work either.
          – Shahood ul Hassan
          Nov 10 at 10:29




          I have already tried using vertical bias but somehow it doesn't work either.
          – Shahood ul Hassan
          Nov 10 at 10:29












          up vote
          1
          down vote













          Ok, with some hit & trial and taking @Pawel Laskowski answer a bit further, here is what solved the issue:



          1. Add app:layout_constraintVertical_bias="1" attribute to iv_locked

          2. Change android:layout_height of the ConstraintLayout from wrap_content to match_parent.

          ConstraintLayout is pretty easy to get started with but quite hard to master. Thanks for the help @Pawel Laskowski.






          share|improve this answer
















          • 1




            That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
            – Pawel Laskowski
            Nov 10 at 10:56














          up vote
          1
          down vote













          Ok, with some hit & trial and taking @Pawel Laskowski answer a bit further, here is what solved the issue:



          1. Add app:layout_constraintVertical_bias="1" attribute to iv_locked

          2. Change android:layout_height of the ConstraintLayout from wrap_content to match_parent.

          ConstraintLayout is pretty easy to get started with but quite hard to master. Thanks for the help @Pawel Laskowski.






          share|improve this answer
















          • 1




            That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
            – Pawel Laskowski
            Nov 10 at 10:56












          up vote
          1
          down vote










          up vote
          1
          down vote









          Ok, with some hit & trial and taking @Pawel Laskowski answer a bit further, here is what solved the issue:



          1. Add app:layout_constraintVertical_bias="1" attribute to iv_locked

          2. Change android:layout_height of the ConstraintLayout from wrap_content to match_parent.

          ConstraintLayout is pretty easy to get started with but quite hard to master. Thanks for the help @Pawel Laskowski.






          share|improve this answer












          Ok, with some hit & trial and taking @Pawel Laskowski answer a bit further, here is what solved the issue:



          1. Add app:layout_constraintVertical_bias="1" attribute to iv_locked

          2. Change android:layout_height of the ConstraintLayout from wrap_content to match_parent.

          ConstraintLayout is pretty easy to get started with but quite hard to master. Thanks for the help @Pawel Laskowski.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 10:42









          Shahood ul Hassan

          409




          409







          • 1




            That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
            – Pawel Laskowski
            Nov 10 at 10:56












          • 1




            That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
            – Pawel Laskowski
            Nov 10 at 10:56







          1




          1




          That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
          – Pawel Laskowski
          Nov 10 at 10:56




          That's correct, good find. match_parent for height is needed for the ConstraintLayout to fill the height of the CardView. Otherwise there will not be any difference. It's easy to miss.
          – Pawel Laskowski
          Nov 10 at 10:56

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236799%2frecyclerview-item-constraintlayout-messing-up-while-using-gridlayoutmanager%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo