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
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.
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.
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.
You should change your line to:
LayoutInflater.from(context).inflate(R.layout.myfile, null);
You can find it in documentation here.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With