Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create View-Object from XML file in android

I only want to get an object from a xml layout file without having to implement it into the current layout.

I know the way with

LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true); 

but after execution of the above the layout will be implemented and shown immediately inside my "myparent"-View, right? I only want to get the Object itself to get its attributes and such. And maybe (but only maybe) insert it later into the shown layout. Is that possible?

Regards

like image 628
Matmarbon Avatar asked Dec 12 '11 21:12

Matmarbon


People also ask

How do you add a view in XML?

Android View Class Constructors To create a new View instance from Kotlin code, it needs the Activity context. To create a new View instance from XML. To create a new view instance from XML with a style from theme attribute. To create a new view instance from XML with a style from theme attribute and/or style resource.

What is view in Android XML?

A View occupies a rectangular area on the screen and is responsible for drawing and event handling. Views are used for Drawing Shapes like Circles,Rectangles,Ovals etc . Just Use View with background and apply a Shape using Custom Drawable.

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.


2 Answers

You should change your line to:

LayoutInflater.from(context).inflate(R.layout.myfile, null); 

You can find it in documentation here.

like image 146
inazaruk Avatar answered Oct 05 '22 23:10

inazaruk


LayoutInflater.from(context).inflate(R.layout.myfile, myparent, true); 

The end parameter determines whether or not to automatically add the new view to myparent. Turn it to false to still use the parent's layout attributes.

Or, if you don't care about the parent's layout params, follow @inazaruk's answer

like image 42
FunkTheMonk Avatar answered Oct 05 '22 23:10

FunkTheMonk