Here's the situation: I have an activity that dynamically generates a bunch of randomized custom imagebuttons and adds them to TableRows, in a TableView, in my xml. This activity also has a method that I want to call when one/any of these buttons is clicked. The buttons have variables inside them; the method gets these variables and sets them into a TextView (in the same activity) so I figure all the buttons can use this one method. If these buttons were defined in the XML I would just use android:onClick="displayCell" to specify the method, but they aren't. Is there a way to just set onClick for these buttons as I'm generating them in the activity or do I have to use
button.setOnClickListener(new OnClickListener(){....});
and go through a bunch of hassle as I've seen in some of the answers around here? The problem I have with that is that I can't seem to call my method from inside onClick because the argument of the method (the button) is not final (I'm making a bunch of 'button' in a loop so I don't think it can be):
button.setOnClickListener(new OnClickListener(){
public void onClick(View q){
button.getActivity().displayCell(button);//I want to do something like this but this obviously doesn't work
}
});
You can have the Activity implement OnClickListener and then (assuming you are in the activity):
button.setOnClickListener(this);
Yes as comodoro states, or make your onClickLIstener a member variable of your class, don't do a "new" on each button.
private OnClickListener mOnClickListener = new OnClickListener() {...};
and when creating your buttons:
button.setOnClickListener(mOnClickListener);
The onClick() function in your listener will be passed the View of the button itself. You can access the buttons variables, etc, from this function.
public void onClick(View v)
{
ImageButton button = (ImageButton)v;
// and access your button data via button object...
}
A solution to this could be :
Create different instances of buttons .(So you can make them final)
Use setId() method to give them an integer ID (to refer to them later).You can store the ID's in an a Listto refer them later on.
Define their onClickListeners right after you create it.
Try using a class that inherits from button and add there the OnClickListener. Like this:
class MyButton extends Button {
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
displayCell(v);
}
};
}
Related
I am a .NET developer who is in the beginning stage in Android development. Please explain me the code below.
nine=(Button)findViewById(R.id.b9);
nine.setOnClickListener(this);
nine = (Button) findViewById(R.id.b9);
Look for the view called b9, cast it to button, and assign it to your variable.
nine.setOnClickListener(this);
Add your class as a listener of clicks on that button if we are implementing the OnClickListener interface otherwise we have to create new OnClickListener using like this.
nine.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
P.S: This is not the way to learn Android.
nine=(Button)findViewById(R.id.b9);
It search the element by id (R.id.b9) and cast it to button and assign that value to the variable nine.
nine.setOnClickListener(this);
variable nine is button type variable so set the OnClickListener on this to fire some event after clicking on it.
and this activity must implement OnClickListener
public class a extends Activity implements OnClickListener {
}
beginner here.
First off, this question arrived while i was trying to implement button clicks using android:onClick in xml and referencing a method.
Now, when you reference a method, the parameter of the method in the activity must be "(View)". Quick question, what is the variable after the word View in the method parameter? Usually it's like "(View v)" or "(View view)". What is the second variable in the parameter, can it be anything? When is it used? Just want general info about it, couldn't really find such specific info anywhere.
Thanks in advance
I think you misunderstood. The first parameter in (View view) is the type of the parameter, and the second is the temporary name supplied to it (it is just a dummy name, so you can use whatever you like). For example, if I have to pass an integer as parameter, I would use (int i), where i is understood to be of type int. So, in your case an object of type View is temporarily called view to be passed as parameter into a function.
This is reference to the view you are clicking. Take a look at the question here, and you may find when to use it.
So you are asking about onClick()?
This method is from View.onClickListener interface, you can see the document in http://developer.android.com/reference/android/view/View.OnClickListener.html
Because it is an interface, so there will be only paramater :View, it represents the view you just clicked. for example, if you set a button into the interface, it means button, if you set a ImageView into the interface, it means the ImageView itself;
For example:
YourActiivty extends Activity implements View.OnClickListener {
public void onCreate(Bundle onSaveInstance) {
super.onCreate(onSaveInstance);
setContentView(R.layout.yourlayout);
//your button, we assume id is R.id.yourbutton;
Button yourbutton = (Button) findViewById(R.id.yourbutton);
yourbutton.setOnClickListener(this);
//your imageview, we assume id is R.id.yourimageview;
ImageView yourImageView = (ImageView) findViewById(R.id.yourimageview);
yourImageView.setClickable(true);
yourImageView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.yourbutton) {
//your button is clicked!
} else if(id == R.id.yourimageview) {
//your imageview is clicked!
}
}
}
I want to create a variable number of buttons, and I want them to be adjacent (I cannot use the ListView), so I know I have to inflate a Button somehow. My question is how to inflate the Button, and for each of the created buttons, how to create a listener?
Thanks
I'm a little confused as to what you're asking, but to inflate a button, in your activity, call:
Button myButton = (Button)findViewById(R.id.layout_xml_name);
Then, to add a listener:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your buttons desired actions
}
});
I'm trying to make multiple OnClickListener methods for 5 buttons in my program, and I've been able to declare them, and I made a switch using the xml id of what was clicked, but I need a parameter for the setOnClickListener method when I call it, and all that will work is null. I have also tried passing in this, so the method has context.
Here's some of the code:
add.setOnClickListener(null);
sub.setOnClickListener(null);
mult.setOnClickListener(null);
div.setOnClickListener(null);
equal.setOnClickListener(null);
The parameter has to be an instance of some object that implements the OnClickListener interface. One way to do it is to use an anonymous inner class:
add.setOnClickListener(new OnClickListener{
public void onClick(View view){
//your event handler code here
}
});
another way is to make your class implement OnClickListener --do that by changing your declaration to look like:
public class MyActivity extends Activity implements OnClickListener{
then define an implementation for the onClick method:
public void onClick(View view){
if(view == add){
//handle add button click
}else if (view == sub){
//handle sub button click
}
//etc
}
then to install the listener you could do:
add.setOnClickListener(this);
You are supposed to pass View.OnClickListener to this function, which is a listener that will get called once the button is clicked.
To do that, you can either:
Declare this listener in the layout XML, with the button, as specified in Button 4 in this site.
Create an instance of View.OnClickListener and pass it to setOnClickListener method as in the example below (Taken from android site which is a great source):
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
Since View.OnClickListener is an interface, your activity may implement it as well, and be itself the listener, in this case, you will pass the activity instance(this) to the setOnClickListener method, but this is just one option, and not that recommended IMHO.
Coming from a .NET background, I struggled with the same thing at first. It's just a different syntax than .NET as java doesn't support properties, or events like I was used to. Here's a simple example of how to do this using a class level click listener variable...
#Override
private void onCreate(Bundle bundle) {
myButton.setOnClickListener(this.genericButtonListener);
}
private OnClickListener genericButtonListener = new OnClickListener() {
#Override
public void onClick(View v) {
//v represents your button
}
};
You need a concrete class here. For example:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code here
}
});
If you look at documentation, you will notice it takes View.OnClickListener as parameter. If you need five separate listeners, which you are not going to use anywhere else, you can pass an anonymous class implementing onClick(View v), like this
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//do required actions here
}
});
I have a list view with 2 buttons on each row.
I am using a cursoradpater to populate the list.
I am also using the view holder pattern on newview() bindview().
My questions are:
where do i put the clicklisteners for the buttons knowing that the action for the button is different from the action of the list item itself?
Do i keep the onListItemClick ?
You do not need the onListItemClick
You can try binding for each of your button an event in the adapter
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
but probably this won't work on the list item, so you need a new aproach as is this described in the button documentation.
However, 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
}
The View passed into the method is a reference to the widget that was clicked.