How set the onClick event in a ImageButton?

50,310

Solution 1

<ImageButton
        android:id="@+id/search"
        android:layout_width="40dp"
        android:layout_height="41dp"
        android:layout_x="312dp"
        android:layout_y="10dp"
        android:clickable="true"
        android:onClick="onClick"
        android:background="@drawable/search" />

As per your xml you should remove android:onClick="onClick" line from your Image button Xml.Then it will work because 1st priority will go for always onClick method what you mentioned in xml.

Solution 2

in XML File (for example activity_main.xml)

<ImageButton
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"
...
/>

in Java File (for example activity_main.java)

    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       // initiate and perform click event on button's
       ImageButton search = (ImageButton)findViewById(R.id.search);
       search.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(getApplicationContext(),"search button is Clicked", Toast.LENGTH_LONG).show();
           }
       });
    }

Solution 3

Try to implement Onclick listener to your activity

 <ImageButton
        android:id="@+id/search"
        android:layout_width="40dp"
        android:layout_height="41dp"
        android:layout_x="312dp"
        android:layout_y="10dp"
        android:clickable="true"
        android:onClick="onClick"
        android:background="@drawable/search" />
    />

No issue with this button

Change your activity as like below

   ***search.java***
    public class SearchData extends Activity implements OnClickListener{
    ImageButton search; 

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search_data);
            search =(ImageButton)findViewById(R.id.search);
            search.setOnClickListener(this);

  }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.search:
                  try {
                        ConnectService();
                    } catch (Exception e) {

                        e.printStackTrace();                    
                    }              
            break;
        }
    }

}

Hope it will work. if you have any doubt to implement this let me know

Solution 4

You need something like this:

in your XML see the value of android:onClick and android:id:

<ImageButton
    android:id="@+id/search"
    android:layout_width="40dp"
    android:layout_height="41dp"
    android:layout_x="312dp"
    android:layout_y="10dp"
    android:clickable="true"
    android:onClick="myFunctionName"
    android:background="@drawable/search" />

In your file.java See the name of function myFunctionName and the actions when the event is fired into the case case R.id.search:

public class SearchData extends Activity {
ImageButton search; 

  public void myFunctionName(View view) {
    switch (view.getId()) {
      case R.id.search:
        try {
          ConnectService();
        } catch (Exception e) {
          e.printStackTrace();                    
        } 
     break;
    }
  }

  protected void onCreate(Bundle savedInstanceState) {
           // code
  }

}

Solution 5

Remove android:onClick="onClick" from xml

Share:
50,310
rajeev patidar
Author by

rajeev patidar

Updated on April 17, 2020

Comments

  • rajeev patidar
    rajeev patidar about 4 years

    I have created one image button in android, but when I am clicking on that button nothing is happening. I have set all the properties but still nothing is happening. So can you help me that where I am wrong.

    xml file

        <?xml version="1.0" encoding="utf-8"?>
        <AbsoluteLayout
            android:id="@+id/widget39"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android">
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_x="0dp"
            android:layout_y="3dp" />
    
        <ImageButton
            android:id="@+id/search"
            android:layout_width="40dp"
            android:layout_height="41dp"
            android:layout_x="312dp"
            android:layout_y="10dp"
            android:clickable="true"
            android:onClick="onClick"
            android:background="@drawable/search" />
    
        />
        </AbsoluteLayout>
    

    search.java

    public class SearchData extends Activity {
    ImageButton search; 
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search_data);
            search =(ImageButton)findViewById(R.id.search);
            search.setOnClickListener(new View.OnClickListener()   {             
                   public void onClick(View v)  {               
                    try {
                        ConnectService();
                    } catch (Exception e) {
                        e.printStackTrace();                    
                    }               
                   }  
                 });
        }
    
    • Adrian Cid Almaguer
      Adrian Cid Almaguer about 9 years
      you got some advance? I edit my answer
  • rajeev patidar
    rajeev patidar about 9 years
    when i am writing your solution than i am getting error that "add unimplemented methods".
  • Adrian Cid Almaguer
    Adrian Cid Almaguer about 9 years
    @rajeevpatidar just make a function in your code with the same name of the value of android:onClick="myFunctionName" in this case public void myFunctionName(View view) { }
  • Lochana Ragupathy
    Lochana Ragupathy about 9 years
    you have given a good solution by noticing where the issue actually is.