Mab
Mab

Reputation: 412

Android Widget method confusion

Weeks after I decided to look into the android api to probably improve my knowledge of Java. I just realized i have actually been hiding alot from myself.

All I want ask is

Button button=findViewById();

It could be button or any widget or whatsoever(just for illustration), we then call a method of different type on it

button.setOnClickListener()

whereas button doesn't have any such method as setOnClickListener and afterall setOnClickListener is of type view.View. So am kinda lost.

Same as others(e.g image.setBackgroundResource)

Any help would be nice.

Upvotes: 1

Views: 33

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364694

The Button class extends the View class:

   ↳    android.view.View
       ↳    android.widget.TextView
           ↳    android.widget.Button

In the View class is defined the setOnClickListener method.
The Button class has own methods and inherits the methods from the TextView and the View classes like the setOnClickListener.

enter image description here

Upvotes: 1

Related Questions