Search code examples
androidbuttonlayerclickable

Android clickable layer


I have set linearlayer to become clickable and want it to act like a button and start a new activity. However i got error. Here are part of the .xml

            <LinearLayout
            android:id="@+id/llproduct1"
            android:layout_width="fill_parent" android:layout_height="wrap_content" 
            android:orientation="vertical" android:clickable="true">
            <ImageView .... />
            <TextView .... />
            </LinearLayout>

This is the .java Button bProduct1 = (Button) findViewById(R.id.llproduct1); bProduct1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.testing.PRODUCTDESCRIPTION"));
        }

What went wrong?


Solution

  • Button bProduct1 = (Button) findViewById(R.id.llproduct1); 
    

    you cannot cast your LinearLayout to a button. But you can do:

     LinearLayout bProduct1 = (LinearLayout) findViewById(R.id.llproduct1); 
     bProduct1.setOnClickListener(...)
    

    see this for reference