android:layout_below not properly working - android

I have a RelativeLayout with three views. Both TextViews should be to right of the ImageView and the seconds one should be below the first TextView.
Code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bdbdbd"
android:paddingBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerVertical="true"
android:src="#drawable/default_avatar" />
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/avatar"
android:text="Chris"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/university"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/username"
android:layout_toRightOf="#id/avatar"
android:text="Oxford"
android:textSize="13sp" />
</RelativeLayout>
Result:
After removing android:layout_centerVertical="true" from the first TextView, everything works as expected (except that it's not vertical).
Why is this and how can I make the first TextView vertical centered?

For some reason the RelativeLayout's height param is giving the problem. Try setting it to 94dp (64 of the image + 15 of bottom padding + 15 of top padding). This should solve the problem

#f. de is right. The problem is android:layout_height="wrap_content", as the height of relative layout is determined my it's content setting it's content to vertically center based on it's height wont work. You need to set this to match_parent or to a fixed value.

You also could wrap your data, in your case Name and University inside another RelativeLayout or any other ViewGroup and make it to align center.
Like that
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bdbdbd"
android:paddingBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="15dp">
<ImageView
android:id="#+id/avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerVertical="true"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="#id/avatar"
android:layout_toRightOf="#id/avatar">
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chris"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/university"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/username"
android:text="Oxford"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
This makes sense in order to group related views (TextViews) and also it produces better result because whole container is aligned center, in your initial example Name view is centered vertically but other view is below it and it makes it to look not as good as it will in case of container where baseline is vetical center of container.

Since trying to place both TextViews center vertical, they overlap. Use the padding attribute for the advantage.
So to align them as required, use this code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bdbdbd"
android:paddingBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerVertical="true"
android:src="#drawable/default_avatar" />
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/avatar"
android:text="Chris"
android:paddingBottom="7dp"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/university"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/username"
android:layout_toRightOf="#id/avatar"
android:paddingTop="7dp"
android:text="Oxford"
android:textSize="13sp" />
</RelativeLayout>
I wish this solves your problem...

The problem is with your RelativeLayout's height attribute,
if you change it to "match_parent" then it will work the way you wanted.
Or
you can make another Relativelayout in your main RelativeLayout who's height is "match_parent". After doing so you can place everything at any place without any problem.

Related

GridView fill column widths

Consider the following:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingEnd="15dp">
<TextView
android:id="#+id/tv_Progress_Level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/White"
android:layout_row="0"
android:layout_column="0"
android:text="#string/Progress"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/pb_Assignment_Progress"
style="#android:style/Widget.Holo.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:paddingStart="10dp"
android:paddingTop="3dp"/>
</GridLayout>
In the above GridLayout, my ProgressBar has been set to 50% for the sake of width testing.
However, with the above setup, this is the result:
The bar is clearly NOT on the 50% mark.
Yes this is because I've used fill parent that actually takes up the space from the start of grid till the end of it.
I have tried using wrap_content on the bar, but it seems a lot shorter:
Is there a way I can fill up the remaining space of the grid?
Isn't fill_parent suppose to mean to fill up the remaining space? Guess I was wrong. It is actually a deprecated version of match_parent
I don't want the bar to possess the width of the grid, instead I want it to just fill up the remaining space after the TextView
Solved it:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingEnd="15dp"
android:paddingStart="15dp"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_Progress_Level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress"
android:textColor="#color/white"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/pb_Assignment_Progress"
style="#android:style/Widget.Holo.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</GridLayout>
Happy coding!!
As simple as changing the following:
android:layout_width="fill_parent"
TO
android:layout_width="0dp"
and add the following:
android:layout_gravity="fill"

Why doesn't gravity work when applied to Relative Layout?

I already understand that if I use layout_gravity="center_horizontal" on the text view, then the text will be center. But I don't want to use that because I want to have as little code as possible and the best way to do that is by applying gravity="center_horizontal" to my relative layout. I am also asking this question because I am concerned about even using gravity or layout_gravity with relative layouts at all. As when doing my research I came upon this answer.
Notice the part that says:
Don't use gravity/layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.
Even though it seems pretty apparent to me from the relative layout Android Documentation, which clearly lists gravity as a valid attribute, that Google intended these attributes to be used with relative layouts.
If that quote is correct, how do I center views in relative layouts?
Also, here is my code:
Notice the title is not centered horizontally
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:gravity="center_horizontal"
tools:context="com.something">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp" />
<AutoCompleteTextView
android:id="#+id/enterContact"
android:text="Enter Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="#+id/title"
android:layout_marginBottom="#dimen/main_spacing" />
</RelativeLayout>
This is a supplemental answer to Gravity and layout_gravity on Android.
View gravity and layout_gravity inside a Relative Layout
My original statement that gravity and layout_gravity should not be used in the subviews of a RelativeLayout was partly wrong. The gravity actually works fine. However, layout_gravity has no effect. This can be seen in the example below. The light green and light blue are TextViews. The other two background colors are RelativeLayouts.
As you can see, gravity works, but layout_gravity doesn't work.
Relative Layout with gravity
My original answer about gravity and layout_gravity dealt with these attributes being applied to the Views within a ViewGroup (specifically a LinearLayout), not to the ViewGroup itself. However, it is possible to set gravity and layout_gravity on a RelativeLayout. The layout_gravity would affect how the RelativeLayout is positioned within its own parent, so I will not deal with that here. How gravity affects the subviews, though, is shown in the image below.
I resized the widths of all the subviews so that what is happening is more clear. Note that the way RelativeLayout handles gravity is to take all the subviews as a group and move them around the layout. This means that whichever view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width.
Linear Layout with gravity
When you add gravity to a LinearLayout, it does arrange the subviews as one would expect. For example, one could "save code" by setting the gravity of LinearLayout to center_horizontally. That way there is no need individually set the layout_gravity of each subview. See the various options in the image below.
Note that when a view uses layout_gravity, it overrides the LinearLayout's gravity. (This can be seen in the title for the two layouts in the left image. The LinearLayout gravity was set to left and right, but the title TextView's layout_gravity was set to center_horizontally.)
Final notes
When positioning views within a RelativeLayout, the general way to do it is adding things like the following to each view:
layout_alignParentTop
layout_centerVertical
layout_below
layout_toRightOf
If one wants to set all the views at once, a LinearLayout would likely be better (or perhaps using a Style).
So to sum up,
The layout_gravity does not work for subviews in a RelativeLayout.
The gravity of a RelativeLayout does work, but not as one might expect.
Supplemental XML
XML for image "View gravity and layout_gravity inside a Relative Layout":
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad" >
<TextView
android:id="#+id/tvTop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:text="Views' gravity=" />
<!-- examples of gravity -->
<TextView
android:id="#+id/tvTop2"
android:layout_below="#id/tvTop1"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:id="#+id/tvTop3"
android:layout_below="#id/tvTop2"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:id="#+id/tvTop4"
android:layout_below="#id/tvTop3"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:id="#+id/tvTop5"
android:layout_below="#id/tvTop4"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd" >
<TextView
android:id="#+id/tvBottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:text="Views' layout_gravity=" />
<!-- examples of layout_gravity -->
<TextView
android:id="#+id/tvBottom2"
android:layout_below="#id/tvBottom1"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:id="#+id/tvBottom3"
android:layout_below="#id/tvBottom2"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:id="#+id/tvBottom4"
android:layout_below="#id/tvBottom3"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:id="#+id/tvBottom5"
android:layout_below="#id/tvBottom4"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</RelativeLayout>
</LinearLayout>
XML for image "Relative Layout with gravity":
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:background="#e3e2ad" >
<TextView
android:id="#+id/tvTop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Views' gravity=" />
<!-- examples of gravity -->
<TextView
android:id="#+id/tvTop2"
android:layout_below="#id/tvTop1"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:id="#+id/tvTop3"
android:layout_below="#id/tvTop2"
android:layout_width="300dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:id="#+id/tvTop4"
android:layout_below="#id/tvTop3"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:id="#+id/tvTop5"
android:layout_below="#id/tvTop4"
android:layout_width="150dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:background="#d6c6cd" >
<TextView
android:id="#+id/tvBottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Views' gravity=" />
<!-- examples of layout_gravity -->
<TextView
android:id="#+id/tvBottom2"
android:layout_below="#id/tvBottom1"
android:layout_width="200dp"
android:layout_height="40dp"
android:gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:id="#+id/tvBottom3"
android:layout_below="#id/tvBottom2"
android:layout_width="300dp"
android:layout_height="40dp"
android:gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:id="#+id/tvBottom4"
android:layout_below="#id/tvBottom3"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:id="#+id/tvBottom5"
android:layout_below="#id/tvBottom4"
android:layout_width="150dp"
android:layout_height="40dp"
android:gravity="center"
android:background="#aacaff"
android:text="center" />
</RelativeLayout>
</LinearLayout>
XML for image "Linear Layout with gravity":
<?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:gravity="center_horizontal"
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="View's gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="300dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="150dp"
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:gravity="center"
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="View's gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="300dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="150dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
</LinearLayout>
Testing your layout, for me it's working (the TextView is centered correctly).
However, you could also remove android:gravity="center_horizontal" from the RelativeLayout and add android:layout_centerHorizontal="true" in the TextView.
I've always had problems with RelativeLayouts and TextViews too. I think that this is due to the fact that TextView calculates its size dynamically and this prevents it to work well with layout's gravity. That's just one my supposition.
The solution I generally adopt is to configure a textview horizontal size with match_parent. It might be a suboptimal solution, but should work. In case you need a Fixed size textView you can put a fixed size dimension and should work too, it gives problems only with wrap_content.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:gravity="center_horizontal"
tools:context="com.something">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp" />
</RelativeLayout>
Edit:
as suggested by #Valentino S., adding
android:layout_centerHorizontal="true"
with wrap_content should work too. The reason is in my head still the same: size of textView is calculated later.
As the documentation says:
android:gravity
Specifies how an object should position its content, on both the X and
Y axes, within its own bounds.
...
center_horizontal
Place object in the horizontal center of its container, not changing
its size.
So the android:gravity="center_horizontal" will center horizontal-ing the RelativeLayout to its parent. Not making its content center horizontal.
Remember that android:layout_gravity is used for the view itself relative to the parent or layout.
Please be aware that the behaviour of Layout can differ slightly for each API Level. And be noted, that we can't 100% sure that the visual result in Layout Editor is correct. Always test your layout in real devices.
try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#ffffffff"
android:text="TITLE"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textSize="24sp" />
<AutoCompleteTextView
android:id="#+id/enterContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginBottom="10dp"
android:background="#ffffffff"
android:text="Enter Contact"
android:textAlignment="center" />
</RelativeLayout>
TlDr: android:gravity Works with RelativeLayout , you can skip to the bottom if you don't want to read the explanation
EDIT this is a giant wall of text, please bear with me
This is what I imagine you want to achieve:
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp"
android:layout_centerHorizontal="true"/> <!-- LAYOUT PARAMS WORK!!!-->
Why does this work, while android:gravity (apparently) doesn't?
if your goal is to put the TextView in the horizontal center of the Layout/Screen you have to let the RelativeLayout know!
this is achieved through something called LayoutParams - these are data structures defined by each View which are used by the View's parent (in this case the RelativeLayout)
so let's say your TextView has the following LayoutParams :
android:layout_centerHorizontal="true"
you will get something like this:
When the RelativeLayout is distributing its child views around the screen, along the way there is a callback method being run - onLayout(...) - which is part of a more complex method sequence that will determine the position of each child View inside the RelativeLayout, this is achieved in part by accessing the LayoutParams in each child View, in this case that line in your TextView
This is why we say LayoutParams are passed on to the parent like in the link you mentioned before
WARNING: Endless Confusion Source!
View positioning inside Layouts / ViewGroups is done through LayoutParams the onLayout calls
It so happens that some layouts like FrameLayout have a LayoutParam called android:layout_gravity which causes great confusion with the android:gravity property that each view can define, which is NOT the same and not even a LayoutParam
android:gravity is used by any View (like a TextView for example) to place its content inside.
Example: let's say that you change your TextView to be very high, and you want the text at the bottom of the TextView, instead of at the top
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="95dp" <------------ very high!
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp"
android:layout_centerHorizontal="true" <----- LayoutParams for RelativeLayout
/>
TextView is CENTERED in the RelativeLayout but text is at the TOP of the "box"
Let's use android:gravity to manage the text position INSIDE THE TextView
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:gravity="bottom" <---**NOT LayoutParams**, NOT passed to RLayout
/>
Result: As expected only the inside of the View changed
TLDR
Now if you want to ignore everything above and still use android:gravity with RelativeLayout, RelativeLayout is also a Viewso it has the android:gravity property, but you have to remove other properties or LayoutParams which will override the behaviour defined by the android:gravity property look at android:gravity at work with RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:gravity="bottom"> <!-- NOT LAYOUT PARAMS -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textAlignment="center"
android:text="#string/app_title"
android:layout_marginBottom="#dimen/main_spacing"
android:textSize="24sp"
android:layout_centerHorizontal="true"
/>
<AutoCompleteTextView
android:id="#+id/enterContact"
android:text="Enter Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_below="#+id/title"
android:layout_marginBottom="#dimen/main_spacing" />
</RelativeLayout>

