I have a CoordinatorLayout in which I have a BottomSheet and an ImageView.
This is my BottomSheett:
<include layout="#layout/bottom_sheet_layout"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
app:layout_behavior="#string/bottom_sheet_behavior"/>
And this my ImageView:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/image_location_crosshairs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_anchorGravity="top|end"
app:layout_anchor="#id/bottom_sheet"
app:srcCompat="#drawable/my_svg_icon"/>
Regardless of the view order in my XML layout, I always get the same result: my ImageView is nicely anchored to the bottomsheet, but the anchor takes place at the center of my image.
I would like to know how can I have my image bottom being anchored instead of its center.
Trying to apply any kind of bottom margin will only have the bottomsheet slide until reaching center of image before making the image slides as well.
Thanks for the help !
You can create an empty View, with layout_height and layout_width set to wrap_content.
<View
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And then link this empty View to the BottomSheet.
Related
I created a ViewPager and put it inside of ConstraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green">
<ViewPager
android:id="#+id/teaser_view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:background="#color/blue"
android:currentPage="#={viewModel.currentItem}"
bind:adapter="#{viewModel.adapter}"
bind:withAnimation="#{viewModel.changeWithAnimation}"
bind:layout_constraintStart_toStartOf="parent"
bind:layout_constraintTop_toTopOf="parent"
bind:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and the layout of item inside of ViewPager is:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/red">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="#color/black"
bind:imageUrl="#{viewModel.imagePath}"
bind:layout_constraintDimensionRatio="3:1"
android:adjustViewBounds="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
as you see, every view or it's parent has different background. And if I run the app, I see:
the background is red, it means, the ConstraintLayout in ViewPager item is stretched to the whole height of screen despite wrap_content.
Anybody knows, how to shrink the item to the size of the image inside?
P.S.: I use ConstraintLayout here, because I need to keep the image size in aspect ratio "3:1"
Set all constraints to parent
then use 0dp in height or width instead of `wrap_content'
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>
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.
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
i want to create something like that:
removed image
i have tried using relative layout, when I align the image and the background view to the top
and applying margin to the bg view:
<RelativeLayout
android:id="#+id/fieldLinearLayout"
android:layout_width="match_parent"
android:layout_height="80dp" >
<ImageView
android:id="#+id/mood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fieldLinearLayout1"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/signup_illu_verificationcode" />
<LinearLayout
android:id="#+id/fieldLinearLayout1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:background="#f9ffff"
android:orientation="vertical" >
but it didn't work, the bg covers the image.
how can I fix that?
Have you tried using a FrameLayout? With a FrameLayout, you can decide the z-index by the order of which you add the items. The first items will sit on the bottom and the last items will sit on the top.
You can try using negative margins to move the ImageView up.