user3789915
user3789915

Reputation: 71

How to turn anything into a button in Android

I need a way of turning a TableRow into a Button in android. I have tried to set up an onCLickListener() and I have tried nesting a TableRow inside a Button but that just crashes the app.

Edit:

I deleted the android:onCLick="onClick" like you said and that got rid of the crashing but nothing happens when I click the table row.

My code:

tableRow1.setOnClickListener(new OnClickListener() {

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent openInfoTR1 = new Intent("android.intent.action.MENU");
        fromTableRow = 1;
        startActivity(openInfoTR1);


System.out.println("Confirmed click");
    }
});

<TableLayout
        android:id="@+id/tlDisplayTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:id="@+id/trTableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="100"
            android:visibility="invisible"                
            android:clickable="true">

             <TextView
                android:id="@+id/tvDisplayedText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:padding="5dp" 
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:layout_weight="5"/>
         </TableRow>
</TableLayout>

Upvotes: 0

Views: 173

Answers (1)

Ridcully
Ridcully

Reputation: 23655

Every element that inherits from View can have attached an OnClickListener. No need to wrap it inside a button.

However, you'll have to look at how events are propagated through your layout. E.g. if you have clickable elements within your TableRow, the click events will normally be consumed by that elements and will not reach your OnClickListener. There are different ways to intercept or modify that behaviour, but you'd have to post your code to get more specific help.

EDIT:

The exception in your app comes from the line android:onClick="onClick" in your layout file. As you set the onClick listener programmatically, you do not need this. android:onClick="onClick" is a shortcut that would expect a method void onClick(View view) directly within your Activity (not, as you have it, as part of the OnClickListener implementation).

Upvotes: 2

Related Questions