Button not centering horizontally - android

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:

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

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

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

Related

Centering a Linear Layout Horizontal and Vertical

Im running into a speed bump in my android App. I want to center my Linear Layout in the center of the screen (Horisontally only) and I want to center another element only vertically. I haven't seen an easy apparent way to do this in the program.
http://i.stack.imgur.com/gfMBD.png
I want the grey Box to be centered horisontally in the app
I'm not sure, how your layouts are now, but I'd do something like this:
Vertical Linearlayout
RelativeLayout, centered horizontally
RelativeLayout, set to match parent
RelativeLayout, centered vertically
Use RelativeLayout as a root ViewGroup, and then just add android:layout_centerHorizontal="true" to your first LinearLayout, and android:layout_centerVertical="true" to your second one.

Android XML: Good examples to use gravity rather than layout_gravity

I have done some basic xml layout from the Internet and I am confused as to what android:gravity would be used for vs android:layout-gravity. The UI designer seems to respond well when I change the layout_gravity, but I get no response to the regular gravity? What is up with this?
Is this similar to layout parameters with fill-parent and match-parent? one is gone now.
android:gravity is a statement from a parent to its content, indicating where the content should slide within the available parent space (if there is room for such sliding).
android:layout_gravity is a request from a child to its parent, asking that it be slid in a certain direction (if there is room for such sliding).
layout_gravity - It contains and it belongs to component's parent layout. Like if you set one TextView in LinearLayout, LinearLayout's width is fill_parent, but TextView basically will set at left-top. So now if you would give layout_gravity=right then your TextView will be shifted to top-right corner. But the text will not change its place. i.e. you had been wrote "Hello world" then "hello world" will not be changed to right, but its parent textView will be changed to right. So here in this case it will belongs to LinearLayout, means parent Layout.
gravity- It contains and it belongs to TextView as per above explanation. if you will give gravity="right" then "Hello-world" text will go to right-top corner of TextView not LinearLayout.

Android top left button

I want to make an about button in the top left. I tried:
android:layout_gravity:"top|left"
but it doesn't work , I searched and all what I found was using RelativeLayout and if I use that I'll have to make all my layout from beginning and it's not that good like the linear layout.
Couldn't post the code here. So this is my code on pastebin
http://pastebin.com/5EjgyB0K
Here you have given android:layout_gravity="center" to the Linear Layout so it is going to set gravity of the layout and as center and your About Button is child of layout its to going to set in center and you have given Margin_top also.Try to remove gravity amd Margin_top and you can see the result, the button will be top|left of the screen.

Alignment issue for TextView

I am displaying the TextView within a RelativeLayout.
I want to put the TextView at the center of the Layout.
Within the layout xml,
for TextView am using the property android:gravity="center" is not working .
I do not want to hard code by using android:layout_marginLeft or Right since it can give different effect in portrait or landscape mode.
Is there property so that it looks the same for both landscape & portrait mode & the TextView occupies the center of the Layout
Since RelativeLayout is your TextView's parent, you simply set android:layout_centerInParent="true" on your TextView. No other properties are needed.

Center Alignment in Android

I want to know how to center a GUI widget programmatically.
Kindly help me. I am using a LinearLayout.
Many Regards.
You need to set gravity for a view as CENTER_HORIZONTAL
with the markup you should use:
android:layout_gravity="center_vertical|center_horizontal"
The difference between android:gravity and android:layout_gravity is that android:gravity positions the contents of that view (i.e. what’s inside the view), whereas android:layout_gravity positions the view with respect to its parent
In the code you should use:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
Most of the time, you don't have to do this if you defined android:gravity="center" in the layout file.
You can also perform this by getting the screen size then doing some calculations on it, but this is not recommended.

Categories

Resources