Search code examples
javaandroidandroid-layout

Android: How to remove blank space from HorizontalScrollView


How to remove blank space that I get on the right?

When I have 5 or more photos, then blank space disappears.
With 2 photos I get blank space.

Tried on Galaxy Nexus emulator and also on my Samsung S23 phone and the result is the same.

Tried also with requestLayout() on each element after the loop, but the result was the same.


Populating photos:

final LinearLayout parent = activity.findViewById(R.id.detailsArticle_Photos);

for (final Bitmap bitmap : bitmaps) {

    final CardView cardView =
        (CardView) LayoutInflater
            .from(activity)
            .inflate(R.layout.element_article_details_photo, parent, false);

    final ImageView imageView =
        (ImageView) cardView.findViewById(R.id.detailsArticle_MultiplePhoto);

    imageView.setImageBitmap(bitmap);

    parent.addView(cardView);
}

Parent view:

<HorizontalScrollView
    android:id="@+id/detailsArticle_PhotosWrapper"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:visibility="gone"
    tools:visibility="visible"
    android:fadeScrollbars="false"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <LinearLayout
        android:id="@+id/detailsArticle_Photos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingBottom="10dp"/>

</HorizontalScrollView>

Child view (element_article_details_photo):

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    app:cardElevation="0dp"
    android:layout_weight="0">

    <ImageView
        android:id="@+id/detailsArticle_MultiplePhoto"
        android:layout_width="wrap_content"
        android:minWidth="267dp"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"/>

</androidx.cardview.widget.CardView>

enter image description here


Solution

  • You need to make couple of changes here:

    1. Parent view:
    <HorizontalScrollView
        android:id="@+id/detailsArticle_PhotosWrapper"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:visibility="gone"
        tools:visibility="visible"
        android:fadeScrollbars="false"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">
    
        <LinearLayout
            android:id="@+id/detailsArticle_Photos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingBottom="10dp"/>
    
    </HorizontalScrollView>
    

    here, android:layout_width="match_parent" has been changed to android:layout_width="wrap_content" and removed android:layout_gravity="center" android:gravity="center"

    1. Child view (element_article_details_photo):
    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        app:cardElevation="0dp"
        android:layout_weight="0">
    
        <ImageView
            android:id="@+id/detailsArticle_MultiplePhoto"
            android:layout_width="wrap_content"
            android:minWidth="267dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"/>
    
    </androidx.cardview.widget.CardView>
    

    here, android:layout_weight="0" has been removed.