1

Im learning android development and they give an example with this syntax:

Button button = (Button) findViewById(R.id.button_id);

The 'Button' word tells the compiler to create an object from the class Button. The syntax to the rigth of the equal sign confuses me. The code is creating an object, but the rigth side of it doesn't seem to create an object.

I've searched and found that there are some ways to create an object: What are all the different ways to create an object in Java? but none of them uses the syntax in the example.

The one that is similar is this one:

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

but it uses the keyword "Class.forName"...

So, could someone explain to me exactly what each part of the code means? Is "findViewById" part of java or its like a 'macro' for the compiler to link the button object to the screen?

I'm a little bit confused with some concepts. I would like to understand this interaction between java code and SDK code.

Community
  • 1
  • 1
Galvani
  • 11
  • 1

4 Answers4

2

This does not create an object.

findViewById(R.id.button_id); is a perfectly normal method call. It calls the method findViewById, and passes it the value of R.id.button_id. findViewById returns a reference to the view (in this case a button).

(Button) is a cast. (Button)something does the following:

  • If something is not a reference to a Button object, then throw a ClassCastException.
  • Otherwise, let you use it as a Button reference.


Without the cast, this:

Button b = findViewById(R.id.button_id);

would not compile, because what if findViewById actually returned a reference to a TextBox?

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Nice explanation, thanks by telling me about that cast thing, I understood it :) – Galvani Mar 09 '15 at 02:01
  • Could you explain me whats R? Is this a file? I can't find enough information. – Galvani Mar 09 '15 at 02:02
  • @Galvani It's just a class created by the Android SDK, which holds IDs in conveniently named static fields. You can find R.java somewhere in your project and open it, it's just another class (except that it's generated automatically by the SDK). – user253751 Mar 09 '15 at 03:19
0

The syntax to the rigth of the equal sign confuses me. The code is creating an object, but the rigth side of it doesn't seem to create an object.

The right side is the invocation of a method, named findViewById(). This method is implemented on a Java class, named Activity, and your code (presumably) resides in a subclass of Activity. The findViewById() method returns an object. Many methods in Java return objects.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But how does the link between an object in the code and an object in the XML (the screen) happens? Is there a place where I can learn this? – Galvani Mar 08 '15 at 23:12
  • @Galvani: The act of calling `setContentView(R.layout.whatever_you_called_it)` reads in the layout XML, parses it, and creates the corresponding widget Java objects for you. `findViewById()` looks in those Java objects for the widget you ask for by ID. "Is there a place where I can learn this?" -- well, it is covered to varying degrees in [the Android documentation](http://developer.android.com/guide/topics/ui/declaring-layout.html), but if you are not familiar with Java methods returning objects, you should focus on learning Java first. – CommonsWare Mar 08 '15 at 23:16
  • ok, but why I need (Button) before the call? Shouldn't the type of obect to be returned by findViewById be already defined as a button? – Galvani Mar 09 '15 at 01:03
  • @Galvani: That is referred to [a cast](https://howtoprogramwithjava.com/java-cast/) (or a type cast). – CommonsWare Mar 09 '15 at 10:36
0

findViewById is used to get a reference of a button that is defined in the view xml after the layout has been inflated so that changes can be made during runtime. This button would be defined in the xml for the activity located at res/layout/Activity.xml (where Activity.xml is your activity that is being loaded)

The second line of code instantiates an object of class subin.rnd.MyObject and then casts it to a MyObject.

Burrito
  • 517
  • 3
  • 8
0

All widgets are subclasses of the View class. Basically, you are grabbing the element according to the identifier in XML and casting it to a Button.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70