How to make span count and icon size automatic











up vote
0
down vote

favorite
1












I am using recycler view with card view to show icons in my application.
I am not able to figure out how to resize the app icons and increase/decrease the span count automatically depending on the screen size. Here is what I am doing to display it and the spen count is fixed to 3.



    RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
mrv.setLayoutManager(new GridLayoutManager(this, 3));
mrv.setAdapter(myAdapter);


The XMLs are below



<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardview_id"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_marginVertical="20dp"
cardview:cardCornerRadius="6dp">


<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/store_image_id"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:background="#2D2D2D"/>

<TextView
android:id="@+id/store_name_id"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/StoreTitle"/>


</android.support.v7.widget.LinearLayoutCompat>


The app is doing nice in a screen size of around 5"5' to 6" screen but on screens smaller/bigger that, the icons looks messy. Can someone please guide me how to achieve this?










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I am using recycler view with card view to show icons in my application.
    I am not able to figure out how to resize the app icons and increase/decrease the span count automatically depending on the screen size. Here is what I am doing to display it and the spen count is fixed to 3.



        RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
    RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
    mrv.setLayoutManager(new GridLayoutManager(this, 3));
    mrv.setAdapter(myAdapter);


    The XMLs are below



    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardview_id"
    android:clickable="true"
    android:foreground="?selectableItemBackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:layout_marginVertical="20dp"
    cardview:cardCornerRadius="6dp">


    <android.support.v7.widget.LinearLayoutCompat
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
    android:id="@+id/store_image_id"
    android:layout_width="wrap_content"
    android:layout_height="110dp"
    android:scaleType="centerCrop"
    android:background="#2D2D2D"/>

    <TextView
    android:id="@+id/store_name_id"
    android:textColor="#2d2d2d"
    android:textSize="13sp"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/StoreTitle"/>


    </android.support.v7.widget.LinearLayoutCompat>


    The app is doing nice in a screen size of around 5"5' to 6" screen but on screens smaller/bigger that, the icons looks messy. Can someone please guide me how to achieve this?










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I am using recycler view with card view to show icons in my application.
      I am not able to figure out how to resize the app icons and increase/decrease the span count automatically depending on the screen size. Here is what I am doing to display it and the spen count is fixed to 3.



          RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
      RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
      mrv.setLayoutManager(new GridLayoutManager(this, 3));
      mrv.setAdapter(myAdapter);


      The XMLs are below



      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.CardView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/cardview_id"
      android:clickable="true"
      android:foreground="?selectableItemBackground"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      xmlns:cardview="http://schemas.android.com/apk/res-auto"
      android:layout_marginLeft="15dp"
      android:layout_marginRight="15dp"
      android:layout_marginTop="15dp"
      android:layout_marginVertical="20dp"
      cardview:cardCornerRadius="6dp">


      <android.support.v7.widget.LinearLayoutCompat
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


      <ImageView
      android:id="@+id/store_image_id"
      android:layout_width="wrap_content"
      android:layout_height="110dp"
      android:scaleType="centerCrop"
      android:background="#2D2D2D"/>

      <TextView
      android:id="@+id/store_name_id"
      android:textColor="#2d2d2d"
      android:textSize="13sp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="@string/StoreTitle"/>


      </android.support.v7.widget.LinearLayoutCompat>


      The app is doing nice in a screen size of around 5"5' to 6" screen but on screens smaller/bigger that, the icons looks messy. Can someone please guide me how to achieve this?










      share|improve this question













      I am using recycler view with card view to show icons in my application.
      I am not able to figure out how to resize the app icons and increase/decrease the span count automatically depending on the screen size. Here is what I am doing to display it and the spen count is fixed to 3.



          RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
      RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
      mrv.setLayoutManager(new GridLayoutManager(this, 3));
      mrv.setAdapter(myAdapter);


      The XMLs are below



      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.CardView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/cardview_id"
      android:clickable="true"
      android:foreground="?selectableItemBackground"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      xmlns:cardview="http://schemas.android.com/apk/res-auto"
      android:layout_marginLeft="15dp"
      android:layout_marginRight="15dp"
      android:layout_marginTop="15dp"
      android:layout_marginVertical="20dp"
      cardview:cardCornerRadius="6dp">


      <android.support.v7.widget.LinearLayoutCompat
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


      <ImageView
      android:id="@+id/store_image_id"
      android:layout_width="wrap_content"
      android:layout_height="110dp"
      android:scaleType="centerCrop"
      android:background="#2D2D2D"/>

      <TextView
      android:id="@+id/store_name_id"
      android:textColor="#2d2d2d"
      android:textSize="13sp"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="@string/StoreTitle"/>


      </android.support.v7.widget.LinearLayoutCompat>


      The app is doing nice in a screen size of around 5"5' to 6" screen but on screens smaller/bigger that, the icons looks messy. Can someone please guide me how to achieve this?







      android android-recyclerview android-cardview android-gridlayout






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 12:05









      Kandarpa

      175




      175
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can calculate number of columns. Define a static function to calculate the number of columns as:



          public class Utility {
          public static int calculateNoOfColumns(Context context) {
          DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
          float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
          int noOfColumns = (int) (dpWidth / 180);
          // Where 180 is the width of your grid item. You can change it as per your convention.
          return noOfColumns;
          }
          }


          And then, when using the GridLayoutManager in the activity or fragment you can do like this:



          int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

          // Some code...

          mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
          mRecyclerView.setLayoutManager(mGridLayoutManager);

          // Some code...


          I hope, this helps you.






          share|improve this answer

















          • 1




            Thank you Nitin. It helped.
            – Kandarpa
            Nov 8 at 16:28










          • You're welcome, Kandarpa. :-)
            – Nitin Gurbani
            Nov 9 at 4:55











          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%2f53207401%2fhow-to-make-span-count-and-icon-size-automatic%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You can calculate number of columns. Define a static function to calculate the number of columns as:



          public class Utility {
          public static int calculateNoOfColumns(Context context) {
          DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
          float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
          int noOfColumns = (int) (dpWidth / 180);
          // Where 180 is the width of your grid item. You can change it as per your convention.
          return noOfColumns;
          }
          }


          And then, when using the GridLayoutManager in the activity or fragment you can do like this:



          int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

          // Some code...

          mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
          mRecyclerView.setLayoutManager(mGridLayoutManager);

          // Some code...


          I hope, this helps you.






          share|improve this answer

















          • 1




            Thank you Nitin. It helped.
            – Kandarpa
            Nov 8 at 16:28










          • You're welcome, Kandarpa. :-)
            – Nitin Gurbani
            Nov 9 at 4:55















          up vote
          1
          down vote



          accepted










          You can calculate number of columns. Define a static function to calculate the number of columns as:



          public class Utility {
          public static int calculateNoOfColumns(Context context) {
          DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
          float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
          int noOfColumns = (int) (dpWidth / 180);
          // Where 180 is the width of your grid item. You can change it as per your convention.
          return noOfColumns;
          }
          }


          And then, when using the GridLayoutManager in the activity or fragment you can do like this:



          int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

          // Some code...

          mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
          mRecyclerView.setLayoutManager(mGridLayoutManager);

          // Some code...


          I hope, this helps you.






          share|improve this answer

















          • 1




            Thank you Nitin. It helped.
            – Kandarpa
            Nov 8 at 16:28










          • You're welcome, Kandarpa. :-)
            – Nitin Gurbani
            Nov 9 at 4:55













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can calculate number of columns. Define a static function to calculate the number of columns as:



          public class Utility {
          public static int calculateNoOfColumns(Context context) {
          DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
          float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
          int noOfColumns = (int) (dpWidth / 180);
          // Where 180 is the width of your grid item. You can change it as per your convention.
          return noOfColumns;
          }
          }


          And then, when using the GridLayoutManager in the activity or fragment you can do like this:



          int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

          // Some code...

          mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
          mRecyclerView.setLayoutManager(mGridLayoutManager);

          // Some code...


          I hope, this helps you.






          share|improve this answer












          You can calculate number of columns. Define a static function to calculate the number of columns as:



          public class Utility {
          public static int calculateNoOfColumns(Context context) {
          DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
          float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
          int noOfColumns = (int) (dpWidth / 180);
          // Where 180 is the width of your grid item. You can change it as per your convention.
          return noOfColumns;
          }
          }


          And then, when using the GridLayoutManager in the activity or fragment you can do like this:



          int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

          // Some code...

          mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
          mRecyclerView.setLayoutManager(mGridLayoutManager);

          // Some code...


          I hope, this helps you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 14:15









          Nitin Gurbani

          661115




          661115








          • 1




            Thank you Nitin. It helped.
            – Kandarpa
            Nov 8 at 16:28










          • You're welcome, Kandarpa. :-)
            – Nitin Gurbani
            Nov 9 at 4:55














          • 1




            Thank you Nitin. It helped.
            – Kandarpa
            Nov 8 at 16:28










          • You're welcome, Kandarpa. :-)
            – Nitin Gurbani
            Nov 9 at 4:55








          1




          1




          Thank you Nitin. It helped.
          – Kandarpa
          Nov 8 at 16:28




          Thank you Nitin. It helped.
          – Kandarpa
          Nov 8 at 16:28












          You're welcome, Kandarpa. :-)
          – Nitin Gurbani
          Nov 9 at 4:55




          You're welcome, Kandarpa. :-)
          – Nitin Gurbani
          Nov 9 at 4:55


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207401%2fhow-to-make-span-count-and-icon-size-automatic%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

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word