Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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" />

like image 282
kimo Avatar asked Oct 27 '25 11:10

kimo


1 Answers

I can't see anything wrong with the code. If it works with a regular button my guess is that you maybe have to set android:clickable="true" in the xml (you can also do it in code).

like image 83
Warpzit Avatar answered Oct 29 '25 00:10

Warpzit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!