Search code examples
androidwidthhorizontalscrollview

How to get width of a horizontal scroll view


I have a horizontal scroll view, I should learn the width of this scroll view. I had tried all solutions which I found in web, but the result has been the same: 0. The scroll view:

<HorizontalScrollView
    android:id="@+id/hsvMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/black"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/lyt_bultens"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" >

        <TextView
            android:id="@+id/tvBultenMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:lines="2"
            android:text="Bülten bulunamadı."
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:lines="2"
            android:text="Bülten bulunamadı."
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>
</HorizontalScrollView>

I already tried:

toDeltaX = hsv.getChildAt(0).getMeasuredWidth();
toDeltaX = hsv.getChildAt(0).getRight();
toDeltaX = hsv.getChildAt(0).getWidth();
toDeltaX = hsv.getMeasuredWidth();
toDeltaX = hsv.getRight();
toDeltaX = hsv.getWidth();

int index = lytBultens.getChildCount() - 1;
View view = lytBultens.getChildAt(index);
toDeltaX = view.getRight();

Please help.


Solution

  • The Layouts in the oncreate method returns the height and width as zero. You can add a method for getting the height and width.

          @Override
       public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        //Here you can get the size!
    
    }