Skip to content Skip to sidebar Skip to footer

Why Do I Have To Cast A Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dyp

Solution 1:

You can set a Button to a new button.

But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.

Solution 2:

See In Android You can create the UI Elements in two ways:

1. create UI elements through layouts (.xml) files. And to use them in java class map them to their corresponding class. And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component. And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.

2. crate UI elements through java end. To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.

But But But, According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code. But it is only recommended not compulsory at all. now its up to you.... and i think at starting you feel it different but after some time you will start loving it...

Thats the beauty of android.

Thanks. i hope, i answered your question throughly.

Solution 3:

Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.

Solution 4:

final Button callButton = (Button) findViewById(R.id.callButton);

I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.

There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.

Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().

Solution 5:

You have two options to create View component in android including Button

1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)

2- Create it dynamically in the code e.g. Button button = new Button();

both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code

Dynamic UI creation is useful in many applications that needs to create Viewson-the-fly

Post a Comment for "Why Do I Have To Cast A Button?"