nenito
nenito

Reputation: 426

How to return the index of child in Layout?

I have the following situation - HorizontalScrollView (HSV), inside HSV - LinearLayout and inside it number of buttons - the code is here:

<HorizontalScrollView
        android:id="@+id/Footer"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:background="@drawable/footer_bg"
        android:scrollbars="none"
        android:fadingEdge="none"
        >

        <LinearLayout
            android:id="@+id/hsvLinearLayout"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <Button
                android:id="@+id/today"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/today"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/today"
                android:onClick="getRSSNews"
                />

            <Button
                android:id="@+id/life"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/life"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/life"
                android:onClick="getRSSNews"
                />

            <Button
                android:id="@+id/corner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/corner"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/corner"
                android:onClick="getRSSNews"
                />

            <Button
                android:id="@+id/banks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/banks"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/banks"
                android:onClick="getRSSNews"
                />

            <Button
                android:id="@+id/it"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/it"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/it"
                android:onClick="getRSSNews"
                />

            <Button
                android:id="@+id/fun"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/fun"
                android:drawablePadding="0sp"
                android:gravity="center_horizontal|bottom"
                android:textSize="8sp"
                android:text="@string/fun"
                android:onClick="getRSSNews"
                />

        </LinearLayout>

    </HorizontalScrollView>

Is there a way to get the position(index) of the button, on which has been clicked!

Upvotes: 24

Views: 22504

Answers (2)

Pooja M. Bohora
Pooja M. Bohora

Reputation: 1331

You may give tag to each view and check the tag when that view is clicked.This may help you.

Upvotes: 6

xevincent
xevincent

Reputation: 3734

indexOfChild returns the position in the group of the specified child view.

Upvotes: 82

Related Questions