Search code examples
androidandroid-layoutlayoutscreen-size

How can you make your android application occupy only half of the screen?


I have a list, but it will occupy all the screen of the tablet. Is there a way to make it only occupy half , or 1/4 of the tablet screen?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" />

</LinearLayout>

Solution

  • You can use the following in your XML for vertical split of screen: Here we are using weightsum and weight attributes. You can adjust the weight property to get the desired results in terms of half or quarter etc.

    <LinearLayout 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"
    android:orientation="vertical"
    android:weightSum="100" >
    
      <RelativeLayout
         android:id="@+id/button1"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="50"
         android:background="@android:color/black" >
    
         <!-- Your RelativeLayout Items -->
    
      </RelativeLayout>
    
    </LinearLayout>
    

    In order to get the same in horizontal manner, the below would be useful:

    <LinearLayout 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"
      android:background="@android:color/darker_gray"
      android:orientation="horizontal"
      android:weightSum="100" >
    
      <RelativeLayout
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:background="@android:color/black" >
    
          <!-- Your RelativeLayout Items -->
    
      </RelativeLayout>
    
    </LinearLayout>
    

    EDIT: To display as widgets, You can refer tutorials here http://www.vogella.com/tutorials/AndroidWidgets/article.html and here http://www.tutorialspoint.com/android/android_widgets.htm