Why android:gravity attribute for ImageView is not there? - android

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.

Related

Autosizing TextView in ConstraintLayout

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.

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.

Which Layout supports android:layout_gravity?

I came through many examples in internet.I found that neither Relative Layout nor Linear Layout supports android:layout_gravity.By it I mean the views inside these layouts does not support android:layout_gravity attribute. So any one having idea which layout supports android:layout_gravity and how to use it in better way?
Children (that is, direct descendants in the View hierarchy) of LinearLayout do use layout_gravity (see LinearLayout.LayoutParams), but only on the "secondary" axis. So, in a vertical LinearLayout, center_horiztonal will work, but center_vertical will do nothing.
Children of FrameLayout also support layout_gravity (see FrameLayout.LayoutParams).
Keep in mind that the layout_* parameters set values in a LayoutParams object provided by the view parent. So a layout_* parameter will only have an effect if the parent view supports the parameter.
Actually if you use RelativeLayout you don't need to use layout_gravity.Better way to position your layout's elements are android. Here you can get a good explanation how to use RelativeLayout.

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.

Hierachy of android:layout_width/ height between containers and views

What is the relationship between android:layout_width & android:layout_height in the container and the views contained within?
For instance if I have a LinearLayout with the layout width and height set to fill_parent, and I have a Button with those values set to wrap_content it uses the value from the Button, like the Button values overide the LinearLayout.
However if i swap them around so that the LinearLayout values are both wrap_content and the Button values are now fill_parent it still uses wrap_content, this time not overriding the values, but using the values supplied with the LinearLayout.
Could someone explain how they relate as this is very confusing to me?
Thanks
Kyros
http://developer.android.com/guide/topics/ui/how-android-draws.html
This article explains it very well

Categories

Resources