Droid Genie
Droid Genie

Reputation: 361

Image Button Event Not Working

I had create a simple app where there is a image button.

I want a toast to be worked after clicking the image button.

My problem is that, I can see that image button but while clicking on that imagebutton nothing happens. I can't find any issues with that code. Here is my code.

Here is my xml File

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:src="@drawable/myimage"/>

</RelativeLayout>

ContentMain.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class ContentMain extends AppCompatActivity {
    ImageButton imgButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        imgButton =(ImageButton)findViewById(R.id.imageButton);
        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
            }
        });
    }


}

Upvotes: 1

Views: 130

Answers (4)

Cheese Bread
Cheese Bread

Reputation: 2275

Add to xml:

<ImageButton
android:clickable="true" />

activity:

implements View.OnClickListener

@Override
public void onCreate(Bundle savedInstanceState) {
    ImageButton imgButton =(ImageButton)findViewById(R.id.imageButton);
    imgButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imageButton:
            Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
            break;
    }
}

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You should Debug yourself first

You can use this way

add this in your Xml

 <ImageButton
    android:clickable="true"
    android:onClick="myClick"
    />

Then

in Java class

public void myClick(View v)
{
 Toast.makeText(ContentMain.this,"You download is resumed",Toast.LENGTH_LONG).show();
}

Clean-Rebuild-Run

Upvotes: 1

Icero
Icero

Reputation: 81

It works (I pasted your code to my project), maybe try clear your project.

Upvotes: 0

Robinhiio
Robinhiio

Reputation: 105

I had alot of troubles with functions that uses context in OnClicklisteners try create a method for the toast and then call it from the clicklistener like this:

Private void callToast(){

Insert your function toast here }

ImgButton.setOnClicklistener(new OnClicklistener){

callToast() {

Upvotes: 0

Related Questions