Frame Layout fixed elements on wrap_content height

I have the next design:
Actual design app
CODE:
<LinearLayout
android:id="#+id/edit_name"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#99000000"
android:weightSum="1">
<FrameLayout
android:background="#color/tabIndicator"
android:layout_width="310dp"
android:layout_gravity="center"
android:clickable="true"
android:layout_height="wrap_content"
android:minHeight="136dp"
android:layout_weight="0.75">
<TextView
android:id="#+id/close_circle"
android:elevation="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:background="#drawable/oval_corner"
android:backgroundTint="#color/colorPrimary"
android:layout_gravity="top|bottom|right"
android:layout_alignParentEnd="true"
android:paddingLeft="8dp"
android:paddingTop="3dp"
android:paddingRight="8dp"
android:paddingBottom="3dp"
android:textColor="#color/tabIndicator"
android:textStyle="bold"
android:layout_margin="2dp"
android:translationZ="2dp">
</TextView>
<TextView
android:id="#+id/textDescLarga"
android:layout_width="match_parent"
android:layout_height="208dp"
android:text="#string/text_placeholder_benefit"
android:textStyle="normal"
android:textSize="12dp"
android:textColor="#color/textDefault"
android:layout_gravity="bottom|center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:maxLines = "12"
android:scrollbars="vertical">
</TextView>
<ImageView
android:id="#+id/imageMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/placeholder_benefit"
android:layout_gravity="top|left">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textTitulo"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:text="Nombre hotel"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="19dp"
android:textColor="#color/cardview_dark_background"/>
</FrameLayout>
PROBLEM:
When I need auto height, on the text, whether increases or decreases, I tried Fragment Layout layout_height: wrap_content but text overlap image.
What am I doing wrong? Is there another way to do this?
Thanks
FrameLayout renders it's children in a stack - one on top of the other unless you use layout_gravity to position them. FrameLayout is typically used with the entire available screen space - the layout_height and layout_width are set to match parent.
Your issue is because you have the layout_height set to wrap content, it will overlay widgets one on top of the other. Set the layout_height to match parent. Or better yet for your case, use a RelativeLayout.

