Android XML: Good examples to use gravity rather than layout_gravity - android

I have done some basic xml layout from the Internet and I am confused as to what android:gravity would be used for vs android:layout-gravity. The UI designer seems to respond well when I change the layout_gravity, but I get no response to the regular gravity? What is up with this?
Is this similar to layout parameters with fill-parent and match-parent? one is gone now.

android:gravity is a statement from a parent to its content, indicating where the content should slide within the available parent space (if there is room for such sliding).
android:layout_gravity is a request from a child to its parent, asking that it be slid in a certain direction (if there is room for such sliding).

layout_gravity - It contains and it belongs to component's parent layout. Like if you set one TextView in LinearLayout, LinearLayout's width is fill_parent, but TextView basically will set at left-top. So now if you would give layout_gravity=right then your TextView will be shifted to top-right corner. But the text will not change its place. i.e. you had been wrote "Hello world" then "hello world" will not be changed to right, but its parent textView will be changed to right. So here in this case it will belongs to LinearLayout, means parent Layout.
gravity- It contains and it belongs to TextView as per above explanation. if you will give gravity="right" then "Hello-world" text will go to right-top corner of TextView not LinearLayout.

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.

Bottom margin Android LinearLayout item in XML ignored, but works in Java code

I am developing an app with an activity with member reactions on a hike event. The reactions are the yellow "balloons" which are made using a LinearLayout. Each item is constructed from a XML file (listitem_deelnemerreactie.xml) which defines the layout for a reaction item. The top level of this layout file is a LinearLayout my itself.
I want some spacing between the separate elements, as well as some right margin. The most straightforward way to so this should be: setting a bottom and right margin on the top-level LinearLAyout element of the listitem_deelnemerreactie.xml layout file.
But setting the bottom margin on the LinearLayout has no effect on the vertical spacing, though the right margin does have an effect.
The only way to be able to set a vertical margin appears to be: setting is in the Java code, after attaching the inflated view to the container.
See the two images for the effect and the code.
Though setting the margins in the code is a working workaround, I still think it is strange this cannot be achieved in the XML. Why is the bottom margin attribute ignored while the right margin is not?
Any ideas?
Have you tried to set an android:padding="10dp" for example on your elements to spaced them ?

Android top left button

I want to make an about button in the top left. I tried:
android:layout_gravity:"top|left"
but it doesn't work , I searched and all what I found was using RelativeLayout and if I use that I'll have to make all my layout from beginning and it's not that good like the linear layout.
Couldn't post the code here. So this is my code on pastebin
http://pastebin.com/5EjgyB0K
Here you have given android:layout_gravity="center" to the Linear Layout so it is going to set gravity of the layout and as center and your About Button is child of layout its to going to set in center and you have given Margin_top also.Try to remove gravity amd Margin_top and you can see the result, the button will be top|left of the screen.

Imageview floating left

How can I create ImageView floating to the left of the textView, something like html:
<div>
<img src="src" style="float:left"> Text here.....
</div>
You have two options:
With LinearLayout, setting the orientation to horizontal so the image is first and then come the rest.
With RelativeLayout, there you can indicate the position of a element relative to another with the attributes of the style android:layout_toLeftOft, android:layout_toRightOf, android:layout_below or android:layout_alignParentTop, ...
However, it is not so flexible as CSS for some actions and, for example, wrapping text around an image is not so easy to achieve.
There's really no concept of floating elements in Android, but you can easily put an image to the left of some text using the drawableLeft attribute of the TextView. Example:
Otherwise, for more complicated layouts, parent views determine how their children are laid out. For example, instead of the concept of a div which simply wraps it children and uses the float and display attributes of the children to determine how things look, Android has more complex parent views (ViewGroups as they are called, since that's the super class) to control things.
Check out the docs for LinearLayout and RelativeLayout for some examples.
The positioning of views depend on the kind of layout you use. In case if you are using a RelativeLayout and you want to float your view(in your case the ImageView) which is within this Relativelayout, you can use the attribute of the ImageView (android:layout_toLeftOf="") specifying the view id of your textview between the double quote.

Android: My text is being displayed vertically in a programmatically set TextView

I'm adding a TextView to a parent LinearLayout RelativeLayout programmtically. There is enough space for the text to be displayed horizontally but for some reason the text is displayed vertically. Does anyone know what's going on here?
It's really hard to say what is the problem without looking at your XML and layout code. In my experience, sometimes it happens when the parent layout has height set as WRAP_CONTENT and the views inside it have wrong weight config, for example one has weight but WRAP_CONTENT and the other MATCH_PARENT (Sorry, I don't remember the case exactly). I suggest you check your LayoutParams carefully or set a fixed width and height for the TextView to see what is the problem.
You can also post your code here so we can have a look at it

Categories

Resources