Set width to match constraints in ConstraintLayout - android

I would like to constrain a View's left and right sides to it's parent view's margins and make it fill the allotted space. However, setting width to either match_parent or wrap_content appears to produce the same result.
Is there something equivalent to match_constraints (as opposed to match_parent and wrap_content)? Do match_parent and wrap_content affect the layout or are they ignored in the new constraint layout?

match_parent is not allowed. But you can actually set width and height to 0dp and set either top and bottom or left and right constraints to "parent".
So for example if you want to have the match_parent constraint on the width of the element, you can do it like this:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

match_parent is not supported, so use android:layout_width="0dp". With 0dp, you can think of your constraints as 'scalable' rather than 'filling whats left'.
Also, 0dp can be defined by a position, where match_parent relies on it's parent for it's position (x,y and width, height)

Apparently match_parent is :
NOT OK for views directly under ConstraintLayout
OK for views nested inside of views that are directly under ConstraintLayout
So if you need your views to function as match_parent, then:
Direct children of ConstraintLayout should use 0dp
Nested elements (eg, grandchild to ConstraintLayout) can use match_parent
Example:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/phoneNumberInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>

match_parent is not supported by ConstraintLayout. Set width to 0dp to let it match constraints.

From the official doc:
Important: MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
So if you want achieve MATCH_PARENT effect, you can do this:
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

You can check your Adapter.
1 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
2 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);
I had a same problem like you when I used 1.
You can try 2.

For making your view as match_parent is not possible directly, but we can do it in a little different way, but don't forget to use Left and Right attribute with Start and End, coz if you use RTL support, it will be needed.
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

in the office doc:
https://developer.android.com/reference/android/support/constraint/ConstraintLayout
When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.
Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent"

set width or height(what ever u need to match parent ) to 0dp and set margins of left , right, top, bottom to act as match parent

If you want TextView in the center of parent..
Your main layout is Constraint Layout
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/logout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:gravity="center">
</androidx.appcompat.widget.AppCompatTextView>

Related

Setting an item to appear next to a recycler view keeps appearing on top [duplicate]

I would like to constrain a View's left and right sides to it's parent view's margins and make it fill the allotted space. However, setting width to either match_parent or wrap_content appears to produce the same result.
Is there something equivalent to match_constraints (as opposed to match_parent and wrap_content)? Do match_parent and wrap_content affect the layout or are they ignored in the new constraint layout?
match_parent is not allowed. But you can actually set width and height to 0dp and set either top and bottom or left and right constraints to "parent".
So for example if you want to have the match_parent constraint on the width of the element, you can do it like this:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
match_parent is not supported, so use android:layout_width="0dp". With 0dp, you can think of your constraints as 'scalable' rather than 'filling whats left'.
Also, 0dp can be defined by a position, where match_parent relies on it's parent for it's position (x,y and width, height)
Apparently match_parent is :
NOT OK for views directly under ConstraintLayout
OK for views nested inside of views that are directly under ConstraintLayout
So if you need your views to function as match_parent, then:
Direct children of ConstraintLayout should use 0dp
Nested elements (eg, grandchild to ConstraintLayout) can use match_parent
Example:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/phoneNumberInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
match_parent is not supported by ConstraintLayout. Set width to 0dp to let it match constraints.
From the official doc:
Important: MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
So if you want achieve MATCH_PARENT effect, you can do this:
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
You can check your Adapter.
1 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
2 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);
I had a same problem like you when I used 1.
You can try 2.
For making your view as match_parent is not possible directly, but we can do it in a little different way, but don't forget to use Left and Right attribute with Start and End, coz if you use RTL support, it will be needed.
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
in the office doc:
https://developer.android.com/reference/android/support/constraint/ConstraintLayout
When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.
Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent"
set width or height(what ever u need to match parent ) to 0dp and set margins of left , right, top, bottom to act as match parent
If you want TextView in the center of parent..
Your main layout is Constraint Layout
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/logout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:gravity="center">
</androidx.appcompat.widget.AppCompatTextView>

