Dandelion
Dandelion

Reputation: 423

Android tablelayout click listener for cell content

I have a 3x3 gridview with custom linearlayouts within. But I want something like this layout and as I search on the internet it's not possible with gridview because of the column span. I used gridview because of the onClickListener method: when the user click on one of the grid cell, a new activity starts(not the same activity, so it's like a main menu). Is it possible to do in a TableLayout? So if I click on a cell even if it spanned can I call an onClick method? I've searched the web for some solutions, but all of what I've found is clicking on a tablerow(which is not good for me if there is 3 custom layouts in one row).

My layout for the TableLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >

<ImageView
    android:id="@+id/headerPicture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/imageString"
    android:src="@drawable/bicaj" />

<TableLayout
    android:id="@+id/mainGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:numColumns="3" >

    <TableRow
        android:id="@+id/mainFirstRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <LinearLayout
            android:id="@+id/mainOnline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/mainIconOnline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/main_page_icon_descp" />

            <TextView
                android:id="@+id/mainTextOnline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp" />
        </LinearLayout>
    </TableRow>
.
.
.
.
    </TableLayout>
</LinearLayout>

Upvotes: 0

Views: 3448

Answers (1)

Manishika
Manishika

Reputation: 5574

You can add clickListeners to the ImageView

.....
    <LinearLayout
                android:id="@+id/mainOnline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <ImageView
                    android:id="@+id/mainIconOnline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/main_page_icon_descp" />
......

And in your Activity

ImageView butt1 = (ImageView) findViewById(R.id.mainIconOnline);

        butt1.setOnClickListener(this);

@Override
    public void onClick(View v) {
        if (v.getId() == R.id.mainIconOnline) {

            Intent i = new Intent(this, SecondActivityclass);
        startActivity(i);

        }

    }

This should work

Upvotes: 3

Related Questions