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

  • Home
  • SEARCH
  • 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 7067851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:15:05+00:00 2026-05-28T05:15:05+00:00

I’m having some trouble getting my button text to align correctly in my Android

  • 0

I’m having some trouble getting my button text to align correctly in my Android application. Currently, my button text is aligning with the left of the text on the center of the button, as shown:

|Whitespace|Text Here|

I have tried using gravity =”center_horizontal|center_vertical” to no avail. I have tried adding a new button to a completely separate project and this still holds. Also it does this in LinearLayouts and RelativeLayouts.

Thanks in advance for your help.

Here is one of the layout files. However it is doing it in all of them including in other projects. Also, all 3 of the buttons on this layout have the same issue.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    android:layout_width="wrap_content"
    android:text="@string/history_delete_record"
    android:id="@+id/History_Delete_Record"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal|center_vertical"></Button>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/History_Delete_Record"
    android:layout_alignLeft="@+id/History_Delete_Record"
    android:id="@+id/History_Open_Details"
    android:text="@string/history_open_record"></Button>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/History_Cancel"
    android:text="@string/cancel"
    android:layout_below="@+id/History_Open_Details"
    android:layout_alignLeft="@+id/History_Open_Details"
    android:layout_marginTop="19dp">
</Button>
</RelativeLayout>
  • 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-05-28T05:15:06+00:00Added an answer on May 28, 2026 at 5:15 am

    Here is a simple implementation of a button with centered text:

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myBtn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:text="Button text!" />
    

    The problem with your code is that you are setting the width in pixels, when you should be using the xml attribute android:layout_width="wrap_content", which will wrap the button text to fit the view accordingly.

    Also note that you should never set a view’s width in pixels as this will not work well across different screen sizes and densities. Instead of using “px”, specify the width in “dp”, a density-independent unit of value. For more information on this topic, see this post.

    EDIT
    Here is my suggested xml code that you use for your layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/History_Delete_Record"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/history_delete_record" >
        </Button>
    
        <Button
            android:id="@+id/History_Open_Details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/History_Delete_Record"
            android:layout_alignRight="@+id/History_Delete_Record"
            android:layout_below="@+id/History_Delete_Record"
            android:text="@string/history_open_record" >
        </Button>
    
        <Button
            android:id="@+id/History_Cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/History_Open_Details"
            android:layout_alignRight="@+id/History_Open_Details"
            android:layout_below="@+id/History_Open_Details"
            android:text="@string/cancel" >
        </Button>
    
    </RelativeLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

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.