Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:51:11+00:00 2026-06-10T11:51:11+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:51:12+00:00Added an answer on June 10, 2026 at 11:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple Activity with the following layout... <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent
I have the following layout in XML (splashscreen.xml): <?xml version=1.0 encoding=utf-8?> <FrameLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/frmLayout
I have an Android Activity with a RelativeLayout and I have implemented the following
I have the following layout: <Button android:id=@+id/MyButton android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/Hello android:clickable=true android:onClick=Foo /> And
I have the following RelativeLayout <RelativeLayout android:layout_width=match_parent android:layout_height=match_parent android:layout_marginLeft=2dp android:layout_marginRight=5dp android:layout_marginBottom=5dp android:layout_marginTop=5dp> <TextView android:id=@+id/x_memtxtlay
I have an activity with the following layout. So in this screen, initally the
In my android application, I have following requirement. Activity A --> Activity B(Go to
The scenario is the following: I have an activity RunTrainingWorkoutsView that uses XML layout
In my application i have an activity which has the following layout <?xml version=1.0
I have the following activity... package org.dewsworld; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.