Is it necessary to use constraints FOR ALL FOUR sides of a view in constraint layout?

Sometimes if you use 3 constraints the view is placed perfectly. So, is it a good practice to use constraints for all four sides of a view.
It's only necessary to set explicit constraints when they can't be determined by the values in layout_width and layout_height.
For example, if a view's layout_width is set to match_parent, the view's start/end constraints are implicitly the parent's start/end.
If the layout_width or layout_height of a view is set to wrap_content, you will have to define at least one horizontal or vertical constraint, as needed.
In the following example, only one constraint has to be defined:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Defining a bottom constraint instead would also be sufficient -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintLayout, move a LinearLayout up (negatively)

I'm migrating some projects to AndroidX with Kotlin. I'm having some issues with ConstraintLayout, I already know that, according to the documentation:
Negative margins will not be supported in ConstraintLayout.
[...]
But I have the following situation:
I need to move up in 5dp the LinearLayout, however I need the height to continue to match the lower limit of the screen. That is, I move up 5dp and increment the height by 5dp.
As it's in the image, I've already tried translateY, but it just moves the entire view (not what I need). Also I can not create a view inside the id#top with height of 5dp and align with constraintTop_toTopOf, since they are part of different groups.
Is there any solution for this case?
It's tricky to get views to overlap in ConstraintLayout, but you can do it by adding an invisible view and constraining the overlapping view to the invisible view.
In this case the invisible view's bottom could be constrained to the bottom of the green LinearLayout, with a bottom margin of 5dp. The red LinearLayout can then have its top constrained to the bottom of the invisible view. This should give you 5dp of overlap.
Try copy-pasting the following into your constraint layout
<LinearLayout
android:id="#+id/green"
android:layout_width="0dp"
android:layout_height="100dp"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#android:color/holo_green_light" />
<View
android:id="#+id/dummyView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="#id/green"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<LinearLayout
android:id="#+id/red"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="#id/dummyView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#android:color/holo_red_light" />
Note that a dimension of "0dp" means "match constraints" when set on a child view of a ConstraintLayout. This is not obvious, but is in fact documented here https://developer.android.com/reference/android/support/constraint/ConstraintLayout
The whole purpose of ConstraintLayout is to have a flat view hierarchy. Therefore, having LinearLayouts nested in defeats the purpose.
I suggest you get rid of the nested LinearLayouts and do everything using constraints.

How to fix button at bottom despite of scroll in Linear Layout [duplicate]

