2

this is my first question on StackOverflow =)

I am using Android Studio 1.0.2 (top update) and I want to create a grid layout that has one column on Nexus 5 portrait and two on Nexus 5 landscape. Nexus 5 is 640 x 360 dp, so I have three folders "layout" and "layout-w600dp" and "layout-w600dp-land" (to be extra sure).

My "fragment_main.xml" in "layout" folder looks like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">
    <GridView
        android:id="@+id/gridview_products"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="1"/>
</FrameLayout>    

In "layout-w600dp" and "layout-w600dp-land" folders the only difference is

android:numColumns="2"

In Android Studio it shows me that "layout-w600dp-land" is used. Screen is here: https://pp.vk.me/c625530/v625530960/19a4d/G7irG51csn8.jpg And on real device when I rotate the screen it doesn't make two columns. Screen is here: https://pp.vk.me/c625530/v625530960/19a57/mmCdluXWSQw.jpg

What can cause this problem?

  • I'm a bit confused. The two screens do not appear to be related. Did you post the wrong image? – Reenactor Rob Feb 03 '15 at 20:57
  • This is the same fragment, just in Android Studio it is not filled with data, so it just shows "Item1, Sub item 1" and at onCreateView I fill it with data using custom made ArrayAdapter. – Bohdan Kachmar Feb 03 '15 at 21:18

2 Answers2

2

You can just rename "layout-w600dp" to "layout-w595dp" and it will work on Nexus 5. To get more responsive design you can also create layouts with more than two columns for bigger screens.

Ross Khanas
  • 1,491
  • 2
  • 15
  • 23
  • More details see Content View size: http://www.emirweb.com/ScreenDeviceStatistics.php#Header248 – user802421 Jun 24 '15 at 03:32
  • Can anyone explain this? I had hoped you could go by [Google device metrics](https://design.google.com/devices/) but clearly that's not enough. To make matters worse, the Content View size appears to differ by 1) API level and 2) orientation. – sidecarcat Jun 09 '16 at 00:58
0

When you have multiple columns in a grid view, you must define a column width. This is a grid view from one of my apps.

 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:padding="10dp"
        android:numColumns="2"
        android:columnWidth="100dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"      
        android:gravity="center"
        android:background="@android:color/transparent"
        android:stretchMode="columnWidth" >

</GridView>

If needed you can also use android:numColumns="auto_fit" to have it create as many columns as possible of size columnWidth.

Reenactor Rob
  • 1,508
  • 1
  • 11
  • 20
  • And if I want columnWidth to be half of the device screen, not a hard coded number? – Bohdan Kachmar Feb 03 '15 at 21:42
  • columnWidth="0dp" and then add android:layout_weight="2". Note that you can only set either columnWidth or columnHeight to 0dp, but not both at the same time. Then layout_weight works with the one set to "0dp". – Reenactor Rob Feb 03 '15 at 21:52