Issue
Can I work somehow with layouts from resources folder in java? I have a layout in the resorces folder which contains a RelativeLayout which contains a LinearLayout, and any TextViews. And I want to create something like a class instance, and change in example the RelativeLayout background and the texts in the TextViews. Because then I want to convert this to a Bitmap (for which I have already written the function). And I don't want it to show up. All this in a static function. It's possible? How do I create a something like variable/class? Sorry for any spelling or composition errors I am not a native speaker.
Solution
fun getLayout(layoutId: Int, context: Context) : ViewGroup {
return LayoutInflater.from(context).inflate(layoutId, null, false) as ViewGroup
}
This is how you would get a Layout from the resources directory programmatically. Using it like so:
val myLayout = getLayout(R.layout.activity_main, context)
Changing the background
myLayout.setBackgroundColor(Color.RED)
Changing some text
myLayout.findViewById<TextView>(R.id.my_text_view).setText("Hello World")
Answered By - Blundell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.