The question might seem incomprehensible just from the title alone so let me elaborate on what I mean. From what little I've dabbled in HTML and JavaScript, you could add HTML lines to the HTML file from using a Javascript function in the script.js file and it would add those HTML lines you've written into the function to the HTML file on execution and it would work as if you've written it in the HTML file to begin with. That was my understanding of how it worked, at least, if I'm wrong on my assessment feel free to correct me on that matter.
Anyway, I'm wondering if we could do a similar thing in Android Studio where we can use a Kotlin function to add an XML line/attribute/command like 'app:srcCompat="@drawable/whatever"' to an XML file.
Of course the question doesn't come from a mere sense of wonder. I currently have an application with a fragment that's supposed to get some football teams from the Room database and display them in CardViews using RecyclerView. In those cards, the team's name and their logo should be displayed. I don't have logos as image files in the Room database itself, however there is a column that stores the names of the drawable files in which the team logos are stored. (For example: Team A's logo is stored in the drawable's as 'teama.png' and it has 'teama' stored in a column.)
In the Adapter class of the RecyclerView, I want to use the bind() function to put the name and the logo on the cards. What I'm expecting to do (related to my question overall) is using a function that can take a string parameter ("app:srcCompat="@drawable/teama"") and puts it to the XML file of my team item. Is this possible? I'm open to other solutions as well and can post code if requested.
Thank you for your answer beforehand.
CodePudding user response:
Is there a way to add XML code to an XML file from a Kotlin code?
Yes, but not in the context of what you are asking.
What I'm expecting to do (related to my question overall) is using a function that can take a string parameter ("app:srcCompat="@drawable/teama"") and puts it to the XML file of my team item. Is this possible?
No. You cannot modify the content of a resource XML file at runtime.
From what little I've dabbled in HTML and JavaScript, you could add HTML lines to the HTML file from using a Javascript function in the script.js file and it would add those HTML lines you've written into the function to the HTML file on execution and it would work as if you've written it in the HTML file to begin with.
JavaScript, run in the browser, does not modify the HTML file on the server. It modifies the DOM: the parsed representation of the HTML that is used by the browser to render a UI on the screen.
Similarly, in Android, you will need to update the View
objects — created from parsing that resource XML file — to reflect your desired name and logo. This approach is covered in books and courses on Android app development. FWIW, here is a free book of mine on the subject.
CodePudding user response:
Till now this technology is not available and if it is available it's not that famous and in use