Trying to set the OnClickListner for a button - android

I'm trying to make a custome dialog box, but I'm getting errors when I call OnClickListener and it pass in a OnClickListener() class as the paramter. The error is
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
also it says in error message type view, shouldn't it be Button, could this be the issue?
code
// error message on line below
Button dialogButton = (Button) dialog.findViewById(R.id.action_settings);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});

I guess you have a wrong import
Make sure you have
import android.view.View.OnClickListener;
Also you say
but I'm getting errors when I call setOnClickListener and itpass in a OnClickListener() class as the parameter.
You need to implement the OnClickListener interface. You use a annonymous inner class.
You can do as below
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Example:
http://androidexample.com/Custom_Dialog_-_Android_Example/index.php?view=article_discription&aid=88&aaid=111

Take into account that instead of implementing the onClick method in your activity, you can use the android:onClick attribute on the xml layout where the button is defined.
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="doSomething" />
Then implement the method doSomething in the activity:
public void doSomething(View view) {
// Kabloey
}
For more details:
Button description in Api reference

Related

Android: Handle Onclick Listerner in included layout [duplicate]

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

click event on button of custom DialogPreference

I have created custom Preference dialog from answer of this link.
Now I want click event on that button in my ActivityPreferenceScreen activity class.
How can i achieve this ?
Add the following attribute in your Activity:
OnClickListener prefButtonListener = new OnClickListener() {
public void onClick(View view)
{
Log.v("Btn", "Clicked");
}
};
In the place you instanciate the EditTextPreferenceWithButton class, add this code:
editTextPreferenceWithButton.attachButtonListener(prefButtonListener);
And then, add following in your EditTextPreferenceWithButton class :
public void attachButtonListener(OnClickListener listener) {
this.button.setOnClickListener(listener);
}
}

android eclipse setOnClickListener

I want to set OnClickListener on my button3.
It's an activity in the second tab in TabHost.
import android.content.DialogInterface.OnClickListener;
public class tab_act extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
}
XML:
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:drawableLeft="#drawable/icon_search"
android:drawablePadding="15dip"
android:text="Найти совпадения" />
And I get an err:
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments (new
DialogInterface.OnClickListener(){})
in this line:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
You've imported the wrong OnClickListener - it should be
import android.view.View.OnClickListener;
instead of import android.content.DialogInterface.OnClickListener;. Also you are setting OnClickListener for button with id button1 while xml you provided declares button with id button3
edit It's better to implicitly specify it like so:
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
...
});
to prevent such errors from happening.
You are doing findViewById(R.id.button1) and you should be doing findViewById(R.id.button3)
One more thing: if you imported more than one method from 2 different places (for example you imported and use in the same activity both android.content.DialogInterface.OnClickListener and android.view.View.OnClickListener) you can't use shortcuts for both times when calling the OnClickListener, and you will somethimes have to call specificaly like so:
findViewById(R.id.button1).setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
}
});

Facing problem in implementing OnClickListener on android

I want to implement a click listener for a button on my main view. My code is something like below
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
But shows me error as follows
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
I have no idea what to do. Please help.
You are not using the correct interface to instantiate the mFinishLinstener variable...
It is possible you have an import specifying DialogInterface and that is confusing the view.
Try specifying View.OnClickListener explicitly.
private View.OnClickListener mFinishListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
As per my opinion Best way to implement On click event for the Button.
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
public void selfDestruct(View view) {
// Kabloey
}
Note: The above code is given in Android SDK - Button.
try this code :::
final Button button = (Button) findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Simply try this one as:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something when the button is clicked
}
};
you can also use like below code..
Button button = (Button)findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Write Your code here
}
});
You can also declare the onclick in the xml.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />
And in your code you would define the function as:
public void buttonClick(View view)
{
// handle click
}

How can set onClickListner fo buttons..?

I am new to android. I want to set OnclickListner for different buttons which are located in different xml layouts.
You can also use a definition like that directly in the XML-file:
<Button android:onClick="myClickHandler" />
After that you can create the method "myClickHandler" in your Code like that:
class MyActivity extends Activity {
public void myClickHandler(View target) {
// Do stuff
}
}
Something like this:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Then all you need to do is reference the different buttons by their different ids set in the XML

Categories

Resources