android: xml attribute android:onClick, why the method needs View parameter - android

When I create an android:onClick attribute in the activity's xml, the method defined by onClick needs to have the View parameter, why View?
i.e.
onClick method in my activity----------> public void sayHello(View v){...}
consider the next code, I'm not using the view variable, but I still need to pass it in method, how come?:
public void onClick(View view){
TextView t= new TextView(this);
t=(TextView)this.findViewById(R.id.textView2);
t.setText("new text");
}

So you know which View is calling the method.
It's like implementing the OnClickListener for you activity, the method created is onClick(View v) (or arg0 depending on your Eclipse), defining it from xml is just specifying a sort of listener for the View, and the method from the listener as that argument.
Once you're in the method, you can do a switch for the id of the button, to perform different actions:
public void myOnClickMethod(View v){
switch(v.getId()){
case R.id.button1:
//Do something for button 1
break;
case R.id.button2:
//Do something for button 2
break;
}
}
In short. Android just implements the OnClickListener for you when you define the android:onClick="myOnClickMethod" attribute.

Before answering the question I would like to mention what a view is...
Android app contains activities which are like screens which further contains GUI elements(such as buttons). In simple words, those GUI elements are called views.
Answer for your question...
Just imagine a situation in which your activity has more than one button (Let's consider it to be 5) and on getting clicked those buttons make a call to the same function (namely onClick()).So, how would the program know which button was clicked. The answer is simple just pass a view parameter to the onClick() function which will allow access to the information regarding the button that was clicked.

Related

How can a widely used custom button track which class its onclick is called from?

In my application, I have a custom button that is used in many different custom views.
The behavior differs very slightly depending on which view the button was clicked in, I want to have one #OnClick implementation in my custom button class that takes care of every case in order to avoid having similar code in every custom view class.
Is there a way I can determine in the #OnClick block where the button was clicked from?
It looks something like
public class customButton extends appcompactimagebutton {
//stuff
#OnClick
public void onbuttonclick(){
//handles general behavior
//if (clicked from customViewA) {
//do A stuff
//}
}
}
and then I have customViewA that has a customButton within it, I'm not sure what to put in the if statement, if that's even a proper way to handle something like this
You can use the tag attribute. In your XML layouts, add a line
android:tag="viewname"
to the XML for the button (in code you can do .setTag("viewname"). You can then just modify your onClick to check the tag using this.getTag() and use that to identify where the click came from.

Event Handling in Android (setOnClickListener)

I'm new to Java and Android Development in General. I found a sample which uses the following statement.
sButton.setOnClickListener(this);
in the onCreate() method of the Activity which implements View.OnClickListener where sButton is a Button variable. As far as I understand this register the on click event handler
Later in the sample
public void onClick(View v) {
if(v.getId() == R.id.button_s)
{
//some work
}
this happens.
My question is if it uses the 'this' keyword from inside the activity shouldn't it pass an object of class Activity? If that happens then the Button ID would never match.
I know there are other methods to implement the Button click.
I have a bit of experience with C# and Windows Phone. The procedure there was that the methods were called for the respective buttons without any need for registering them.
Also what is the difference between event handlers and listeners?
Any help would be appreciated! Thanks
The OnClickListener (in this case the Activity instance) is used to declare the behaviour of your application when it consumes click event. However, your application's Main (UI) Thread registers all (UI) events and it will dispatch the appropriate View object as a parameter to your onClick(View v) function.
The snippet you provided - sButton.setOnClickListener(this) simply instructs your application to use the implemented OnClickListener inside your Activity implementation to respond to user clicks. It doesn't forward the this instance as the parameter to the onClick() function, Android OS does that.
In conclusion: The View v parameter in your onClick(View v) function will correspond to the View that the user clicked, regardless of the OnClickListener that has been attached to that View
EDIT: This is (perhaps outdated but) Android's source-code for the performClick() method of the View class. As you can see, inside that method it calls the mOnClickListener.onClick(this) if there's a listener attached, and that's how the view that has been clicked is forwarded to the onClick() method of the appropriate onClickListener object.
One is below
Button btn_stop=(Button) findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
}
});
Second is through XML android:onClick="doClick"
below is code for activity
public void doClick(View v)
{
}
When you do this
sButton.setOnClickListener(this);
this is indeed an instance of Activity. But it is declared as
MyActivity extends Activity implements View.OnClickListener
The important part is that it implements the interface that setOnClickListener takes as argument. This is why it works, this is treated as an OnClickListener, regardless of being an Activity or not.
As for the difference between event handlers and listeners, see this question.

