Vektor88
Vektor88

Reputation: 4920

ImageButton "onClick" not called

In my Activity layout I have some ImageButton. They all work but one and I can't understand why onClick isn't triggered when I click on it. I really don't have any ideas...

EDIT There was some ancient forgotten code that was overriding my OnClickListener with an empty one...

This is my xml Layout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:background="#000000"
    android:paddingLeft="10dp"
    android:paddingTop="5dp" >

    <ImageButton
        android:id="@+id/gameselect_button_home"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_home_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_opendir"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_home"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_opendir_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_refresh"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_opendir"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_refresh_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_refreshone"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_refresh"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_refreshone_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_settings"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_refreshone"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_settings_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_grid"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_settings"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_grid_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_list"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_grid"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_list_selector" />

    <ImageButton
        android:id="@+id/gameselect_button_help"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_list"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/btn_help_selector" />

    <Spinner
        android:id="@+id/gameselect_emu_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@+id/gameselect_button_help" />
</RelativeLayout>

And this is the code called inside onResume()

helpBtn = (ImageButton) findViewById(R.id.gameselect_button_help);
helpBtn.setOnClickListener(this);

While this is my simple onClick

@Override
public void onClick(View v) {
    if (v.getId() == R.id.gameselect_button_help) {
        Log.i("Click", "OK");
        showHelpDialog();
    }
}

Upvotes: 1

Views: 424

Answers (3)

Piyush
Piyush

Reputation: 18933

Put this code in onResume() method like this.

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    ImageButton textview = (ImageButton) findViewById(R.id.gameselect_button_help);
    textview.setOnClickListener(this);
    super.onResume();
}

you can show Toast on Click of ImageButton.

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.gameselect_button_help) {
        Log.i("Click", "OK");
        Toast.makeText(getApplicationContext(), "Hello", 5000).show();

    }

}

Working perfectly my side.

Upvotes: 1

T.V.
T.V.

Reputation: 793

In your OnCreate() method use this example code.

Change the ID button for the one you use.

ImageButton helpBtn = (ImageButton) findViewById(R.id.gameselect_button_refresh);

helpBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("CLICK", "click");    
    }
});

Hope it helps.

Upvotes: 1

Harshal Benake
Harshal Benake

Reputation: 2401

Add this code called inside onCreate() and not in on onResume().

helpBtn = (ImageButton) findViewById(R.id.gameselect_button_help);
helpBtn.setOnClickListener(this);

Upvotes: 0

Related Questions