How do I make my Button size match parent RelativeLayout size exactly in android?

I thought that match_parent would do the trick however when I set match_parent to the width it stretches the whole RelativeLayout. However the height is correct...
Here is my xml:
<RelativeLayout
android:id="#+id/moreRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.24"
android:layout_gravity="center">
<Button
android:id="#+id/moreButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="#drawable/more_menu_icon"
android:alpha="0.5"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:layout_weight="0.24"
android:onClick="moreIconPressed"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:alpha="0.5"
android:textColor="#000000"
android:text="More"
android:layout_centerInParent="true"
android:layout_below="#+id/moreButton"
android:layout_marginTop="-3dp"
android:textSize="12dp" />
<Button
android:id="#+id/moreBiggerButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" />
</RelativeLayout>
It is the last Button(moreBiggerButton) that I wish to cover the whole RelativeLayout (moreRelativeLayout). What am I doing wrong? I thought something like this would be as simple as setting both width and height to match_parent :S Please help.

Can't align ImageViews to the left in ListView Row (RelativeLayout)

This is how I have my view at the moment.
As you can see, the icons are not all aligned all the way to the left. The red line is my objective. If they go all the way to the left, no problem, because I can solve it later with marginLeft.
This is my XML code (layout/listview_style_listview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/BackGroundColor"
android:orientation="vertical"
android:scaleType="center" >
<LinearLayout
android:id="#+id/titleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:background="#color/BackGroundColor"
android:orientation="horizontal" >
<TextView
android:id="#+id/sectionTitle"
style="#style/sectionTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:text="(blank)" />
<Button
android:id="#+id/sectionInfoButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/infoicon" />
<Button
android:id="#+id/sectionInfoButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/filtericon" />
<Button
android:id="#+id/sectionOptionsButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/menuicontop2" />
<Button
android:id="#+id/addCatButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/effect_button_add_cat_click" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:background="#color/blueOceanStroke" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/BlueOcean"
android:dividerHeight="1dp"
android:listSelector="#drawable/listview_style_list_selector"
android:paddingBottom="1dp" />
</LinearLayout>
This is my XML code (layout/listview_style_row.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="35dp" >
<ImageView
android:id="#+id/catThumbnail"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/bank1" />
<TextView
android:id="#+id/sectionTitle"
style="#style/title"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignBottom="#+id/catThumbnail"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="66dp"
android:gravity="center_vertical"
android:text="Title Text"
android:textColor="#color/listsTextColor"
android:textSize="15sp"
android:textStyle="normal"
android:typeface="normal" />
<ImageView
android:layout_width="25dip"
android:layout_height="32dp"
android:layout_alignBottom="#+id/sectionTitle"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:src="#drawable/arrow" />
</RelativeLayout>
PS: I added an arrow icon to the right after the screenshot and that's why it's not showing on the image but I guess it's not important.
From the screenshot the layout is OK because the divider with back color #color/blueOceanStroke goes all the way and so do the separators. The problem must be in the "#+id/catThumbnail", if you measure the distance between your red line and the left edge of the yellow square and compare it with the distance between the right edge of the square and the left edge of letter "B" they are about the same which it shouldn't be.
My guess is that there is some king of padding in the #drawable/bank1. Try to remove it completely and see if the letter "B" left aligns with the title.
Hope this helps...
I was able to fix it with android:layout_marginLeft="-10dp" on the "ImageView #+id/catThumbnail" in listview_style_row.xml, but i really dont think this is a valid solution for a final perfectly designed app, yes? For me to put negative values in there, theres something wrong, right?
As someone else mentioned, I would try removing the ImageView and seeing if the TextView will be flush against the red line. If it is, the ImageView has some issues, if not it's something else. Also, the ImageView may not have the same height and width and that could cause issues with the scaling you're doing by setting to 32 dp, see: Unwanted padding around an ImageView.
It might be easier to wrap your ImageView in a FrameLayout. Something like this:
<FrameLayout>
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/catThumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/bank1" />
</FrameLayout>
Or if that doesn't work, try setting the width and height of the image view and/or the frame layout to wrap_content. Also if that image is a 9-patch, there may be other causes.

Categories

Resources