• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How can I use XML Layout ID?

A1B2

Newbie
I know I can set the layout in the program:

Code:
...
LinearLayout ll = new LinearLayout(this);
Button b = new Button(this);
ll.addView(b);
setContentView(ll);
...
But I want adding dynamically components to an XML defined layout.

main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
...
Error:
Code:
LinearLayout xml_layout = new LinearLayout(this);
LinearLayout xml_layout = findViewById(R.id.linear_layout); //Type mismatch: cannot convert from View to LinearLayout
Button b = new Button(this);
xml_layout.addView(b);
setContentView(R.layout.main);
I want to assign the LinearLayout XML variable to a program
variable. findViewById is obviously a wrong method.
How Can I do it?
 
what error is it giving you? it looks correct at a glance...

The code
Code:
LinearLayout xml_layout = findViewById(R.id.linear_layout); //Type mismatch: cannot convert from View to LinearLayout
gives the error

Code:
Type mismatch: cannot convert from View to LinearLayout
 
Oh, you just need to cast it I think.

try:
Code:
View myLinearLayoutView = (View) findViewById ( R.id.my_linear_layout );
 
Oh, you just need to cast it I think.

try:
Code:
View myLinearLayoutView = (View) findViewById ( R.id.my_linear_layout );

If I cast the Layout to a view I can't add components:

Code:
Button b = new Button(this);
View xml_layout = findViewById(R.id.linear_layout);
xml_layout.addView(b); //The method addView(Button) is undefined for the type View
 
Thank you for your tips.

Code:
LinearLayout xml_layout = (LinearLayout) findViewById(R.id.linear_layout); 
//ViewGroup xml_layout = (ViewGroup) findViewById ( R.id.linear_layout );
worked for me to avoid compilation errors.

But now I have a new problem (as well emulator as real device).
If I try to run the program I get the error:

Code:
Sorry!

The application Simple
Memory Game (process com.
memory) has stopped
unexpectedly. Please try again.
If I comment out both lines
Code:
//LinearLayout xml_layout = (LinearLayout) findViewById(R.id.linear_layout); 
//ViewGroup xml_layout = (ViewGroup) findViewById ( R.id.linear_layout );
I don't get the error.

What can be wrong?
 
Check LogCat, there isn't enough info about what error you're getting.

ViewGroup DOES work too, LinearLayout is a subclass of ViewGroup which is a subclass of View which is what is returned by findViewByID. This is pretty standard polymorphism. So it should NOT give you compilation errors.

Anyways, something else is wrong. You may need to be sure you are setting the content view before calling findViewByID but I cant recall off the top of my head.
 
Check LogCat, there isn't enough info about what error you're getting.

ViewGroup DOES work too, LinearLayout is a subclass of ViewGroup which is a subclass of View which is what is returned by findViewByID. This is pretty standard polymorphism. So it should NOT give you compilation errors.

Anyways, something else is wrong. You may need to be sure you are setting the content view before calling findViewByID but I cant recall off the top of my head.

You are right in both regards.

1) LinearLayout is indeed a subclass of ViewGroup, however it's not so much polymorphism as it is inheritance. :P Using the lowest possible node in the inheritance tree, however, is standard OOP coding practice, so I was just correcting you on that... Either way it will work.

2) Yes, a call to setContentView(blah) needs to be made BEFORE you make any calls to findViewById(blah).
 
You may need to be sure you are setting the content view before calling findViewByID
2) Yes, a call to setContentView(blah) needs to be made BEFORE you make any calls to

findViewById(blah).
I used findViewByID before I setted the content view.
I corrected it. Now I can run the program! Thank you!
 
You are right in both regards.

1) LinearLayout is indeed a subclass of ViewGroup, however it's not so much polymorphism as it is inheritance. :P Using the lowest possible node in the inheritance tree, however, is standard OOP coding practice, so I was just correcting you on that... Either way it will work.

2) Yes, a call to setContentView(blah) needs to be made BEFORE you make any calls to findViewById(blah).


I always thought it was the other way around, cast to the most flexible object that still has the features you need.

But yeah, it's inherritance more than polymorphism.

But I guess in his case you might as well cast to what it is, idunno! :)
 
Back
Top Bottom