Reputation: 59
I am going through Head First Android Development and I am a bit confused with this method --> findViewById(int id)
I have the below button in the file "activity_find_beer.xml" :
<Button
android:id="@+id/find_beer"
android:text="@string/find_beer"
android:onClick="onClickFindBeer" />
and the following code from the class FindBeerActivity.java which is taking the user selected beer and displaying the same in a textview.
public class FindBeerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
//Call when the button gets clicked
public void onClickFindBeer(View view) {
//Get a reference to the TextView
TextView brands = (TextView) findViewById(R.id.brands);
//Get a reference to the Spinner
Spinner color = (Spinner) findViewById(R.id.color);
//Get the selected item in the Spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}
My Question is the method onClickFindBeer(View view) takes a View type of
object as a parameter , but in the xml i have just mentioned
android:onClick="onClickFindBeer"
and when the user clicks the
button , the method onClickFindBeer gets invoked...Who is passing the object of
type View to the onClickFindBeer(View view) ...is it something
implicit ?
Second,on developer.android.com I see that the method findViewById(int id) is both in the Activity class ( https://developer.android.com/reference/android/app/Activity.html ) and also in the View class https://developer.android.com/reference/android/view/View.html ... It's not clear to me which class (Activity or View) findViewById(int id) method is invoked when i call findViewById() from onClickFindBeer(View view){}.
Would be highly obliged if someone could throw light on this.
Regards.
Upvotes: 1
Views: 3040
Reputation: 13785
2.
As I know, main difference is that when you used OnClickListener
from activity it is connected with partivular object such as Textview,Button
find_beer.setOnClickListener
and below code is excuted when someButton is pressed.
While android:onClick = "onClickFindBeer"
is used handle click directly in the view's activity without need to implement any interface
Upvotes: 1
Reputation: 1
Base on your sample above the android:onClick
method is the one being invoked because when invoking a onclick
method in java class, it need to call a onClickListener
.
cause on the other question. as far as I know it belong to the view class because it always to reference an object on your design.
Upvotes: 0
Reputation: 317
You have assigned the method onClickBeer to your button. When the button gets clicked, the object, in this case the button, is passed to the method you assigned to it. A Button is a type of View object, so you have a more generic View object as the parameter, but you are perfectly ok to caste it to a button object.
findViewById is called through a "context", which is a way of getting at system resources. You are asking the system to return to you a specific object, which you can then use. It is worth reading up on contexts.
Hope that answers some of your query.
Upvotes: 0
Reputation: 165
The findViewById() is being called here by the activity. You can see the method declaration by clicking on findViewByID while pressing Ctrl. For a view, you would have to call it using the view. For example,
view.findViewById();
Upvotes: 2
Reputation: 762
View
parameter as that is how it is implemented in a superclass of the Button
class (It is public class Button extends TextView
.). The views you add to XML are actually java classes. When you set a property to such an XML item, that constructs the object from the particular java class accordingly. The onClick
method of the View
class goes as onClick(View v)
. By setting an XML you just asked the Button class to look for the entered method but its signature is always with a View as a paramenter, which refers to the view clicked.findViewById
has to be called on a View group. But the Actyvity class implements it to search an item in view assigned to it by the setContentView()
method.Upvotes: 5