• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Unused parameter in onClick method

The7even

Lurker
Hi guys, I'm new to the forums.

The first thing I did was use the search function and after 10 pages I could not see what I was looking for. So I'm going to give this a shot.

In java, we use method parameters for a number of reasons but I've never used parameters in my methods unless I needed them

When defining a method in android for a button that I want to click, I had my method defined in XML under the Button element

[HIGH]android:onClick="showText"[/HIGH]

then, in my ProjectName.java file I defined the method

[HIGH]public void showText(View view){

}[/HIGH]

The View parameter is never used by me.. I have no need for it.
By asking questions on Stackoverflow I was told that the View in the parameter is the view that was clicked.
Button is a subclass of View as is TextView and since that is the case, that answer sort of makes sense.
But the question I have is.... is this the rule for just the onClick method?
Also, why can I not pass the Button as a parameter? Button is the actual "View" being clicked, so why not Button?

Furthermore, are there other methods similar to this and if so, do they all follow the rule just like the View parameter, where let's say if I had a class called "A" and it had a subclass "B" and that class had a subclass "C", and if I wanted to use the onClick method, I can only pass the Highest Class as the parameter?

To simplify that, are there ever cases where I would pass anything else OTHER THAN "View" as the parameter to these methods?

Thank you.
 
What they told you on stack overflow is correct, but when you use onClick in your XML like that, you are actually using a specific shortcut for the View.OnClickListener.


Basically Android is doing some work for you behind the scenes in order to save you from a few extra lines of code.

Thus it is unlikely you would have a use for this with different methods. The xml attribute android : onClick will ALWAYS apply to a View (and thus, you will always have the view returned to you).

This is actually very handy though. Say you have 2 buttons, you can use the view to get the ID of what was clicked.

[HIGH]
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showText"

/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showText"
/>
[/HIGH]then in your activity

[HIGH]
public void showText(View view)
{
if (view.getId() == R.id.btn1)
{
Log.i(TAG, "Button 1 was clicked!");
}
else if (view.getId() == R.id.btn2)
{
Log.i(TAG, "Button 2 was TOTALLY clicked!");
}
}


[/HIGH]
hth
 
Also the "view" you get, actually is the Button, you would just need to cast it, I think.


[HIGH]
public void showText(View view)
{
Button myButton = (Button) view;
}
[/HIGH]
 
What they told you on stack overflow is correct, but when you use onClick in your XML like that, you are actually using a specific shortcut for the View.OnClickListener.


Basically Android is doing some work for you behind the scenes in order to save you from a few extra lines of code.

Thus it is unlikely you would have a use for this with different methods. The xml attribute android : onClick will ALWAYS apply to a View (and thus, you will always have the view returned to you).

This is actually very handy though. Say you have 2 buttons, you can use the view to get the ID of what was clicked.

[HIGH]
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showText"

/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showText"
/>
[/HIGH]then in your activity

[HIGH]
public void showText(View view)
{
if (view.getId() == R.id.btn1)
{
Log.i(TAG, "Button 1 was clicked!");
}
else if (view.getId() == R.id.btn2)
{
Log.i(TAG, "Button 2 was TOTALLY clicked!");
}
}


[/HIGH]
hth

Thanks very much for the reply. It actually DOES help. In order to get comfortable, I need to know what's going on and you helped.

Just a few things to clarify.

When you said
The xml attribute android : onClick will ALWAYS apply to a View

This is because it is defined under <TextView/> in XML, right?

and lastly..

say I didn't want android to do any of the work for me and wanted to code it by hand my self..

what would this method look like?


Thank's again for your help.
 
This is how you might set those click listeners yourself in java:

(Note I just wrote this up quickly havent tested, the switch statement might be wrong, if it doesnt work, use an if-else)

[HIGH]
package com.example.temp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";
private Button btn2;
private Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
super.onResume();

btn1 = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);

btn1.setOnClickListener(mClickListener);
btn2.setOnClickListener(mClickListener);
}

private View.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {

int id = view.getId();

switch (id){
case R.id.button:
Log.i(TAG, "button 1 clicked");
break;
case R.id.button2:
Log.i(TAG, "button 2 clicked");
break;
default:
break;
}

}
};
}

[/HIGH]Your main XML:
[HIGH]<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/button"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click!!! me!!!"
android:id="@+id/button2"
/>
</LinearLayout>
[/HIGH]
 
Back
Top Bottom