android eclipse setOnClickListener - android

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) {
}
});

Related

what is better way to organize onclicklistener? [duplicate]

This question already has answers here:
Multiple Buttons' OnClickListener() android
(11 answers)
Closed 7 years ago.
When you have many buttons in a view and all the button have listener. Your main activity gets dirty.
Anyone know how to organize listeners ?
Currently I used this way and implement onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
You could set the property:
android:onClick="buttonClicked"
in the xml file for each of those buttons, and use this in the java code:
public void buttonClicked(View view) {
if (view.getId() == R.id.button1) {
// button1 action
} else if (view.getId() == R.id.button2) {
//button2 action
} else if (view.getId() == R.id.button3){
//button3 action
}
}
You can implement onclicklistner for multiple buttons using swith case
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.firstButton:
// do your code
break;
case R.id.secButton:
// do your code
break;
case R.id.thirdButton:
// do your code
break;
......
default:
break;
}
}
Ya...It s the best way to use multiple onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
#Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.spotifyBtn:
intent = new Intent(this, SimpleSingleExample.class);
break;
case R.id.superDuoBtn:
intent = new Intent(this, CustomExample.class);
break;
case R.id.libraryBtn:
intent = new Intent(this, SequenceExample.class);
break;
case R.id.buildItBiggerBtn:
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
break;
}
if(intent!=null){
startActivity(intent);
}
}
If you want better way than you have to use Android Annotations, its simple and useful, you can find here
Add those View object references to some type of list, iterate through it usin a for-each loop, then call the setOnClickListener on each element which will reduce those lines to just 2 lines for you.
ArrayList <View> list = new ArrayList <>(spotify,superDuoBtn,libraryBtn, buildBiggerBtn, capstoneBtn);
for (View view : list) {
view.setOnClickListener(this);
}
The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks. As far as I know, there are four different ways to add listeners for handling button clicks. If you know of other ways, please post a comment and share them with us.
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Inner Class (btn1)" android:id="#+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:text="Anonymous Inner Class (btn2)"
android:id="#+id/Button02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Implementing an Interface (btn3)"
android:id="#+id/Button03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Calling From XML Layout (btn4)"
android:id="#+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn4Listener">
</Button>
</LinearLayout>
in MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//method 1 - uses an inner class named btn1Listener...
Button btn1 = (Button)findViewById(R.id.Button01);
btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener...
Button btn2 = (Button)findViewById(R.id.Button02);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn2 - uses an anonymouse inner class");
}
});
//method 3 - note that this class implements
//the View.OnClickListener interface
//which means that we must implement the onClick()
//method (which you'll find below)..
Button btn3 = (Button)findViewById(R.id.Button03);
btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below
}
//here's the inner class used as a listener for btn1...
private View.OnClickListener btn1Listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
}
};
//here's a method that you must have when your activity implements the
//View.OnClickListener interface...
#Override
public void onClick(View v) {
showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
}
//here's the handler for btn4 (declared in the xml layout file)...
//note: this method only works with android 2.1 (api level 7), it must be public and
//must take a single parameter which is a View
public void btn4Listener(View v) {
showToastMessage("You clicked btn4 - listener was set up in the XML layout");
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}

Trying to set the OnClickListner for a button

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

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

Button setonclicklistener error

I am having a problem right now with setOnClickListener.
When i put this following line:
button.setOnClickListener(this);
And run the application then it does not run and shows a message that "Application closed forcefully".
Could you please help me how I can set button onclick event in Android 2.2?
See if the code below works for you...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();
}
});
Remember to add }); at the end.
another possible reason ( happened to me ) is your activity must implement OnClickListener
public class MainActivity extends Activity implements OnClickListener ...
For defining button click event in android,
You can try the below code:
public class Main_Activity extends Activity {
private Button myButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.Button01);
myButton.setOnClickListener(new Button_Clicker());
}
class Button_Clicker implements Button.OnClickListener
{
#Override
public void onClick(View v) {
if(v==myButton)
{
Toast.makeText(v.getContext(), "Hello!! button Clicked", Toast.LENGTH_SHORT).show();
}
}
}
}
Although it's been a long time, thought it might help others who have this problem, it took me many trials to get it right. But i think what finally solved my problem was setting the clickable attribute of a button in the layout's xml to true.
Code sample:
<Button android:text="Button" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:clickable="true">
</Button>
Further, if you would've looked at the DDMS perspective, you would've seen that the cause of the error was NullPointerException, which ofcourse was showing because clickable wasn't set. Correct me if i am wrong.
Type View.onClickListener instead of Button on ClickListener
Check if in the class definition there is implements OnClickListener

Categories

Resources