1

I just started Android development and I'm having trouble figuring out why my text alignment inside my button isn't horizontally centered. It is vertically centered.

I've tried gravity, some padding, center, and text alignment center with no luck. I am using a RelativeLayout with an EditText and a TextView above. When I take the other views out of the activity, there is also no change.

I can get it to center with gravity center_vertical and some padding left, but this method seems inappropriate.

Here is a view of the button before update:

Update: Tested on my ADV for android:gravity="center_vertical|center_horizontal" and it ended up working fine. Seems to be a bug with the Graphic Layout view in Eclipse that is mis-aligning the inner context of the Button view. Here's it working on my AVD:

enter image description here

Ben Sewards
  • 2,571
  • 2
  • 25
  • 43
  • @Harshid updated. Notice how I'm using paddingLeft to center now. This is bad for various reasons. I'll try testing on AVD to see if it persists when using gravity=center. Who knows, could work on AVD. – Ben Sewards Mar 14 '13 at 05:18

3 Answers3

4

Set the gravity of the button as android:gravity = "center" or android:gravity = "center_vertical|center_horizontal" .

bostan
  • 373
  • 2
  • 8
3

try layout_gravity="center" or gravity="center" instead. I always forget which one : P

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • layout_gravity is for the outside of the view while gravity if for the inside. Thanks for introducing me to layout_gravity, but I suggested about that gravity wasn't working on the context of my view button. I ran into more posts where the text starts on the half mark (same as mine), but works normally in the AVD. Possible bug fyi. – Ben Sewards Mar 14 '13 at 02:44
  • I allways get them mixed so I try them both to see who does the trick, glad you figured your problem out. have a nice coding. – Emil Adz Mar 14 '13 at 07:59
0

android:gravity = "center" will align the text inside the button both horizontally and vertically.

android:layout_gravity is different from android_gravity. Refer for a detailed answer: Gravity and layout_gravity on Android

Community
  • 1
  • 1
newbie
  • 1,049
  • 5
  • 15
  • 29