I have 3 buttons in a fragment that I want to use the same click event. How can this be achieved within a fragment?
XML
<Button
android:id="#+id/btn_1"
android:onClick="btnClick_DoSomething"
android:text="#string/one"/>
<Button
android:id="#+id/btn_2"
android:onClick="btnClick_DoSomething"
android:text="#string/two"/>
<Button
android:id="#+id/btn_3"
android:onClick="btnClick_DoSomething"
android:text="#string/three"/>
Java
#Override
public void btnClick_DoSomething(View v) {
}
Error
#Override (within the fragment Java class) becomes underlined in red and the following error is returned
Annontations are not allowed here
I want the onClick event to be the same for all 3 buttons
You dont need to write #Override annotation as you are not overriding the method. Just use
public void btnClick_DoSomething(View v) {
}
You will get a callback in this method at runtime.
you simply don't do it in XML, do it in Java instead:
<Button
android:id="#+id/btn_1"
android:text="#string/one"/>
<Button
android:id="#+id/btn_2"
android:text="#string/two"/>
<Button
android:id="#+id/btn_3"
android:text="#string/three"/>
then...
private final OnClickListener onClick = new OnClickListener(){
#Override
public void onClick(View view){
switch(view.getId()){
... cases...
}
}
somewhere initialising the views you do:
fragmentView.findViewById(R.id.btn_1).setOnClickListener(onClick);
fragmentView.findViewById(R.id.btn_2).setOnClickListener(onClick);
fragmentView.findViewById(R.id.btn_3).setOnClickListener(onClick);
}
What method are you trying to override? Do any of the parent classes have a method btnClick_DoSomething? You do not need to override anything when setting a click listener from a Layout XML. Just ensure that a method of the same name with a void return type and a View as its only argument exists in the activity class that will use this layout
The general way to distinguish clicks from different Views in the same onClick handler is to identify them by id
public void btnClick_DoSomething(View v){
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
There are some misunderstandings here. To override a Click event, your Fragment need to implement the View.OnClickListener interface like this:
public class YourFragment extends Fragment implements View.OnClickListener{
#Override
public void onClick(View v) {
}
}
Note that the name of the method must be onCLick, must return void and recives a View as a parameter to be overrided from the interface. In this case you need to set a Listener to each button in your fragment:
btn1 = (Button) view.findViewById(R.id.btn_1);
btn1.setOnClickListener(this);
Inside this method you can control which object was clicked by it´s ID
#Override
public void onClick(View v) {
int id = v.getId();
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
Or, you don't need to override the method, so remove the anotation that will work's fine :
public void btnClick_DoSomething(View v) {
switch(v.getId()){
case R.id.btn_1:
// handle click from button 1
break;
case R.id.btn_2:
// handle click from button 2
break;
case R.id.btn_3:
// handle click from button 3
break;
}
}
Instead of declaring the onClick method in the xml layout file, you could have your Fragment implement View.onClickListener interface, override onClick(), and set all 3 buttons onClickListener like this btn.setOnClickListener(this). Put the behavior you want for all 3 buttons in onClick(). All 3 will have the same behavior (unless you check which button the event came from in onClick()).
Related
I wanted to ask what was the significance of creating a switch statement when implementing the onClickListener interface for multiple buttons. Like, we're already calling the setOnClickListener for that particular button by saying button_name.setOnClickListener()
So what's the point of specifying the id's again in the switch statement ? Why is it necessary to do so ? doesn't the point of doing button_name.setOnClickListener() mean - "do what's in here for this button" ?
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know
// So we will make
switch (v.getId() /*to get clicked view id**/) {
case R.id.corky:
// do something when the corky is clicked
break;
case R.id.corky2:
// do something when the corky2 is clicked
break;
case R.id.corky3:
// do something when the corky3 is clicked
break;
default:
break;
}
}
}
What I'm trying to ask is, doing button2.setOnCLickListener() means - "Do what's set by this function for button2" right ? if not then what is the actual purpose/function performed by setOnClickListener() ?
Since you have set clickListeners to 3 different buttons, the method
....onClick(View v){.....
is going to get called when you click any of those 3 buttons. If you click the button1 you do not want to perform task actually assigned for button3. So to check and find out which button was pressed the switch statement is necessary.
Alternatively you could have done this:
corkyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// do something when the corky is clicked
}
});
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// do something when the play is clicked
}
});
so on and so forth...
This way you don't need a switch statement as in this case you know which button is being clicked on, AS you're setting the clickListener AND ALSO specifiying what you wanna do there itself.
It's more of a clean look, a more readable code style structure. If you have multiple widgets who need click listener, by implementing View.OnClickListener interface
and overriding onClick gives a cleaner look.
and inside onClick(View view){
- you can also use if statement but when the number of widgets is more
then it becomes messy, that's why we use switch statement to look cleaner.
}
and
btn.setOnClickListener(this)
// telling the button to pass the interface
so that overridden onClick can listen to this.
But If you don't want that or you have only one widget for click
then just use this, no need to implement and override.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btn_Login.setOnClickListener(this);
In android studio.
I've seen this in countless places. What does this mean? I know how this operates but what is the listener called then?
This example is from: Link
Suppose that you have 16 Buttons and every button has setOnclicklistener this means that you are creating many repetitions of similar code to this in your class. And that makes your code ugly, also this is not the efficient way to write your code. So to make your code efficient you have to implement OnClickListener() on your activity and then for each button use buttonX.setOnClickListener(this). Now use the override onClick method. In this method, you can use either the switch case block or if-else to identify which button is pressed. So in the onClick method you just have to give ids of the button.
Implement OnClickListener in Activity
public class MyActivity extends Activity implements View.OnClickListener {
}
For each button use this:
buttonX.setOnClickListener(this);
After this implement override the onClick method
#Override
public void onClick(View view) {
switch(view.getId){
case R.id.buttonX:
// Do something
break;
}
in that example its defined like this
private Button btn_Login;
Button is a class
onClickListener is a listener, to set the listener he is using setOnClickListener method.
From the next time if u need to those kind of clarifications don't post in a separate thread, add a comment in that question itself.
Thank You #august alsina
It is a listener that helps to specify the events to occur on click of a widget.
When your class implement View.OnClickListener, you can defined your click of each button in method public void onClick(View v). Keyword this refer to the method onclick. It is good to use this way when there are a lot of button in your class file. You can define following code On Create method:
button1.setOnClickListener(this);
button2.setOnClickListener(this);
and define its definition oncreate method. For example:
public void onClick(View v){
int id= v.getId();
switch (id){
case R.id.btn1: {
//do sth
//break;
}
case R.id.btn2: {
//do sth
//break;
}
...
}
}
In layman's terms
By writing btn_Login.setOnClickListener(this);
whenever btn_login will be pressed program will go to onclick method public void onClick(View v)
and then you can write in the method what you want to do when button is pressed
There is a lot programmatically created buttons, that's 2 of them:
Button agafon_1 = new Button(this);agafon_1.setText(R.string.txt_agafon_1);llPreViewList.addView(agafon_1, lParams);
Button agafon_2 = new Button(this);agafon_2.setText(R.string.txt_agafon_2);llPreViewList.addView(agafon_2, lParams);
There OnClickListener and it has a switch. What do I need to insert in place of the question marks to make the switch work?
switch (???) {
case ???:
//
break;
case ???:
//
break;
}
The file R.java not even mention the id buttons which makes it impossible to use v.getId()...
If you are using the same OnClickListener for every Button set a different id for every Button with Button.setId(int) and the use v.getId() for your switch, where v is the argument of onClick(). Otherwise you can create a different OnClickListener for every button
You class should implement OnClickListener
public class MainActivity extends Activity implements OnClickListener
Then
Button agafon_1 = new Button(this)
agafon_1.setId(yourbuttonid);
agafon_1.setOnClickListener(this);
Then
#Override
public void onClick(View v)
{
switch(v.getId())
{
case buttonid1 : // id must match
break;
case buttonid2 :
break;
...
}
}
Say I have two activities: "A" and "B".
Activity A has a button in it's layout. and I want to set it's click listener implemention on Activity B.
So let's say here's Activity A:
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(B.this);
and in Activity B, I'm trying to implement the function:
public void OnClick(View v)
{
//DO SOMETHING
}
I'm getting the following errors:
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments (A)
No enclosing instance of the type A is accessible in scope
What am I doing wrong here?
The handling of the GUI components must be accompanied within the same UI thread that instantiated that.
So your desired view is not correct also make sure the you can have the click and other listener been worked only if the view is set with that components and is currently visible ( In foreground) for the user to have interaction.
If you really want that then You can override the default implementation of the click listener within the different activities via following:
1)Static Reference: make the button as public static in activity A and use it in Activity B by Class A's name.
2)Interface:implements OnClickListener on the activity A , but will not be accessible in B
3)Custom MyClickListener for all activites.
public class MyClickListener implements OnClickListener {
#Override
public void onClick(View v) {
mContext = v.getContext();
switch (v.getId()) {
case R.id.button:
// Your click even code for all activities
break;
default:
break; }}
}
Use it the class A and B both as shown below:
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new MyClickListener());
You must pass an instance of an OnClickListener to button.setOnClickListener(..). Class A isn't implementing OnClickListener, so you must implement it in order for it to be an instance of an OnClickListener.
class A extends Activity implements OnClickListener {
// instance variable, constructors, etc
#Override
public void onClick(View v) { // note onClick begins with lowercase
// DO SOMETHING
}
}
I don't understand why I have to implement the OnClickListener to use the OnClick-method. Assuming this code:
public class KlickitestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View v) {
// code what happens when a click is made
}
From the class OnClickListener I only use the method onClick(View v) - and this one is overwritten. Why can't I just define the onClick-method without implementing the OnClickListener?
You can. You can do that by using an Anonymous Inner Class :
Button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Do stuff
}});
Button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Do stuff
}});
However implementing an OnClickListener makes it easier to handle events, and improves code readability. i.e You can use one Listener method, and passing a View to handle multiple buttons/listeners with a switch statement, something similar to :
public void onClick(View view){
switch(view.getId()){
case R.id.Button1:
//Stuff for button 1
break;
case R.id.Button2:
//Stuff for button 2
break;
break;
case R.id.Button3:
//Stuff for button 3
break;
}
Just to expand on Mob's answer and also Scott's comment and link...
An Activity is primarily a framework for a UI and as such has no pre-defined way of interacting with a user. As designers / developers we choose which UI components we want the Activity to contain based on the purpose of the Activity.
The UI components such as Buttons, CheckBoxes, ListViews and so on, serve very different purposes and it is not the place of an Activity in it's basic form to know what events those UI elements react to (click, long-click, swipe etc) simply because there is no pre-defined set of UI elements that an Activity will always host. As such it is our responsibility to implement the event handlers (listeners) that we need to use based on how we design the UI.