<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Toth Arpad</title>
    <description>The latest articles on Forem by Toth Arpad (@arpytoth).</description>
    <link>https://forem.com/arpytoth</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F99901%2Fbfd13ab2-3742-4819-b5b2-85c02ddd4378.jpg</url>
      <title>Forem: Toth Arpad</title>
      <link>https://forem.com/arpytoth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arpytoth"/>
    <language>en</language>
    <item>
      <title>How to get Android resource ID by name</title>
      <dc:creator>Toth Arpad</dc:creator>
      <pubDate>Mon, 21 Jan 2019 09:41:53 +0000</pubDate>
      <link>https://forem.com/arpytoth/how-to-get-android-resource-id-by-name-5aa7</link>
      <guid>https://forem.com/arpytoth/how-to-get-android-resource-id-by-name-5aa7</guid>
      <description>&lt;p&gt;You may want to do this if you want to write a framework that relies on the name of the resources and not the actual ID, in order to remove references to the R class. For example if you use &lt;em&gt;R.layout.activity_todo_list&lt;/em&gt; in your class, and you want to move your class in a separate library… you can’t. &lt;strong&gt;R is a generated class&lt;/strong&gt; and is project specific. An easy solution would be to somehow load the &lt;em&gt;R.layout.activity_todo_list&lt;/em&gt; value dynamically at runtime. Luckily, you can do this easily like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;context.resources.getIdentifier("activity_todo_list", "layout", context.packageName)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you can see we have no reference to the R class so this piece of code is project agnostic but: &lt;strong&gt;it’s not safe!&lt;/strong&gt; If the resource is missing from the project this code won’t give you a compilation error so your application may fail at runtime. In my case, the benefits of moving the common code for all my apps in a single library was more important than the fact that for some resources I lose the compilation errors so I’m happy with the trade-off but I don’t advise using this technique if you don’t have a really good reason for it.&lt;/p&gt;

&lt;p&gt;Share this:&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>resources</category>
    </item>
  </channel>
</rss>
