I've learn that casting is a way to convert variable and object into something else then I come across a line a code in a android programming book :
Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
I tried to break it down;
Button is an object
buttonPlay is the object variable
= assignment operator
(Button) is an argument taken from findview by id... -> here I get mess up
I mean casting object should be like this example:
Graphics comp = new Graphics();
Graphics2D comp2D = (Graphics2D) comp; -> by the way I know nothing about graphic object yet.
I know that (R.id.buttonPlay) is a reference to the designer button but simply put in this manner get confusing
So I wonder is this casting or not?
and why the button object don't have an access spacefire like private for example?
Thanks for the help!!
Post a Reply
|Oldest Newest Rating|
· December 18, 2015
findViewById(R.id.buttonPlay) returns a reference to a generic Object with the id you give it.
(Button) casts it to a Button widget object which is what you nee to match the Button on the left side.
And buttonPlay is now a reference to the widget with the id.
· December 19, 2015
ok thank, so there are casting object after all.
Java / Android Development
Very popular language used to create desktop applications, website applets, and Android apps.
|Bucky Roberts Administrator|