I know we can set the following values to the android:gravity and android:layout_gravity properties:
center
center_vertical
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?
Their names should help you:
android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on.
android:layout_gravity sets the gravity of the View or Layout relative to its parent.
And an example is here.
Inside - Outside
gravity arranges the content inside the view.
layout_gravity arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.
Notes
The layout_gravity does not work for views in a RelativeLayout. Use it for views in a LinearLayout or FrameLayout. See my supplemental answer for more details.
The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity are meaningless together.
The view's width (or height) has to be less than the parent. Otherwise layout_gravity won't have any effect. Thus, match_parent and layout_gravity are meaningless together.
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.
This answer only dealt with setting gravity and layout_gravity on the views within a layout. To see what happens when you set the gravity of the of the parent layout itself, check out the supplemental answer that I referred to above. (Summary: gravity doesn't work well on a RelativeLayout but can be useful with a LinearLayout.)
So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
xml
Here is the xml for the above image for your reference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
Related
Difference between a View's Padding and Margin
Match_parent vs wrap_content
How to set both gravity and layout gravity of a LinearLayout programatically
The difference
android:layout_gravity is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.
android:gravity is the Inside gravity of that View. Specifies in which direction its contents should align.
HTML/CSS Equivalents
(if you are coming from a web development background)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
Easy trick to help you remember
Take layout-gravity as "Lay-outside-gravity".
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:
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.
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.
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.
Look at the image to be clear about gravity
If 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) with in its parent view then we will use "android:layout_gravity".
An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.
Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view
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.
There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).
Use Of Gravity and Layout-gravity in FrameLayout .....
Note:-
Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.
Layout-gravity is used with the parent View as some User have answer.
Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....
We can set Child View any where in the FrameLayout using layout-gravity .
We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .
FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).
have look on example ......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
Output:-
Use Of Gravity and Layout-gravity in LinearLayout .....
Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.
LinearLayout with orientation vertical ....
Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
Output:-
LinearLayout with orientation horizontal ....
Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
output:-
Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....
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.
The basic difference between the two is that-
android:gravity is used for child elements of the view.
android:layout_gravity is used for this element with respect to parent view.
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
Gravity: Allow you move the content inside a container. (How sub-views will be placed).
Important: (MOVE along X-axis or Y-axis within available space).
Example: Let's say if you were to work with LinearLayout (Height: match_parent, Width: match_parent) as root level element, then you will have full frame space available; and the child views says 2 TextViews (Height: wrap_content, Width: wrap_content) inside the LinearLayout can be moved around along x/y axis using corresponding values for gravity on parent.
Layout_Gravity: Allow you to override the parent gravity behavior ONLY along x-axis.
Important: (MOVE[override] along X-axis within available space).
Example: If you keep in mind the previous example, we know gravity enabled us to move along x/y axis, i.e; the place TextViews inside LinearLayout. Let's just say LinearLayout specifies gravity: center; meaning every TextView needs to be center both vertically and horizontally. Now if we want one of the TextView to go left/right, we can override the specified gravity behavior using layout_gravity on the TextView.
Bonus: if you dig deeper, you will find out that text within the TextView act as sub-view; so if you apply the gravity on TextView, the text inside the TextView will move around. (the entire concept apply here too)
Gravity is used to set text alignment in views but layout_gravity is use to set views it self. Lets take an example if you want to align text written in editText then use gravity and you want align this editText or any button or any view then use layout_gravity, So its very simple.
gravity: is used for simple views like textview, edittext etc.
layout_gravity: is used for current view only gravity in context of it's relative parent view like linear Layout or FrameLayout to make view in center or any other gravity of its parent.
The android:gravity sets the gravity (position) of the children whereas the android:layout_gravity sets the position of the view itself. Hope it helps
android:gravity
is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"
android:layout_gravity
is used for view itself relative to the parent or layout file.
android:gravity -> Sets the gravity of the content of the View its used on.
android:layout_gravity -> Sets the gravity of it's Parent's view or Layout
gravity--Applies to its own view.
layout-gravity---Applies to view related to its parent.
Gravity and layout_gravity both are the XML attributes.
The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget).
The android:layout_gravity is used to arrange the position of the entire View relative to it’s container.
For further details you can see this link.

Unable to center the text in TextView when width is set to 'match_parent'

I am relatively new to the Android SDK and am working with a TextView amongst other components on my activity UI. When I set the width to wrap_content and use layout_gravity, I am able to center the TextView in my parent container. However, I now need to give the TextView a background that stretches fully along the width of the parent and so I set the width to match_parent. Once I do this, neither layout_gravity nor textAlignment="center" are able to center the text within the view. This is the code I am using:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/trackName"
android:textColor="#ffffff"
android:layout_gravity="center"
android:textSize="28sp"
android:paddingTop="7dp"
android:paddingLeft="20dp"
android:background="#99000000" />
This is what the activity UI looks like right now:
How can I fix this and align my text to the center of the TextView control? Please help!
You should use android:gravity. As the docs say:
gravity Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
layout_gravity Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.
Use gravity instead of layout_gravity
Difference between them
android:gravity sets the gravity of the content of the View its used on and android:layout_gravity sets the gravity of the View or Layout in its parent.
Try this that should solve your problem.
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/trackName"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="28sp"
android:paddingTop="7dp"
android:paddingLeft="20dp"
android:background="#99000000" />
android:gravity="center_horizontal"
Difference between gravity and layout_gravity :
layout_gravity means the gravity of the widet in its layout. you have used width as match_parent. So your textview is already occupying all the space.
gravity means the gravity of the content of that view. i.e. gravity of the text in your textview. u have to place that text in the center, not the textview.

Categories

Resources