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.

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Nice explanation, thanks by telling me about that cast thing, I understood it :)
Could you explain me whats R? Is this a file? I can't find enough information.
@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).
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.

4 Comments

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: 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, but if you are not familiar with Java methods returning objects, you should focus on learning Java first.
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: That is referred to a cast (or a type cast).
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.