2

Suppose I have a background in my Activity's root layout. I wish to place some buttons on the screen which always stay in the same place relative to the screen. How can I accomplish that?

I tried using dp and sp but if I test the layout in another screen it all falls apart. (So if it is OK in WVGA it is not working in QVGA. Am I doing something wrong?

This is my layout xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/menu_activity_bg" >
        <Button
            android:id="@+id/continueButton"
            android:layout_width="110sp"
            android:layout_height="30sp"
            android:layout_marginLeft="100sp"
            android:layout_marginTop="180sp"
            android:background="@drawable/menu_activity_continue" />
        <Button
            android:id="@+id/newGameButton"
            android:layout_width="130sp"
            android:layout_height="35dp"
            android:layout_marginLeft="90sp"
            android:layout_marginTop="220sp"
            android:background="@drawable/menu_activity_new_game" />
            <!-- ... -->
    </RelativeLayout>
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Adam Arold
  • 29,285
  • 22
  • 112
  • 207

2 Answers2

1

My solution to this is to have a number of style types, which I place in the values-large, values-small, values-normal, and values-xlarge folders. A typical entry will look like this:

<resources>
<style name="bigText">
    <item name="android:textSize">60dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_marginTop">5dp</item>
    <item name="android:layout_marginBottom">5dp</item>
    <item name="android:textColor">#000000</item>
</style>
</resources>

Then my layout XMLs would look like:

<TextView
    android:id="@+id/somTextValue"
    style="@style/bigText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/someText" />

Having a number of these styles, I apply them to my objects that need to scale. Trial and error will allow me to find something that works well for all screen sizes.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
0

I have one another 2 approaches:

  1. Use layout weight to your view so that it will take space accordingly

  2. Use of DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int wwidth = metrics.widthPixels; This will give you width & height of screen then while creating give measurement in percentage e.g. 20% of width. It is helpful when you are dynamically creating UI.

R World
  • 766
  • 9
  • 26
  • I have a static UI I just wish to scale based on percentages. – Adam Arold Dec 03 '12 at 12:08
  • if all above are not working for you & app having few layouts then create separate UI layout & put on appro. folders. this is also simple way Ref[link](http://developer.android.com/guide/practices/screens_support.html#terms) – R World Dec 03 '12 at 12:15