Brandon Cormier
Brandon Cormier

Reputation: 70

Beginner Android layout

So I am new to Android development and have a question on layouts. When I create my app in Eclipse I have it set up so that I can see what it would look like on a Nexus S (4 in., 480 x 800). This looks great when I open it on my Exhibit 2 4G (3.7 in., 480 x 800), however it looks terrible when I open it on the Nexus 7. The buttons are in the wrong places and things just don't look the same. I am assuming this has to do with layouts? When I create a Windows application in java I use a layout such as the border layout, grid layout, flow layout, etch... and then add it to the frame. That way no matter how big I resize the window, it always looks the same. How do you do this in Android? Also, kinda off topic, but when displaying numbers in a textbox how do you make it so that it only shows up to the hundredths place? I am starting out creating apps that deal with tips, sale prices, etch, and don't need the final calculated price to be to the 5th decimal place.

Here is my layout code:

<RelativeLayout 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=".Main" >

    <TextView
        android:id="@+id/tagpricetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="21dp"
        android:text="Enter tag price:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tagpricetext"
        android:layout_marginTop="36dp"
        android:layout_toLeftOf="@+id/tagprice"
        android:text="Enter % off:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/calc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tagpricetext"
        android:layout_alignRight="@+id/percentoff"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="31dp"
        android:text="Calculate"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/saleprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/textView1"
        android:text="     "
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:ignore="HardcodedText" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tagpricetext"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        tools:ignore="UselessLeaf" >

    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/calc"
        android:layout_below="@+id/calc"
        android:layout_marginTop="20dp"
        android:text="Sale Price: $"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:ignore="HardcodedText" />

    <EditText
        android:id="@+id/tagprice"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tagpricetext"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/tagpricetext"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/percentoff"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tagprice"
        android:layout_alignTop="@+id/textView1"
        android:ems="10"
        android:inputType="numberDecimal" />

</RelativeLayout>

And here is a screenshot: https://www.dropbox.com/sh/cq327dyhzzb1af6/PjRXqS2DWc/screenshot.jpg

Thanks in advance!

Brandon

Samsung Exhibit 2 4G (T-Mobile)

Upvotes: 1

Views: 446

Answers (1)

AC Arcana
AC Arcana

Reputation: 366

This article in the developer guide covers everything you need to know about supporting different screen sizes: http://developer.android.com/guide/practices/screens_support.html

If you're looking to center everything then just tag it all with center_horizontal...

As for your number, perhaps multiply it by 100, Math.round it, then divide it by 100?

Upvotes: 1

Related Questions