I have an xml file that represents a slide that gets loaded by a custom PagerAdapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slideWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ccc"
android:orientation="vertical">
<ImageView
android:id="@+id/ivSlide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/slide_im_putting_face"
android:contentDescription="image"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
<EditText
android:id="@+id/edtSlide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:lines="3"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollHorizontally="false"
android:textSize="25sp"
android:hint="@string/main_hint"
android:textColor="#000"
android:padding="10dp"
android:layout_marginBottom="40dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
How do I adjust the slideWrap container to be the exact same dimensions as the scaled ImageView? Not quite sure how to do this a PagerAdapter.
Ok, so the extra white space was because the ViewPager, apparently you cannot set wrap content for your height: https://stackoverflow.com/a/8532550/641738
Instead, I just calculated a few percentages based on the static width/height of all my slides and set the wrapper accordingly. I could have just set the slide dimensions in their respective dp layout folders, but that seemed like a hassle. Seems to work fairly well on a few handheld devices so far as long as all the slides are the same dimensions. The percentages were somewhat trial and error:
// Check the display and make the necessary adjustments for Portrait mode
if(display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) {
double width = display.getWidth() * 0.93;
double adjustedHeight = width * 0.85;
wrapLayoutParams = new LinearLayout.LayoutParams((int)width, (int)adjustedHeight);
}
else {
double height = display.getHeight() * 0.73;
double adjustedWidth = height * 1.15;
wrapLayoutParams = new LinearLayout.LayoutParams((int)adjustedWidth, (int)height);
}
slideWrap.setLayoutParams(wrapLayoutParams);