I've got three alias resource files supporting different sizes and orientations with the same, but different layout names e.g
<resources>
<item name="main_act_res" type="layout">@layout/main_act_h</item>
</resources>
<resources>
<item name="main_act_res" type="layout">@layout/main_act_v</item>
</resources>
Each layout has a FrameLayout container for a fragment that must know which layout is currently displayed in order to fetch an image with the correct orientation.
The code I use in fragment will always get an alias name (main_act_res), instead of the layout name (e.g main_act_h)
val layoutName = activity?.resources?.getResourceEntryName(R.layout.main_act_res)
I tried getting resource id first and then name, but I'm getting "Unable to find resource ID #0xffffffff" exceptlion error.
val layoutId = activity?.findViewById<View>(android.R.id.content)?.rootView?.id
val layoutName = layoutId?.let { activity?.resources?.getResourceEntryName(it) }
What is the correct way to get the currently displayed activity layout in this case?
Thanks to Mike M. suggestion in the comment, I was able to retrieve the name of the current activity layout with the following code:
val out = TypedValue()
activity?.resources?.getValue(R.layout.main_act_res, out, true)
val layoutId = out.resourceId
layoutName = activity?.resources?.getResourceEntryName(layoutId)
User contributions licensed under CC BY-SA 3.0