2

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.

ismailarilik
  • 2,236
  • 2
  • 26
  • 37

2 Answers2

6

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!

}
Abhishek Agarwal
  • 1,907
  • 12
  • 21
0

It is very simple: try the following Code:

ScrollView myScrollView = (ScrollView) findViewById(R.id.hsvMain);
myScrollView = (FrameLayout.LayoutParams) myScrollView .getLayoutParams();
Float width = myScrollView .width;

Cheers !

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58