Difference between android:layout_gravity and android:gravity [duplicate] - android

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.

Related

Why android:gravity attribute for ImageView is not there?

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.

What is the difference between android:gravity and android:layout_gravity [duplicate]

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.

Strike view with line [duplicate]

This question already has answers here:
creating a strikethrough text?
(14 answers)
Closed 9 years ago.
I wanna achieve an horizontal line that strikes a whole view inside a ListView, so I give the effect of striking the whole element in the listview.
How can I achieve this? I have found how to make a line below the view, but not over it.
This will create a strike out line on your view
<View
android:id="#+id/View_Strike"
android:layout_width="match_parent"
android:layout_below="#id/Layout_myRow"
android:layout_height="1dp"
android:background="#android:color/white" />
Now where you are designing a row layout for your listview. place this above view in such a way that it overlaps you Text View at the desired position in you Row Layout
and set its visibility Gone
Now depending on the Situation when you have to strike thru Your Item make its Visibility Visible
Surely is the solution !! i have used it in one of my app
Don't think there is a standard fast way of doing such a thing, but you can always create a view of fill_parent width and 1dp height and strike the whole view centering it relative to its parent.
make your custome view like stike image and make visible invisible as per your requirment over the current view
I have found the answer, if someone founds this useful:
Create a FrameLayout, inside of it I put the LinearLayout (with my TextViews) inside of it, and then a View with height=1dp width=fill_parent, and gravity of center.
The FrameLayout actually was created to make multiple layer views so it was the perfect thing.
Also for the details, in the listAdapter I make the View visible or gone.

Button not centering horizontally

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

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