mahesh Rao
mahesh Rao

Reputation: 375

Android - How to match the width of HorizontalScrollView element to screen width?

I have a HorizontalScrollView, with TextViews in it. I wanted to match the width of the TextView's to that of the screen. On trying, I got the following

Each TextView does not match it's width to that of the screen

I tried using android:fillViewport. It helped the TextView's match it's width to that of the screen but it is not able to scroll on doing so.

Here is the xml code:

?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="com.example.Mahesh.practice.test">

    <HorizontalScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/background"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/test"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/test"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:text="Film"
                android:textAlignment="center" />
        </LinearLayout>
    </HorizontalScrollView>
</android.support.constraint.ConstraintLayout>


How could I match the width of the TextView to that of the screen and also allow the ScrollView to scroll ? and why is the screen like as shown in the above image ?

Upvotes: 0

Views: 1056

Answers (1)

Mayank Panchal
Mayank Panchal

Reputation: 365

Set the width of textview programmatically by calculating screen width.

Try this

activity_main.xml

<?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="com.example.Mahesh.practice.test">
<HorizontalScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:background="@color/background"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="0.0">
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/test"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/test"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:text="Film"
            android:textAlignment="center" />
    </LinearLayout>
</HorizontalScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    linearLayout=findViewById(R.id.ll);
    int width=getScreenWidth(Main2Activity.this);

    int childCount=linearLayout.getChildCount();
    for (int i=0;i<childCount;i++){
        TextView textView= (TextView) linearLayout.getChildAt(i);
        textView.setWidth(width);
    }
}
public static int getScreenWidth(Context context) {
    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}
}

Upvotes: 1

Related Questions