Home » Android » android – RelativeLayout Gravity not applied?

android – RelativeLayout Gravity not applied?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an activity with the following layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/testlayoutMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/testlayout_bottom"
        android:layout_width="fill_parent"
        android:layout_height="62dp"
        android:background="#122334" >

        <ImageView
            android:id="@+id/testbtnBlock"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left|center_vertical"
            android:contentDescription="Test1"
            android:padding="@dimen/padding_medium"
            android:src="@drawable/btnblock" />

        <TextView
            android:id="@+id/testtxtZoomPan"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/testbtnX"
            android:layout_toRightOf="@+id/testbtnBlock"
            android:gravity="center_horizontal"
            android:text="@string/txtZoomPan"
            android:textColor="#FFFFFF" />

        <ImageView
            android:id="@+id/testbtnX"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="right|center_vertical"
            android:contentDescription="Test2"
            android:padding="@dimen/padding_medium"
            android:src="@drawable/btnx" />
    </RelativeLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/testlayoutPuzzleInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/testlayoutChronoErrors"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Chronometer
                android:id="@+id/testchronometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:format="@string/chronometer_initial_format"
                android:gravity="center" />

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/testlayoutErrors"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="1px"
                    android:layout_height="20dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

and the following code :

package minmaxdev.android.picrossanywhere;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    // Capture our button from layout
    ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
            rv.setGravity(Gravity.CENTER);

            Button btnRetry = new Button(TestActivity.this);
            btnRetry.setId(R.id.btnRetry);
            btnRetry.setBackgroundResource(R.drawable.btnselector);
            RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
            prmBtn.setMargins(0, 120, 0, 0);
            btnRetry.setLayoutParams(prmBtn);
            // btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
            btnRetry.setText("Retry");
            btnRetry.setOnClickListener(new ImageView.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            });
            rv.addView(btnRetry);
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test, menu);
    return true;
}
}

I would like to know :
Why is my dynamically created Button (named btnRetry) not appearing in the center of the screen, since I set the parent RelativeLayout gravity to center with rv.setGravity(Gravity.CENTER) ?

Thank you very much for your time

How to&Answers:

The answer is easy. Gravity works for the content, layout_gravity for the view that uses that.
Source basically: https://stackoverflow.com/a/3482757/180538

Try to use LayoutParams with addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

To answer your gravity understanding: You are right but I guess that this information in the documentation is important:

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

I can’t test your layout at the moment but my guess is that the time where gravity is applied doesn’t create the expected result.

Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.