RelativeLayout: wrap_content is not respected - android

Why the RelativeLayout doesn't wrap content? If I remove the last view which aligns to the bottom parent, it works...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:layout_marginBottom="#dimen/persistent_buttons_area_height"
android:paddingBottom="8dp">
<com.xxx.ui.presentation.VerticalNestedScrollview
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="11dp"
android:clipToPadding="false"
android:overScrollMode="never">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/description"
style="#style/PresentationDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
</com.xxx.ui.presentation.VerticalNestedScrollview>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/keyline_1"
android:layout_alignParentTop="true"
android:background="#drawable/list_top_gradient_dark"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/keyline_1"
android:layout_alignParentBottom="true"
android:background="#drawable/list_bottom_gradient_dark"/>
</RelativeLayout>
The current result is (in red rectangle is the RelativeLayout):
So can I have a wrap content properly for this RelativeLayout?
Thank you very much guys!

How it could be, because you are using View with android:layout_alignParentBottom="true", Now what it does is that align that view to the bottom of the relative layout, now RelativeLayout height is wrap_content this won't work because android:layout_alignParentBottom="true" which force relative layout to use as much space is available.
So Possible solution can be:
add android:layout_below="#+id/scroll_view" and remove android:layout_alignParentBottom="true" from View which is at bottom of the screen.

Related

Fit Scroll View to Available Space in Android

I have a problem with my ScrollView. I want to fit it inside the space available but i donĀ“t know how.
Now i have my ScrollView and my TextView before one LinearLayout:
<ScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/previousimage"
android:layout_marginTop="#dimen/margin">
<TextView
android:id="#+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margintop"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/margin_five"
android:scrollbars="vertical"
android:textColor="#color/white"
android:textSize="#dimen/common_textsize" />
</ScrollView>
<LinearLayout
android:id="#+id/bottom_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/scrollView"
android:layout_marginTop="#dimen/margin_small"
android:gravity="center|bottom"
android:orientation="vertical">
The problem is that if i have so much text inside my TextView the LinearLayout go down and it hides.I want to scroll the text inside. Without push down the LinearLayout.
I want to take this space for ScrollView automatically to adapt to all screen size.
See the image Layaout
Thank you!
Define LinearLayout before your ScrollView and set ScrollView to above LinearLayout, match_parent. Like this:
<LinearLayout
android:id="#+id/bottom_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="#dimen/margin_small"
android:gravity="center|bottom"
android:orientation="vertical">
...
</LinearLayout>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:fillViewport="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/previousimage"
android:layout_above="#+id/bottom_options"
android:layout_marginTop="#dimen/margin">
<TextView
android:id="#+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margintop"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/margin_five"
android:scrollbars="vertical"
android:textColor="#color/white"
android:textSize="#dimen/common_textsize" />
</ScrollView>
The ScrollView should always match_parent (or at least be smaller than the available space) otherwise it just take the same space as its children and so from its perspective, no children is out, so no need to scroll.

How can I position the end edge of a TextView to the center of the layout?

I have a simple TextView inside of a RelativeLayout.
I want to position end edge of TextView to center of the RelativeLayout as in image below
I found something like android:layout_toStartOf=...
Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf=...
/>
</RelativeLayout>
But this only aligns the end edge to start of something. (Can't be used for align end edge to CENTER of layout).
NOTE: There is already a similar question and answer here.
You need to have a widget in the RelativeLayout that you can use as a reference to position the TextView. An empty Space widget (0dp x 0dp) centered horizontally works well for this.
<RelativeLayout>
<!-- Empty space widget (0x0 dp) centered horizontally -->
<Space
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<!-- TextView to left of the spacer-->
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/spacer"
android:text="awesome text!"/>
</RelativeLayout>
You could create a dummy View centered within your RelativeLayout and then align the TextView left to it:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/center_anchor"
android:text="hello world" />
</RelativeLayout>
You may also have a look at the PercentRelativeLayout which supports percentage based dimensions and margins. That means you can specify a right margin of 50% for example.
You can do that using PercentRelativeLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/white"
android:text="TextView"
android:textColor="#android:color/black"
app:layout_marginRightPercent="50%" />

Setting center gravity of child view change layout alignment of another child view in horizontal layout?

In my horizontal LinearLayout, I set the gravity in one view to be center_vertical, and then I tried to set layout_gravity for a second view, but that view is lined up with the centered text in the first view!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top"
android:background="#drawable/border"
android:gravity="center_vertical"
android:text="layout_gravity=top gravity=center_vertical" >
</TextView>
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_gravity="top"
android:background="#drawable/border"
android:gravity="top"
android:text="layout_gravity=top gravity=top" >
</TextView>
</LinearLayout>
And here is the same code but for a vertical layout. Notice that the desired behavior works correctly in the vertical layout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:background="#drawable/border"
android:gravity="center_horizontal"
android:text="layout_gravity=left gravity=center_horizontal" >
</TextView>
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="right"
android:background="#drawable/border"
android:gravity="right"
android:text="layout_gravity=right gravity=right" >
</TextView>
</LinearLayout>
I can just use a RelativeLayout or maybe another nested LinearLayout to fix the problem. But I am asking this question because I would like to know if I am not understanding how gravity and layout_gravity works!! It is important to me that I understand how these fundamental attributes work. Thanks
If you want one centered and one top then you need to add a baseline aligned flag to the LinearLayout
android:baselineAligned="false"
It is default for the layout to align the children's baselines. This can cause issues such as this when the children use different values of gravity.
The difference with vertical is that the baselines can't be aligned as they can in horizontal.
See:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:baselineAligned
Additional info - This appears to explain the issue better than I can:
http://www.doubleencore.com/2013/10/shifty-baseline-alignment/
if you are trying something not "connected to each other" i.e. you want some space between two widgets then use a blank widget and use weights
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_weight="40"
android:gravity="center_vertical"
android:text="layout_gravity=top gravity=center_vertical" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20" />
<TextView
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_weight="40"
android:gravity="right"
android:text="layout_gravity=top gravity=top" >
</TextView>
</LinearLayout>
You werent clear about the output that you wanted , this is what i could interpret as your required output.. But i will explain you how this works ..
For more on weights you can go to http://developer.android.com/reference/android/widget/package-summary.html
There is something called as "widget" and "content of widget"
While talking of linearlayout with layout_gravity you set gravity of it self as a "widget" in the main View and with gravity you set gravity to the content in that linearlayout
Simmilarly with say textview , if you use layout_gravity (say equal to center) then the textview will be centered in its space (i.e. it will be centered in its width if layoutorientation is vertical , and it will be centered in its height if layout_orientation is horizontal)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Second Activity" />
</LinearLayout>
See the same in vertical orientation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Second Activity" />
</LinearLayout>
Also now see about the gravity when you apply gravity to textview, the "TEXT" in the textview is centered (if gravity=center)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
**android:gravity="center"**
android:text="Second Activity" />
</LinearLayout>
Now you can realise that gravity sets the gravity to the content within the widget.. the "content" of a widget can be widgets itself..
LinearLayout is a widget , and within linear layout you add yet other types of widgets (button and text view)
So applying gravity=center to textview sets its content(i.e. text)at the center and applying gravity to linearlayout will center its content(i.e. textview) at the center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Second Activity" />
</LinearLayout>
gravity is how the view itself positions its content within the area determined by its width and height. layout_gravity is how the parent view positions the child within the parent's area. Usually you can simply set gravity on the LinearLayout instead of setting layout_gravity on all of its children.
Do note that for LinearLayouts, gravity only has an effect on the axis that is not the orientation, e.g. top and bottom would be relevant to a horizontal LinearLayout, but left and right (or start and end) would not be relevant.

How to make view match parent dynamically

I have a layout as below. Location view maybe gone or visible and controlled by code. Thus the height of content view is variable based on the location view. But the divider view has always same height although it declared as matching to its parent. Would you like to let me know how to make sure divider view has same height with its parent? Thanks a lot.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/divider"
android:orientation="vertical" >
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="gone" />
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<View
android:id="#+id/divider"
android:layout_width="1px"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/ic_divider_dashed_holo_dark" />
</RelativeLayout>
Man you have it totally mixed up, why are you using linear layout inside? makes no sense. The relative layout has no orientation concept.
About that height, possibly try alignTop and alignBottom to content, with wrap_content instead match_parent (also dont use fill_parent, atleast dont mix them with match_parent).

RelativeLayout - CenterInParent and marginTop

I has the following XML:
<RelativeLayout android:id="#+id/cover_box"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="#+id/cover"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="#+id/download" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/mark_download"
android:layout_centerInParent="true" android:layout_marginTop="90px" />
</RelativeLayout>
But it's look's like the marginTop is being ignored.
If you want the 2nd image to be 90dp under the center of the screen, you could replace it with a FrameLayout where you can control the padding to move your image downwards.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="90dp">
<ImageView android:id="#+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark_download"/>
</FrameLayout>
When you use center in parent the view is put directly in the center. The margin top will only come into play if an object is within 90px of the top of your view. Thus pushing the centered view down to keep at least 90px of space on top of it. So it isn't being ignored but it is not having the effect that you think it should have.
I want the progressbar to shown under android:layout_centerInParent="true" so i have added a dummy TextView and set it to centerInParent .Then i put my progressbar under it. Now you can increase its distance from center in two ways . First by increasing marginTop in TextView and second increasing TextView height.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash" >
<FrameLayout
android:id="#+id/splash_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.s3.tdd.interfaces.CircularProgressBar
android:id="#+id/circularprogressbar2"
style="#style/Widget.ProgressBar.Holo.CircularProgressBar"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_below="#+id/txt1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can put your imageview inside of other ViewGroup (LinearLayout of RelativeLayout layout), leaving the margin of imageview, and doing android:centerInParent="true" for the ViewGroup:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:centerInParent="true">
<ImageView android:id="#+id/download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/mark_download" android:layout_marginTop="90px" />
</LinearLayout>
</RelativeLayout>
You can make a dummy view and center it to parent. Now align your view relative to dummy view using layout:alignComponent and give the marginTop. Now in the code move it according to the width of your view to center it.

Categories

Resources