What is the difference between gravity and layout_gravity in Android?
Their names should help you:
android:gravity
sets the gravity of the contents (i.e. its subviews) of theView
it's used on.android:layout_gravity
sets the gravity of theView
orLayout
relative to its parent.
And an example is here.
Difference between android:layout_gravity and android:gravity
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
What is exact difference between gravity and layout_gravity
- Yes, your understanding is correct. Any
layout_
attribute gives instructions to the parent view. - Generally, if a view is taking up the full width/height of its' parent, then you won't need to specify
layout_gravity
, you'll probably findgravity
more useful. If your view does not take up an entire dimension of its' parent, you might want to align it depending on your desired look.
What is the difference between android:gravity and android:layout_gravity
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
LinearLayout android:layout_gravity=center_vertical works at design time but doesn't at run time
In your LinearLayout, change layout_gravity
to just gravity
The difference between vertical and horizontal orientation about layout_gravity and gravity
The main thing to understand is that layout_gravity
on a child view is an attribute that communicates "up" to the parent, and "asks" the parent to behave in a certain way.
In this case, the parent is a LinearLayout
, which has a different attribute (orientation
) that already affects the positioning of child views. Therefore, when a child specifies layout_gravity
, what happens depends on the LinearLayout
's orientation
.
- For a
horizontal
LinearLayout, any horizontal component of the child'slayout_gravity
is ignored. - For a
vertical
LinearLayout, any vertical component of the child'slayout_gravity
is ignored.
You're using center
, which is basically center_vertical
+ center_horizontal
. So for a horizontal LinearLayout, the horizontal centering will be ignored. And, for a vertical LinearLayout, the vertical centering will be ignored.
Given that your example only has one child view, you could consider replacing the parent LinearLayout
with a FrameLayout
. Then the child layout_gravity
will be fully respected.
What is the difference between gravity=center and textAlignment=center and centralHorizontal=true
For practicality purposes, textAlignment and gravity are equivalent, except that textAlignment is a member of the View Class and the gravity is a member of TextView class. layout_centerHorizontal defines how a view should be placed in relation to it's parent view, while the former two deal with how the view should lay out it's subviews.
Related Topics
How to Send an Object from One Android Activity to Another Using Intents
Android Listview With Different Layouts For Each Row
Make a Link in the Android Browser Start Up My App
How to Filter Specific Apps For Action_Send Intent (And Set a Different Text For Each App)
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget - Android
Ioexception: Read Failed, Socket Might Closed - Bluetooth on Android 4.3
How to Completely Uninstall Android Studio from Windows(V10)
How to Load Script from Assets Index.Android.Bundle on Windows
How to Create a Custom-Shaped Bitmap Marker With Android Map API V2
Can an Android Toast Be Longer Than Toast.Length_Long
How to Check Visibility of Software Keyboard in Android
Sending and Receiving Sms and Mms in Android (Pre Kit Kat Android 4.4)
How to Set the Part of the Text View Is Clickable
Comparison of Android Networking Libraries: Okhttp, Retrofit, and Volley
How to Use Recyclerview Inside Nestedscrollview