This question already has answers here:
What is the difference between gravity and layout_gravity in Android?
(21 answers)
Closed 9 years ago.
I'm working on a simple android application , and i need to align some of views in a specific location but without using padding or constants integer values for sizes , I wanna do that using gravity attribute , Can any one tell me what is the gravity attribute and what is the difference between android:gravity and android:layout_gravity attribute .
and thanks in advance .
android:gravity sets the gravity of the content of the View its used on.
android:layout_gravity sets the gravity of the View or Layout in its parent.
check https://stackoverflow.com/a/6819801/1434631
android:gravity sets the gravity of the content of the View its used on.
android:layout_gravity sets the gravity of the View or Layout in its parent.
Gravity: will specify to where the content of the view will be alighted, meaning If you have a TextView and it's parameters are not wrap_content for both dimensions then Gravity will determine which boarder this text will touch.
Layout_Gravitiy: this will specify the location of the View inside it's parent layout, meaning that if you Layout is bigger the the View inside this will determine which boarder of this layout this View will touch.
Related
I have a chain in a ConstraintLayout which consists in a TextView, an ImageView and another TextView (doesn't need autosizing). I'd like them three to be vertically centered in the parent container (i.e. same distance to left/right).
I want the TextView to autosize so the text doesn't overflow. I can't use autosize with a layout_width set to wrap_content (nothing happens and the text doesn't resize). I can't set a fixed layout_width either since the extra space when the text isn't very long throws off the alignment.
Is there anyway to do this via XML or should I implement my own autosizing behavior?
This is a picture of what I'm trying to achieve (a center-aligned chain):
For TextView Auto-sizing, use android:layout_width="0dp".
But make sure you apply the ViewComponents Left/Start and Right/End Constraints.
This trick is basically used instead of android:layout_width="fill_parent" in ConstraintLayout.
Using this, you'll achieve Auto-sizing and it will consume only the space falling under the specified constraints and no over-lapping and no spilling-out.
Does anybody know, why there is no android:gravity attribute for ImageView?
Why only android:layout_gravity is shown by Eclipse?
Does anybody know, why there is no android:gravity attribute for ImageView?
Because the developer of ImageView decided not to create one. Not every attribute is used for every widget. In this case, ImageView instead uses android:scaleType to control how the image fits in the space devoted to the ImageView.
android:gravity is used to set the gravity to the content inside the View, whereas android:layout_gravity sets the gravity of the View relative to its parent.
I think an ImageView does not need android:gravity because it should not have child content.
This question already has answers here:
What is the difference between gravity and layout_gravity in Android?
(21 answers)
Closed 9 years ago.
I'm some what new to android and I'm really confused very much about the difference between parameters android:layout_gravity and android:gravity. So can someone please help me on using these.
Thank you!
android:layout_gravity is the gravity related to your whole Layout like LinearLayout,Relative layout and android:gravity is the gravity related to your particular view like Button,EditText..etc
Hope this helps
android:gravity sets the gravity of the content of the View its used on.
android:layout_gravity sets the gravity of the View or Layout in its parent.
go through this example
android:gravity applies graivity to the content of the view, while
android:layout_gravity applies gravity to the view itself inside its parent.
For a button,layout_gravity will apply gravity to whole button whereas android:gravity will apply to its content that is text on Button etc.
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
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.