Question

I was testing out B4A (Basic4Android) and there's something called a "designer script", this basically allows you to position elements on a screen. For example:
TextBox1.setTopAndBottom(0%y, 50%y)
TextBox1.setLeftAndRight(0%x, 50%x)

When this script runs it will automatically position the TextBox1 on the screen, imagine the the screen is 100 pixels by 100 pixels (100x100), the TextBox1 will be placed at (0, 0) (top left), (50, 50) (bottom right).

How can I achieve something like this in Eclipse?
I can't figure it out. I want to position an element (TextBox1 for example) to fit 25% of the width and 50% of the height for example. How do I achieve this?

Was it helpful?

Solution

OK. This is the trick

In simple words: make a 1px (px, not dp - you don't want it scaled, but small enough to be trascurable!) TextView which will be your invisible (you leave it transparent and set no text in it) "center of the universe".
Then stretch your other TextView, but limit it to stay to the left (which is at 50% - 1/2 px) of the center and above (again, 50% - 1/2 px) it:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/centerPoint"
        android:layout_width="1px"
        android:layout_height="1px"
        android:layout_centerInParent="true"
    />
    <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/centerPoint"
        android:layout_above="@id/centerPoint"
    />
</RelativeLayout>

OTHER TIPS

Consider using the Android percent support library, which adds a PercentRelativeLayout and PercentFrameLayout that allow children to define their sizing in percentages.

e.g.

 <android.support.percent.PercentFrameLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout/>

You need to use LinearLayout and weights.

http://developer.android.com/guide/topics/ui/layout/linear.html

Example of 6 TextViews, each row will take up 50% of the screen height, and within each row each TextView will take up 33% of the screen width.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top