Haris
Haris

Reputation: 14053

Android button position changes while the screen rotating

  1. Hai all I am new in android programming.....

    For my application I am using image button and it successfully installed on the device. But when I rotate the screen all the button position get changed... and I need all the button placed at centre of the screen.... And here is my code....

    <TextView
        android:id="@+id/testText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFCC99"
        android:textSize="24dp" />
    
    <ImageButton
        android:id="@+id/up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="124dp"
        android:layout_marginTop="80dp"
        android:src="@drawable/up" />
    
    <ImageButton
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/up"
        android:layout_marginRight="200dp"
        android:layout_marginTop="150dp"
    
        android:src="@drawable/left" />
    
    <ImageButton
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/up"
        android:layout_marginRight="50dp"
        android:layout_marginTop="150dp"
        android:src="@drawable/right" />
    
    <ImageButton
        android:id="@+id/down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/up"
        android:layout_alignParentRight="true"
        android:layout_marginRight="124dp"
        android:layout_marginTop="80dp"
        android:src="@drawable/down" />
    

    Please help............

Upvotes: 0

Views: 444

Answers (3)

Lazy Ninja
Lazy Ninja

Reputation: 22537

Set android:gravity to center. The following layout will do for you:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cool Button" />

</LinearLayout>

There are other ways to do it.
1. Using relative layout
2. Using 2 layouts : 1 one for portrait and and 1 for landscape.

Upvotes: 1

Eldhose M Babu
Eldhose M Babu

Reputation: 14520

I've made some modifications to your code. Please check whether this is the desired one.

<?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:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:id="@+id/testText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:gravity="center_horizontal"
    android:text="Sample text"
    android:textColor="#FFCC99"
    android:textSize="24dp" />

<ImageButton
    android:id="@+id/up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/up" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageButton
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="left"
        android:src="@drawable/left" />

    <ImageButton
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:src="@drawable/right" />
</RelativeLayout>

<ImageButton
    android:id="@+id/down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/down" />

</LinearLayout>

Upvotes: 0

Aerilys
Aerilys

Reputation: 1639

You have to put all your widgets in a single container (LinearLayout) and center it. For that you can use the gravity attribute.

You can check this page: http://developer.android.com/guide/topics/ui/layout/linear.html

Upvotes: 0

Related Questions