How to make a “perfect” 2 grid layout using RecyclerView and a single ImageView












0















I am trying to make a 2-grid layout using a RecyclerView with GridView as the layout manager. So far I have a single image view under a relative layout in the xml file, but it's never "perfectly" aligned to the center. Also, when trying to drag the single image view in the xml file Design tab, the image "jumps" when I try to make it more "centerish" (Won't let me drag the image to the correct position).



What do I need to change, in order to make the 2-grid with a single image view be centered?



This is the xml code for the single item:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/icon"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>


This is the RecyclerView xml:



<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">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>


And this is how it looks:
enter image description here










share|improve this question

























  • hai issue fixed?

    – Jins Lukose
    Nov 21 '18 at 8:17
















0















I am trying to make a 2-grid layout using a RecyclerView with GridView as the layout manager. So far I have a single image view under a relative layout in the xml file, but it's never "perfectly" aligned to the center. Also, when trying to drag the single image view in the xml file Design tab, the image "jumps" when I try to make it more "centerish" (Won't let me drag the image to the correct position).



What do I need to change, in order to make the 2-grid with a single image view be centered?



This is the xml code for the single item:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/icon"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>


This is the RecyclerView xml:



<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">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>


And this is how it looks:
enter image description here










share|improve this question

























  • hai issue fixed?

    – Jins Lukose
    Nov 21 '18 at 8:17














0












0








0








I am trying to make a 2-grid layout using a RecyclerView with GridView as the layout manager. So far I have a single image view under a relative layout in the xml file, but it's never "perfectly" aligned to the center. Also, when trying to drag the single image view in the xml file Design tab, the image "jumps" when I try to make it more "centerish" (Won't let me drag the image to the correct position).



What do I need to change, in order to make the 2-grid with a single image view be centered?



This is the xml code for the single item:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/icon"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>


This is the RecyclerView xml:



<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">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>


And this is how it looks:
enter image description here










share|improve this question
















I am trying to make a 2-grid layout using a RecyclerView with GridView as the layout manager. So far I have a single image view under a relative layout in the xml file, but it's never "perfectly" aligned to the center. Also, when trying to drag the single image view in the xml file Design tab, the image "jumps" when I try to make it more "centerish" (Won't let me drag the image to the correct position).



What do I need to change, in order to make the 2-grid with a single image view be centered?



This is the xml code for the single item:



    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/icon"
android:layout_width="118dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>


This is the RecyclerView xml:



<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">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>


And this is how it looks:
enter image description here







android android-recyclerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 3:58









Nilesh Rathod

32.7k83057




32.7k83057










asked Nov 20 '18 at 10:43









TTnoteTTnote

969




969













  • hai issue fixed?

    – Jins Lukose
    Nov 21 '18 at 8:17



















  • hai issue fixed?

    – Jins Lukose
    Nov 21 '18 at 8:17

















hai issue fixed?

– Jins Lukose
Nov 21 '18 at 8:17





hai issue fixed?

– Jins Lukose
Nov 21 '18 at 8:17












2 Answers
2






active

oldest

votes


















4














Instead of giving width to ImageView, try making it match_parent






share|improve this answer

































    0














    <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">

    <ImageView
    android:id="@+id/icon"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:contentDescription="TODO"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher_background" />
    </android.support.constraint.ConstraintLayout>


    Try this whether it will helps you or not.






    share|improve this answer


























    • what should be instead of frame2 in my code? because for me it's not defined

      – TTnote
      Nov 20 '18 at 12:23











    • no need of that line

      – Nivedh
      Nov 21 '18 at 8:04











    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',
    autoActivateHeartbeat: false,
    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%2f53391250%2fhow-to-make-a-perfect-2-grid-layout-using-recyclerview-and-a-single-imageview%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









    4














    Instead of giving width to ImageView, try making it match_parent






    share|improve this answer






























      4














      Instead of giving width to ImageView, try making it match_parent






      share|improve this answer




























        4












        4








        4







        Instead of giving width to ImageView, try making it match_parent






        share|improve this answer















        Instead of giving width to ImageView, try making it match_parent







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 3:58









        Nilesh Rathod

        32.7k83057




        32.7k83057










        answered Nov 20 '18 at 10:49









        ahuja007ahuja007

        21119




        21119

























            0














            <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">

            <ImageView
            android:id="@+id/icon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="TODO"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_background" />
            </android.support.constraint.ConstraintLayout>


            Try this whether it will helps you or not.






            share|improve this answer


























            • what should be instead of frame2 in my code? because for me it's not defined

              – TTnote
              Nov 20 '18 at 12:23











            • no need of that line

              – Nivedh
              Nov 21 '18 at 8:04
















            0














            <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">

            <ImageView
            android:id="@+id/icon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="TODO"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_background" />
            </android.support.constraint.ConstraintLayout>


            Try this whether it will helps you or not.






            share|improve this answer


























            • what should be instead of frame2 in my code? because for me it's not defined

              – TTnote
              Nov 20 '18 at 12:23











            • no need of that line

              – Nivedh
              Nov 21 '18 at 8:04














            0












            0








            0







            <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">

            <ImageView
            android:id="@+id/icon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="TODO"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_background" />
            </android.support.constraint.ConstraintLayout>


            Try this whether it will helps you or not.






            share|improve this answer















            <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">

            <ImageView
            android:id="@+id/icon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="TODO"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_background" />
            </android.support.constraint.ConstraintLayout>


            Try this whether it will helps you or not.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 8:05

























            answered Nov 20 '18 at 11:00









            NivedhNivedh

            738516




            738516













            • what should be instead of frame2 in my code? because for me it's not defined

              – TTnote
              Nov 20 '18 at 12:23











            • no need of that line

              – Nivedh
              Nov 21 '18 at 8:04



















            • what should be instead of frame2 in my code? because for me it's not defined

              – TTnote
              Nov 20 '18 at 12:23











            • no need of that line

              – Nivedh
              Nov 21 '18 at 8:04

















            what should be instead of frame2 in my code? because for me it's not defined

            – TTnote
            Nov 20 '18 at 12:23





            what should be instead of frame2 in my code? because for me it's not defined

            – TTnote
            Nov 20 '18 at 12:23













            no need of that line

            – Nivedh
            Nov 21 '18 at 8:04





            no need of that line

            – Nivedh
            Nov 21 '18 at 8:04


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391250%2fhow-to-make-a-perfect-2-grid-layout-using-recyclerview-and-a-single-imageview%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini