Bottom align and bottom clip a view? - android

I'm trying to clip a view that should be bottom aligned.
For example, we can have a view that is top clipped by using a negative top margin:
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="wrap_content">
<ImageView
android:layout_marginTop="-20dp" />
</LinearLayout>
But I want to bottom-align the ImageView in my case, then push it down by 20 dp.
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:gravity="bottom">
<ImageView
android:layout_marginTop="20dp" />
</LinearLayout>
but since the parent view is wrapping the height, the extra margin just increases the height of the parent view, instead of having the ImageView get clipped. I need to leave the height of the parent as wrap_content.
I can do this with a custom view of course, just wondering if there's a way to do this without having to go that route.
Thanks

Related

View to fill and center contents in the remaining space Android

I have a LinearLayout which is a view that I need to display under another view, all within a ConstraintLayout. I want to LinearLayout's height to be dynamically equal to the space between the bottom of the above view, and the bottom of the parent. I've tried
<android.support.v7.widget.LinearLayoutCompat
android:id="#+id/sample_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:visibility="gone"
without success. I am very new at Android UI work so I'm not sure if I'm using the right layout. Can anyone suggest a better implementation, or a fix to my current attempt?
You means this?:
<TextView
android:id="#+id/your_linear_layout"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginEnd="56dp"
android:background="#0ff"
android:text="your LinearLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/above_view"
app:layout_constraintVertical_bias="0.0"/>
you can put the LinearLayout inside a RelativeLayout that is under the needed view, and make the height of the relative layout to fill the space, while you can make the linear layout to be in the center of the relative layout such as:
<RelativeLayout ... >
<LinearLayout
....
android:centerVertical="true"
</RelativeLayout>

Stretch image view up to the screen sides (without margins)

Good Day!
How can I achieve the look of the first screen? I wanted to stretch an image view up to the edge of the screen but I all I get is an image view with margins on the sides. Here is my xml code:
<ImageView android:layout_width="wrap_content"
android:layout_height="240dp"
app:srcCompat="#drawable/green_image"
tools:layout_editor_absoluteX="0dp"
android:id="#+id/imageView"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent" />
generally to achieve what you want to stretch the ImageView's width your layout.xml should look like this :
the parent layout of your imageView should not have any margin and padding on left or right
and its width should be match_parent
and your image view should not have any margin on left or right and its width should be match_parent too.
for example you have your ImageView inside an LinearLayout :
<LinearLayout
android:layout_width="match_parent"
...
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp">
<ImageView
android:layout_width="match_parent"
...
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp" />
</LinearLayout>
marginLeft or marginRight = 0dp could be removed just added them so could point that you should not have margins
hope this helps.

Linearlayout spacing in Android

This is my layout:
TEXTVIEW
IMAGEVIEW (optional)
LINEARLAYOUT - to which I add Buttons dynamically
LINEARLAYOUT - with two buttons side by side (left button and right button)
What do I need to do to ensure that the bottom two linear layouts are fixed to the bottom of the screen, regardless of how much space they may take up? ie. The first linear layout might have 3 buttons and take up over half the screen, which is okay. It just needs to be above the left/right buttons in the last linear layout, which is fixed to the bottom.
Then I want my TextView and my ImageView vertically centred in the remaining space. The ImageView will be set to invisible if there is no image, so it could only be the text view which needs to be centred.
I've been playing around with android:gravity="bottom", android:layout_height="0dip"/android:layout_weight="1" (I later realised this would only give 50% to the text/imageview and 50% to the 2 linear layouts), but I can't get my desired result.
Any advice appreciated.
You have to take RelativeLayout.
There you have a better control of the relative position of the views, something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/textView"
android:layout_above="#+id/imageView"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView"
android:layout_above="#+id/ll_1"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLyout
android:id="#+id/ll_1"
android:layout_above="#+id/ll_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLyout
android:id="#+id/ll_2"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>

android:gravity="top" doesn't align child view to top, but "center" aligns to center?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top" <!--this line-->
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="56dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="36dp" />
</LinearLayout>
Shouldn't that make the smaller "0" t-view align to the top of the inner LinearLayout?
Since the other "0" is larger in height, it increases the overall height of the inner LinearLayout.
Alternatively, if you included this:
android:layout_gravity="top"
within the t-view of the smaller 0, it also does nothing.
Why is this?
Does the wrap_content of the LinearLayout wrap each individual view independent of others?
If so, then why does setting gravity to "center" work? In the sense that the smaller zero is vertically centered to its parent.
I know you can just set the smaller 0's height to match parent and set its own gravity to top. I'm just trying to understand this. Thanks.
The default behavior of the layout is to align the baselines of the text, which in English are at the bottom of the view. Adding android:baselineAligned="false" to your LinearLayout will align the second TextView at the top of the view.
gravity attribute applies to widgets within the LinearLayout, while layout_gravity tells the parent of LinearLayout where to place the child (LinearLayout).
Also, in a vertical LinearLayout the gravity="top" attribute won't work.
In the layout you have now, both text views will be stacked one on top of the other, wrapped with a linear layout with no space in-between - so the "top" or "center" values won't do anything because there is no extra space to move the text views up or down.
If you want to understand this better, try giving your linear layout and both text views backgrounds of different colors, like this:
android:background="#color/mycolor"
Then you will see the bounds of each widget.
You can use android:layout_weight="10" and distribute on the other components.

How to get an image to resize in a widget layout to make room for other views in its container?

I'm struggling with a widget layout. Essentially I have an image above some text, wrapped in a vertical LinearLayout container. The image seems to take up all the space in the container (aligned at the bottom of the widget) forcing the text off the bottom of the widget and therefore its not visible. Is there a way to get the image to auto-resize to avoid any other views below it from being forced out of the containers view? Here's the code:
<LinearLayout
android:id="#+id/vertical_container"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/the_image"
android:src="#drawable/image_32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/the_poor_text_which_doesnt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Where am I?"
android:textColor="#ffffff"
android:textSize="11dip" />
</LinearLayout>
Try using a RelativeLayout instead of a LinearLayout, declaring the TextView first with the parameter android:layout_alignParentBottom="true", and then the ImageView, with android:layout_above="#+id/the_poor_text_which_doesnt_show".

Categories

Resources