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();
}
}
});
}
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.
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.
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.
<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With