Search code examples
androidandroid-layoutandroid-linearlayout

Android Designing with LinearLayout , how it works?


I am using an Android LinearLayout for a simple page, where I have added 1 ImageView and 1 GridView for both portrait and landscape mode from a single AXML file.

I want in landscape mode all the item comes in one line and in portrait mode gridvew will cut down to the second line.

How can I solve this.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">  
  <ImageView
           android:id="@+id/imageView2"
           android:layout_width="300dp"
           android:layout_height="100dp"           
           android:src="@drawable/TESTIMAGEGOF"
           android:layout_marginBottom="1dp" />
    <GridView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:columnWidth="75dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:stretchMode="spacingWidthUniform"
        android:id="@+id/gridView1" />

</LinearLayout>

Solution

  • For setting in one layout, you must use java code for this problem, you should do 2 step:

    1- you must check orientation in device with java:

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    
    }
    

    2- for set orientation in your LinearLayout try this code:

    LinearLayout layout = /* ... */;
    layout.setOrientation(LinearLayout.VERTICAL);
    

    you can use this technique.