Search code examples
javaandroidandroid-layoutdpi

Layout for different size screens (Android)


I'm working on an app, it looks great on 7" devices (Nexus and Dell streak 7). I created the images from different densities, so it looks good.

However, when I try to run the app on my phone (Samsung Galaxy 2) or Tablet 10" it doesn't look good at all.

Here is how it looks on the nexus 7":

enter image description here

Here is how it looks on the Galaxy 2 phone: enter image description here

Here is how it looks on the 10" Tablet:

enter image description here

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/background1" >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="@string/tour_menu"
                android:textSize="@dimen/font_large" />   
    </LinearLayout> 
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        <TextView
                android:id="@+id/tour_info"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="@dimen/font_medium" />   
    </LinearLayout> 
    <TableLayout  
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:shrinkColumns="*"
            android:paddingRight="5dp"
            android:orientation="vertical" >
        <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/passenger_list"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:gravity="center_horizontal|center_vertical"
                        android:background="@drawable/pax_list"
                        android:contentDescription="@string/pass_list"  />    
                <TextView
                        android:id="@+id/passenger_list_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/pass_list"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/arrivals"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal|center_vertical"
                        android:background="@drawable/arrivals"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:padding="44dp"
                        android:contentDescription="@string/arrivals" />
                <TextView
                        android:id="@+id/arrivals_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/arrivals"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/departures"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/departures"
                        android:contentDescription="@string/departures" />
                <TextView
                        android:id="@+id/departures_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/departures"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/master_rooming"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/master_rooming_list"
                        android:contentDescription="@string/rooming" />
                <TextView
                        android:id="@+id/master_rooming_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/rooming"
                        android:textSize="@dimen/font_medium" />        
            </LinearLayout>
        </TableRow>
        <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="50dp" >
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/itinerary"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/itinerary"
                        android:contentDescription="@string/itinerary" />   
                <TextView
                        android:id="@+id/itinerary_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/itinerary"
                        android:textSize="@dimen/font_medium" />    
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/voucher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/vouchers"
                        android:contentDescription="@string/voucher" />  
                <TextView
                        android:id="@+id/voucher_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/voucher"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>         
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                <ImageButton
                        android:id="@+id/qa"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/qa_list"
                        android:contentDescription="@string/qa" />  

                <TextView
                        android:id="@+id/qa_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/qa"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/optionals"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/optionals"
                        android:contentDescription="@string/itinerary" />   
                <TextView
                        android:id="@+id/optionals_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/optionals"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
        </TableRow> 
        <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="50dp" >
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/flights"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/flights"
                        android:contentDescription="@string/itinerary" />   
                <TextView
                        android:id="@+id/flights_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/flights"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>             
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/close_tour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/close_tour"
                        android:contentDescription="@string/close" />  

                <TextView 
                        android:id="@+id/close_tour_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/close"
                        android:textSize="@dimen/font_medium" />                    
            </LinearLayout>
                <!--
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/voucher" />  
                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/voucher"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>         
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/qa" />  

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/qa"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/close" />  

                <TextView 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/close"
                        android:textSize="@dimen/font_medium" />
            </LinearLayout>  -->    
        </TableRow> 

    </TableLayout>
    <!-- Time Stamp at the bottom of the screen -->
    <TextView
            android:id="@+id/timeStamp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:gravity="bottom|center"
            android:textSize="@dimen/font_medium" />
</LinearLayout>

Solution

  • OK after seing your layout file, I think you should do one of the following things:

    1. Use a GridView
    2. Use ImageView instead of ImageButtons

    Using GridView can save you a lot of work. Take a look at this website and you might find what you need: http://www.androidviews.net/

    If you chose to use ImageViews, you should set the following preferences:

    adjustViewBounds="true"
    scaleType="centerInside"
    

    That way your images will keep their ratio.

    Hope it helps!