setOnClickListener for a button - android

Just getting started with Android development and I can't figure out why this won't work. Here is the error that I am getting (on the last line):
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments (MainActivity)
And here is the code. Seems pretty simple, but I don't see what the problem is. Can anyone help? Thank you!
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
View continue = findViewById(R.id.ContinueBtn);
continue.setOnClickListener(this);
}
}

Try this.
public class MainActivity extends Activity implements OnClickListener
When you pass this object into setOnClickListener then you need to implement OnClickListenere .

you have to do like this
public class MainActivity extends Activity implements OnClickListener {
/// code
}

first of all change the continue to some other name as it is the keyword you can't give a keyword as variable name
implements OnClickListener for your Mainactivity
Button continuea = (Button)findViewById(R.id.ContinueBtn);

Related

Context Argument in Toast.maketext()

this is my first post here. I used the search function and could not find a complete answer,so I hope this is not a redundant question.
I should note that I m really new to coding so maybe I did find an answer but did not realise it.
I have been asked in class to find two different ways to fill the argument in the code below.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(???,"Clicked!", Toast.LENGTH_LONG).show();
the first way i suppose would be toast.makeText(MainActivity.this.getActivity(),....).show();
the second one?
Use the MainActivity context.
Toast.makeText(MainActivity.this,"Clicked!", Toast.LENGTH_LONG).show();
v.getContext() and this both can be used

'this' in setOnCllickListener method

This is not the complete code, just the part where I have a question
I'm wondering if this:
public class MainActivity extends AppCompatActivity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(this);
}
}
Is the same as this:
public class MainActivity extends AppCompatActivity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new MainActivity());
}
}
Sorry if it feels like a stupid question, newbie here :)
No, absolutely not, because in the second case you are creating a new activity with no reference to the first one.
Instead in the first case you are passing the reference of the current instance of the main activity. So go for the first one :-)
When we use this or getAplicationContext() it means you are passing reference and when you use new MainActivity() means you are creating new object of same activity. In this first one is more reliable so use first one.

Why am I not allowed to use setOnClickListener in this situation?

This makes no sense, I am given an error saying that setOnClickListener can't be applied to MainActivity. But i've made other projects where I've never encountered this problem. What's going on?
public class MainActivity extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = ((Button) findViewById(R.id.button));
button.setOnClickListener(this);
}
You have to declare MainActivity like this:
public class MainActivity extends ActionBarActivity implements View.OnClickListener
and after that you have to override the onClick method in MainActivity
#Override
public void onClick(View v) {
//do something...
}
As we can see you havn't implemented the View.OnClickListener on Main activity..
this will be used as MainActivity but the parameter that can be passed is OnClickListener
SideNote: Always try to typecast before you use, this greatly reduce the complications and sometimes takes you to mistakes you are doing.

Test extends Activity implements onClickListener, when/where does the instance of class "Test" created?

In the following code, the class "Test" is extends Acitivity and implements OnClickListener.
but, the "this" refer to the instance of class "Test". There is no "new" to create
a new object to class "Test". So, where/when the instance of Test class created?
public class Test extends Activity implements OnClickListener {
Button playButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.Button01);
playButton.setOnClickListener(this);
}
Thanks for anyone help.
Android framework is instantiating your activities for you when you open new intents, that's why you don't need to do new ActivityClass, all you have to do is declare your activities in your AndroidManifest.
So, where/when the instance of Test class created?
The instance is created 'internally', somewhere between openIntent and onCreate methods.
So, where/when the instance of Test class created?
On calling public void onCreate(Bundle savedInstanceState) method ( in your case) since its main Activity
To make it more clear, lets change a bit your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button) this.findViewById(R.id.Button01);
OnClickListener clickListener = this; // take current instance
playButton.setOnClickListener(clickListener);
}
We register our Activity to listener by passing instance to listen on events. however we don't want that setOnClickListener will see all our methods so they ask us to provide Interface only (OnClickListener).

Best practice to access Activity from Views

I have a question that seems simple but I cannot figure out what is the best practice for that :)
What is the best practice to access from a View, a method on the Activity that launched the View?
For example, I have an Activity with a layout that contains a Button and a Textfield. I want when I click on the Button, to call a method on my Activity that update the Textfield with some value. I come with multiple solutions:
1 - Inner class for the OnClickListener directly on the Activity so I can the method of the Activity with MyActivity.this.updateTextField() on onClick method
2 - Outer class for the OnClickListener, on my onClick method I can do: ((MyActivity)getContext()).updateTextField()
3 - Reference the Activity on my OnClickListener class when I instantiate it:
myButton.setOnClickListener(new MyOnclickListener(MyActivity));
I don´t want solution 1 because I don´t like that much inner class and I want reusable code. Solution 2 seems good but can produce error on runtime if my context is not an activity. Solution 3 seems good also but "heavy".
What is the best practice on Android to tell from the View to its Actitity that something needs to be done on the Activity?
Thanks!
implement activity with onclickListener and add unimplemented method onclick
just check for the view to see which button is clicked incase you are using multiple buttons
Although I mostly find myself end up with inner classes, there are other options.
You can create an interface like the following and let your activity implement it:
public interface UpdateableTextField {
public void updateTextField();
}
Now let the Activities that you want implement this interface.
Now, create a class that implements View.OnclickListener and set the constructor to get UpdateableTextField as a parameter:
public class MyListener implements View.OnclickListener {
UpdateableTextField updatable;
public MyListener(UpdateableTextField updatable) {
this.updateable = updatable;
}
#Override public void onClick(View v) {
// do some stuff
updateable.updateTextField();
}
}
And last, in the Activity:
public class MyActivity extends Activity implementes UpdateableTextField{
#Override public void onCreate(Bundle savedInstanceState) {
// usuall stuff
MyListener listener = new MyListener(this);
someView.setOnClickListener(listener);
// other stuff
}
#Override public void updateTextField() {
// well, update the text field :)
}
}

Categories

Resources