Benjamin Basmaci
Benjamin Basmaci

Reputation: 2567

Margin is set to Match_Parent. Works in emulator but shows space on real device

I have a Method that creates an ImageView to be added in a LinearLayout programmatically:

private static ImageView createBaseImageView(Context context, Drawable image, float bottomMargin)
{
    ImageView imageView = new ImageView(context);
    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(image);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    imageView.setLayoutParams(layoutParams);

    return imageView;
} 

I want the image to match the parents width and to match the content in its height. This works perfectly on the emulator Android Studio provides. However, when I install the app on a real device, somehow I see an edge in the right an left side of the image that shows the background color.

I tried to set the padding to 0 explicitly with imageView.setPadding(0, 0, 0, 0); but that didn't help either.

What is the reason for that behaviour, and how can I solve it?

As additional information, this is a snippet of the setup for my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".Context">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/default_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/imageLayoutView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Upvotes: 0

Views: 193

Answers (1)

SANDIP CHAUDHARI
SANDIP CHAUDHARI

Reputation: 166

Set imageview scale type to fitxy

Upvotes: 1

Related Questions