5

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();                    
                }               
               }  
             });
    }
Community
  • 1
  • 1
rajeev patidar
  • 85
  • 1
  • 1
  • 7

5 Answers5

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

learner
  • 3,092
  • 2
  • 21
  • 33
3

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();
           }
       });
    }
Amir Hosseinzadeh
  • 7,360
  • 4
  • 18
  • 33
1

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
  }

}
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
1

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

Madhu
  • 1,780
  • 23
  • 47
0

Remove android:onClick="onClick" from xml

Fahim
  • 12,198
  • 5
  • 39
  • 57