Android - Handling a grid

I am trying to make a 2x2 grid of buttons and handle them. Right now I have a relative view activity with four buttons...but my question is: is the best way to do this? Than give each button a listener? Or is there anyway to add the buttons to the GridView and handle them all in one method?
Ex.:
Instead of using something like
if(button1x1)...
if(button1x2)...
if(button2x2)...
if(button2x1)...
and write a method for all of them, is there a way for me to just have one method and it will automatically detect which button is being pushed? Sorry if this is a confusing question, I can think it perfectly but translating to words is a bit difficult. Thanks for any help!
First of you you can do
public class YourActivity extends Activity implements OnClickListener {...
and then implement the onClick method as
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.A_UI_Element:
//do what you need for this element
break:
case R.id.A_Different_UI_Element:
//do what you need for this element
break;
//continue with cases for each element you want to be clickable
}
}

OnClickListener for many views

I have done some onclick listeners before in earlier projects but i have have never done like 100-200 onclicks. The idea is a horizontal scroll that AddImagViews to it if a variable is == somethin and if you click the imageview the imageview will setText to an TextView.
I have done the set imageview part but is there any easier way then creating like 100 different onclick for each imageview. Sorry if this was bad explained and difficult to understand.
Sure, have your Activity implemenent OnClickListener something like:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.splash_startScan:
// do something
break;
case R.id.splash_startReview:
// do something
break;
}
}
then for each button do
button.setOnClickListener(this);
You can create a generic onClick event that reacts to the view that is passed to it. You can use a decision statement (like an if statement) to determine the button, but if all you need is the text from the button you can get that generically by casting the view back into a button and getting its text.
Button button = (Button)v;
button.getText().toString();
Might be best to create a class that implements onClickListener and has a constructor that passes in what you need and then just set the listener to a new instance of that object with the right parameters.

Android, Bunch of imagebuttons

I am a learning via a book so please forgive this newbie question.
I have a bunch of imageButtons in my xml, here is how one of them looks:
<ImageButton android:src="#drawable/level1" android:layout_width="wrap_content"
android:id="#+id/imageButton1" android:layout_height="wrap_content"
android:onClick="button_clicked1"></ImageButton>
and processing code:
public void button_clicked1(View v) {
text1.setText("clicked");
}
rather than have each button have its separate onClick code, is there anyway I can pass which button was clicked? for example button_clicked(1) and then button_clicked(2) instead of button_clicked1 like it is now (in the above example xml code)
or i have no choice but have to do it separately?
Kind of - what I like to do is make my View or Activity implement View.OnClickListener.
public class MyView extends ImageButton implements OnClickListener
Then during onCreate, I do something like:
((ImageButton)findViewById(R.id.imageButton1)).setOnClickListener(this);
then, in my onclick:
public void onClick(View view){
switch(view.getId()){
case R.id.imageButton1:
// do something.
break;
case R.id.imageButton2:
// do somethign else.
break;
}
Of course, you can definitely get creative and toss the switch statement if any of your buttons should trigger the same event behavior. Also, I'm not in a place where I can easily view my droid references so there may be an OnClickListener specific to ImageButton - if so, implement that on your containing View or Activity to consolidate the handlers...
Hope that makes sense - happy coding!
B

Categories

Resources