Android ImageButton not firing the onclick event, what is wrong with my code?

Posted by kimo on Stack Overflow See other posts from Stack Overflow or by kimo
Published on 2012-11-10T22:56:37Z Indexed on 2012/11/10 22:59 UTC
Read the original article Hit count: 182

Filed under:

I have very simple code that includes ImageButton with OnClickListener that points to another Activity, the click on the ImageButton doesn't fire the onClick (The same problem was with Button) :

public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tools);
    this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
    this.btnCamera.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(context,MainActivity.class);
            startActivity(intent);  
        }
    }); 
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_tools, menu);
    return true;  
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

  <ImageButton
      android:id="@+id/cameraButton"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:background="@drawable/btncamera"
      android:contentDescription="@string/desc" />

© Stack Overflow or respective owner

Related posts about android