Search code examples
androidsyntaxandroid-widget

Importance of "(Button)" in buttonBlue = (Button) findViewById(R.id.buttonInvisibleMan);


I am doing this course on Coursera, and in the 3rd week 1st block video, the instructor has us write the following line of code:

buttonBlue = (Button) findViewById(R.id.buttonInvisibleMan);

What is the significance of each part of this line of code? What does the R.id. stand for? What is the importance of the "(Button)"?

Please explain each part in detail.

Thank you


Solution

  • R.id. stands for the Resource ID which is important because blueButton needs to know which UI element it needs to work with.

    (Button) is a form of casting, by writing (Button) you promise the compiler that this will be a Button element and not TextView or any other UI element.