VIewPager2 using wrap_content instead of match_parent - android

I have a horizontal scrolling view pager. I set child items width to match_parent. But im getting them wrap_content instead. This view should be centred by gravity but parent's FrameLayout is cropped to child's width
Items code:
<FrameLayout 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">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exo_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:use_controller="false" />
<ImageView
android:id="#+id/sound"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_gravity="bottom|right"
android:layout_margin="13dp"
android:src="#drawable/icon_sound_off_active" />
</FrameLayout>

I think the issue might be related to the resolution of the video you being played, rather than the view matching its parent's width.

Related

RecyclerView wraps its conent

I am using the RecyclerView to inflate a Linear Vertical listViews, However, although the width of a child is set to match_parent, RecyclerView wraps that at runtime.
Here is a screenshot of the problem:
item_recyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="#+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="#drawable/ic_user"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
When inflating a layout file, the layout_ attributes on the root view will be ignored unless you specify a parent ViewGroup in the inflate() call. You are not currently doing so.
In your onCreateViewHolder() method, replace this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
with this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
Note also that you probably don't want to be using match_parent for the height of your RecyclerView's items:
android:layout_width="match_parent"
android:layout_height="match_parent"
This will give the appearance that only a single item is visible in your RecyclerView, since each item will be the full height of the screen. This is currently not a problem for the exact same reason that the match_parent width is being ignored.
give for your parent linearlayout weightSum 5
give for imageView weight 1 and do his width 0
and for child linearlayout weight 4
enter code here

ScrollView and its content use 'wrap_content'. How to assign constraint priority?

I need a horizontal ScrollView with a fixed height of 100dp and dynamic content of ImageViews.
Currently I the ScrollView has a horizontal LinearLayout with ImageViews. The ImageViews have wrap_content enabled for their width, while the height is on the fixed height for all of them. The ScrollView itself has its height on wrap_content.
If the ScrollViews content is bigger than what the view can display without scrolling, the ImageViews images are scaled down. The ImageViews layout_hight works in terms of its bounds, but the image itself is not.
Changing scaleType to something else doesn't help. Setting a fixed width for the LinearLayout works, but as the content should be dynamic that's not an option. This seems like a default use case. Isn't this possible in xml?
Example with a manually given exact ScrollView-width:
The view looks as I need it, but won't allow dynamic content.
Example with width on wrap_content:
The ImageView
ScrollView code below:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#CCCCFF">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AAAAFF"
android:orientation="horizontal">
<ImageView
android:id="#+id/so1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:background="#FF0000"
android:scaleType="fitCenter"
android:src="#drawable/so" />
<Space
android:layout_width="10dp"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/so2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:background="#FF0000"
android:scaleType="fitCenter"
android:src="#drawable/so" />
<Space
android:layout_width="10dp"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/so3"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:background="#FF0000"
android:scaleType="fitCenter"
android:src="#drawable/so" />
</LinearLayout>
</ScrollView>
Ok, that was way easier to fix, than i thought:
ScrollViews are always vertical. There is no orientation attribute in ScrollView. To get a horizontal ScrollView instead use HorizontalScrollView and ditch the orientation.
So this
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
should instead be:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

ConstraintLayout inside of ViewPager wrap_content not working

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'

RecyclerView item fills the whole screen

I have a Recyclerview which is working fine with the Android version 23 but if i am running the same code with the Android version 25 then the whole screen is occupied by the single item.
Initially the list looks fine where the item height is wrap content. But as i scroll the whole screen is occupied by single item.
Below is my layout containing RecyclerView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
Your Recyclerview code is ok.
if you are using TextView or else in your iteam_raw.xml then make sure you give
"wrap_content"
iteam_raw.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Please use recyclerview item's main layout height "wrap_content".
Make sure that the all UI element you are using in ViewHolder must have height to wrap content and if you are using cardview as parent container make that height also wrap_content.
change the layout_height property from match_parent to wrap_content or some size in your linearlayout.
example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="120dp" <-- **change in height**
android:layout_width="match_parent"
android:orientation="vertical">
Set layout height and width to 0dp
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_height="0dp"
android:layout_width="0dp"
android:scrollbars="vertical" />

Android: FrameLayout squeezes View

I am using the following layout:
FrameLayout fills the entire Viewport, i.e. width and height are set to MATCH_PARENT
Width and height of RelativeLayout are set to WRAP_CONTENT
View1 to View5 have fixed dimensions e.g. width = 500, height = 50
View 5 lies near the border of FrameLayout and is squeezed by Android,
so that it lies fully within FrameLayout. The height of View5 should be 50 but unfortunately Android changes it to a smaller value.
How can I avoid, that Android changes the height of View5 ?
When I scroll RelativeLayout, the error is still existing.
A similar behavior is described here:
button is squeezed when it exceeds layout
A "better" Approach to this problem is to use a ScrollView like
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true" />
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/view1" />
<View
android:id="#+id/view3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/view2" />
<!-- YOU GET THE IDEA -->
</RelativeLayout>
</ScrollView>

Categories

Resources