3

How can i change Been programmatically Gridview 'android:numColumns="2"' when device size larger than 5 inch Or How can i detect Activity create on tablet and set 'numColumns=3'.

thanks.

  • To check if device is tablet or phone : refer [link 1](http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet) ,[link 2](http://stackoverflow.com/questions/5832368/tablet-or-phone-android) – Manishika Jul 11 '14 at 06:03

3 Answers3

2

Set numColums as auto_fit to Display as many columns as possible to fill the available space.

null pointer
  • 5,874
  • 4
  • 36
  • 66
1

what you can do in this situation is create two different layouts for devices bigger than 5 inches and devices having screen space less than 5 inches in those layouts you can set the number of columns in XML as you wish.

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
1

Try this:

       <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit" 
        >
        </GridView>

And In .java file

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int density = metrics.densityDpi;


        int width = 0, height = 0;
        metrics = new DisplayMetrics();        
         getWindowManager().getDefaultDisplay().getMetrics(metrics);


         height = metrics.heightPixels;     
         width = metrics.widthPixels;

         if(width >480 && width <780)
         {
             setContentView(R.layout.home_student);
             gridView = (GridView) findViewById(R.id.gridView1); 

                gridView.setNumColumns(3);
               if(density <200)
               {
                   h=200;
                   w=200;
                   CustomAdapterStudent mAdapter;
                   mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w); 
                   gridView.setAdapter(mAdapter); 
               }
               else
               {
                   h=200;
                   w=200;
                   CustomAdapterStudent mAdapter;
                   mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w); 
                   gridView.setAdapter(mAdapter); 
               }
        }
         else if(width<=480)
         {
             setContentView(R.layout.home_student);
             gridView = (GridView) findViewById(R.id.gridView1); 

                gridView.setNumColumns(3);
                   h=150;
                   w=150;
                   CustomAdapterStudent mAdapter;
                   mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w); 

                     gridView.setAdapter(mAdapter); 

         }

         else if(width >= 780)
         {
             setContentView(R.layout.home_student);
             gridView = (GridView) findViewById(R.id.gridView1); 

                gridView.setNumColumns(4);
               if(density <200)
               {
                   h=200;
                   w=200;
                   CustomAdapterStudent mAdapter;
                   mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w); 
                   gridView.setAdapter(mAdapter); 
               }
               else
               {
                   h=200;
                   w=200;
                   CustomAdapterStudent mAdapter;
                   mAdapter = new CustomAdapterStudent(this,prgmNameList, prgmImages,h,w); 
                   gridView.setAdapter(mAdapter); 
               }
         }

Hope This may help you!

Krupa Patel
  • 3,309
  • 3
  • 23
  • 28