Search code examples
androidhorizontalscrollview

how to fit layout width inside of a horizontalscrollview?


I want to fit width of the LinearLayout horizontally but there is some blank space in end of it(see the picture)Picture

this is my code :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shahin.testing2.MainActivity">
<HorizontalScrollView
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="40px"
    android:id="@+id/layout">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/Linear"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>

</HorizontalScrollView>


</LinearLayout>

how can i change this code that there would be no blank space at the end?


Solution

  • finally i got the answer , i must declare the gravity attribute to center_horizontal in parent linear_layout.