Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set the onClick event in a ImageButton?

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();                    
                }               
               }  
             });
    }
like image 852
rajeev patidar Avatar asked Mar 16 '15 06:03

rajeev patidar


People also ask

How do I make a clickable image button?

To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

What is onClick event in android Studio?

Onclick in XML layoutWhen the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

What is the difference between onClick and OnClickListener?

View. OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.


1 Answers

<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.

like image 196
learner Avatar answered Sep 29 '22 23:09

learner