Center elements of HorizontalScrollView when not enough to make it scroll Center elements of HorizontalScrollView when not enough to make it scroll android android

Center elements of HorizontalScrollView when not enough to make it scroll


I just solved this issue. I ran into it a few hours ago. You need to center the HorizontalScrollView in its parent and set its width/height to wrap_content. The layout you put in the the HSV must have its width/height set to wrap content as well. The important part here is to not set any gravity/layout_gravity on this layout or you may experience (very annoying) clipping issues after inflating your views. Example below is contained in a RelativeLayout.

 <HorizontalScrollView  android:id="@+id/svExample"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_below="@id/rlExample">    <LinearLayout        android:id="@+id/llExample"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">    </LinearLayout></HorizontalScrollView >


I had the same problem and finally got it to work. Here is a minimal example:

The main activity layout with the HorizontalScrollView:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <HorizontalScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:fillViewport="true" >        <LinearLayout            android:id="@+id/container"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal" >        </LinearLayout>    </HorizontalScrollView></LinearLayout>

The layout of the elements that will be inflated and put inside the scroll view:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="horizontal" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="your text" /></LinearLayout>

And this is an example of the code to inflate and add the elements:

LinearLayout container = (LinearLayout) findViewById(R.id.container);LayoutParams elementLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);int elementCount = 3; // ...or however many you likefor (int i = 0; i < elementCount; i++) {    LinearLayout element = (LinearLayout) getLayoutInflater().inflate(R.layout.element, null);    container.addView(element, elementLayoutParams);}

It's mainly based on a technique for stretching the content of ScrollViews explained in this article by Romain Guy.

However in this case where you are adding contents dynamically, another key point is setting the positive value for the weight (1f in this example) using LayoutParams when adding the elements to the container. If you have a static set of elements that you can include directly in the container and don't need to inflate you can specify the weight of the elements' outer LinearLayout in the XML layout like so:

android:layout_weight="1"

But if you do it dynamically that won't work because the weight will reset to 0 and the elements collapse. Hence you need to set it via the LayoutParams.


Below is the simplest way worked for me.

<HorizontalScrollView    android:id="@+id/horizontalScrollView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:layout_gravity="center">    <LinearLayout        android:id="@+id/layout_others"        android:orientation="horizontal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></HorizontalScrollView>