0

I am using a ViewPager in an activity layout which is a ConstraintLayout. This ViewPager is constrained in order to be displayed below a sibling widget. This ViewPager, so, doesn't cover the whole screen.

However, in the fragments of this ViewPager, there is an ImageView that must be displayed covering the whole screen. This ImageView is unique per fragment of this ViewPager (implying that each ImageView must actually be contained in each ViewPager's fragments).

The problem is: since this ImageView is within the fragments of the ViewPager that doesn't cover the whole screen, then it's impossible to use something like match_parent for this ImageView, since it will match the ViewPager which, obviously, doesn't cover all the screen (since it's shown below a sibling widget).

What could I do to make the ImageView fill the whole screen?

JarsOfJam-Scheduler
  • 1,659
  • 1
  • 16
  • 40

2 Answers2

1

When you show the imageView fragment in view pager you can change your constraints programmatically. Here there is simple example for this

ConstraintLayout: change constraints programmatically

  • I'm trying it but it appears that it's impossible to programatically define constraints between a `ViewPager`'s item's widget and a container's widget (container that contains also the `ViewPager`). – JarsOfJam-Scheduler Apr 08 '20 at 09:15
  • I think you have to change all viewPager constraints as matchConstraint for all screen when you open the imageView – Okan Serdaroğlu Apr 08 '20 at 11:15
1

fragment.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPagerImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

viewpger_item.xml

<androidx.constraintlayout.widget.ConstraintLayout
            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="vertical">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imgPool"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Shweta Chauhan
  • 5,468
  • 5
  • 26
  • 51
  • `android:layout_margin="@dimen/_10sdp"` used on the `Viewpager` will impede the `ImageView` from covering the whole screen, no? – JarsOfJam-Scheduler Apr 08 '20 at 08:45
  • 1
    you can remove that margin. I add this code from my working code that's why it's there – Shweta Chauhan Apr 08 '20 at 08:54
  • 1
    Thank you for your answer. However, I actually need to set the `ViewPager`'s item's `ImageView` covering the screen, BUT the `ViewPager` must not cover the whole screen (so the `margin` was good, but your solution didn't fix the problem). I think it's not possible. However I really must do this UI. I'm trying another thing and I will create a new question. – JarsOfJam-Scheduler Apr 08 '20 at 09:13