Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 15, 2015

Gravity and layout_gravity on Android

Gravity and layout_gravity on Android


I know we can set the following value to the android:gravity and android:layout_gravity.

  1. center
  2. center_vertical
  3. center_horizontal, etc.

But I am confused regarding both of these.

What is the difference between the usage of android:gravity and android:layout_gravity?

Answer by Sephy for Gravity and layout_gravity on Android


Their names should help you:

  • 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.

And an example is here.

Answer by matto1990 for Gravity and layout_gravity on Android


From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.

I think this is right but the best way to find out is to play around.

Answer by Juan Pickselov for Gravity and layout_gravity on Android


Though the question is already answered I have some samples demonstrating the use of gravity, layout_gravity, and layout_weight.

You can find the examples at http://juanpickselov.com/LayoutExamples.zip

I created the files in Eclipse, removed the .svn subfolders and have included styles, strings, colors, etc. The layout files are the main point of the demos. Since I'm a Java and Android development Newbie, one may find the Java inefficient. The files can be copied into an Eclipse Project or I've also used them in Netbeans with the Android development plugin available for that IDE.

Answer by boiledwater for Gravity and layout_gravity on Android


The below examples show the difference between layout:gravity and gravity, and they may be help you.

http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity.html

Answer by Vishnu Haridas for Gravity and layout_gravity on Android


android:layout_gravity is the Outside gravity of the View. That means, to specify the direction in which the View should touch it's parent's border.

android:gravity is the Inside gravity of that View. This means, in which direction it's contents should align.

HTML/CSS Equivalents:

android:layout_gravity = float in CSS

android:gravity = text-align in CSS

Easy trick to remember: Take "layout-gravity" as "Lay-outside-gravity"

Answer by Ying for Gravity and layout_gravity on Android


Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.

Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:

1) To control the gravity of ALL child views of a LinearLayout container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.

2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:

import android.widget.LinearLayout.LayoutParams;  import android.view.Gravity;  ...    Button button = (Button) findViewById(R.id.MyButtonId);  // need to cast to LinearLayout.LayoutParams to access the gravity field  LayoutParams params = (LayoutParams)button.getLayoutParams();   params.gravity = Gravity.BOTTOM;  button.setLayoutParams(params);  

For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravity value is -1 but Android put it center vertically.

To change the horizontal positions of child views in a horizontal linear container, one can use layout_weight, margin and padding of the child view.

Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or center_horizontal) and can be changed to left or right.

Actually, a child view such as a button also has android:gravity XML attribute and the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.

Answer by Abhishek Tamta for Gravity and layout_gravity on Android


If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".

Answer by JustSomeGuy for Gravity and layout_gravity on Android


Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.

If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.

I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.

Answer by Suragch for Gravity and layout_gravity on Android


The other explanations here are good, but sometimes it helps to have a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.

enter image description here

Note:

Don't use gravity/layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.

If I hadn't made the width and height of the TextViews larger than the text, then setting the gravity would have had no effect. So if you're using wrap_content on the TextView then gravity won't do anything. In the same way, if the LinearLayout had been set to wrap_content, then the layout_gravity would have had no effect on the TextViews.

The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=center only centers horizontally.

So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view. Its easy to forget which is which. Think of this little analogy to help you remember: In daily life, "gravity" is a more common term than "layout gravity". And in daily life, the earth's gravity is a more common experience than any other kind of gravity. So think of the earth like a TextView and you are the text. Now, just like it is hard to remember what "layout gravity" is, the gravity we don't think about much in daily life is the gravity of the sun pulling on the earth. So the sun is the earth's (TextView's) parent ViewGroup (a LinearLayout or whatever) and the earth's layout gravity is its orbit in the solar system.

Here is the xml for the above image for your reference:

                                                                                                                                                              

Related:

Answer by Akshay for Gravity and layout_gravity on Android


Look at the image to be clear about gravity

Answer by Matt Szaro for Gravity and layout_gravity on Android


Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.

Answer by IntelliJ Amiya for Gravity and layout_gravity on Android


android:gravity is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view.

android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.

For more details you can visit

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.