1

I need to add TextViews in Horizontal ScrollView and on scroll I have to check which textview is in the middle of the center if three textView are showing on the screen? Note: I have added horizontal scrollView and TextViews.Here is my code.

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/catagorybar"
        android:orientation="horizontal" >

        <TextView 
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
        <TextView 
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
        <TextView 
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
        <TextView 
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
        <TextView 
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />
        <TextView 
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            />

    </LinearLayout>
</HorizontalScrollView>
Robin Royal
  • 1,788
  • 1
  • 18
  • 29

2 Answers2

0

Suggest you use Gallery - A view that shows items in a center-locked, horizontally scrolling list. and set your yourGallery:

yourGallery.setOnItemSelectedListener(new OnItemSelectedListener(){
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
        //get focused/center view position here
});

Here is one sample available, but in the gallery adapter, in getView, you should inflate your custom view instead of just ImageView.

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
0

You can get if the TextView partially/totally visible or not using the way described here:

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (text.getLocalVisibleRect(scrollBounds)) {
    // Any portion of the TextView, even a single pixel, is within the visible window
} else {
    // NONE of the TextView is within the visible window
}

So, you have to get the HorizontalScrollView children and loop through it, then see what are the visible children and from that you can get the centered child:

    List<Integer> centered_textViews = new ArrayList<Integer>();
    TextView text;
    HorizontalScrollView scrollView = findViewById(R.id.your_hsv);
    Rect scrollBounds = new Rect();
    scrollView.getHitRect(scrollBounds);
    for (int i = 0; i < scrollView.getChildCount(); i++) {
        text = (TextView) scrollView.getChildAt(i);
        if (text.getLocalVisibleRect(scrollBounds))
            centered_textViews.add(i);
    }
    int centered_textview_index = (int) (centered_textViews.size() / 2);
    TextView centered_text = (TextView) (scrollView
            .getChildAt(centered_textViews.get(centered_textview_index)));
Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118