sandalone
sandalone

Reputation: 41759

How to get layout xml file path?

I need to display the path of layout used in setContentView(R.layout.main);. So if the mobile phone is normal-mdpi, I need to display "Path is: /res/layout-normal-mdpi" or if the mobile phone is normal-hdpi, I need a text "Path is: /res/layout-normal-hdpi".

I basically need to know which layout file was loaded and its path.

Upvotes: 0

Views: 3013

Answers (2)

VenomVendor
VenomVendor

Reputation: 15392

You can add a Hidden Text View with corresponding Folder names in the xml
Get the String in the text view by

TextView path = (TextView)findViewbyid(R.id.hiddentextview); 
 String s =  path.gettext().tostring();

Make sure that all the id's of the text view are same Example

if your xml is in `normal-mdpi` in hidden textview hard code `normal-mdpi`
if your xml is in `large-mdpi` in hidden textview hard code `large-mdpi`

Upvotes: 3

Ivan Bartsov
Ivan Bartsov

Reputation: 21066

You can call Activity.getResources().getConfiguration() which will get you current Configuration - see the doc, you can get most of the info you need from there (namely, you'll want orientation and screenLayout), for screen density you'll want Activity.getResources().getDisplayMetrics() and the density field. There's no way to retrieve actual layout path that I know of.

UPD Hmm, take a look at this: LayoutInflater.inflate() - this is how it's working internally, if you dig deeper - it calls through to a native method that does the actual inflation, so no luck there, but maybe if you poke around with the xml parser, you'll find a way to do that. From the top of my head, you can add a comment to every layout and put the file's name into it. And then use Activity.getResources().getLayout(your_layout_id) to get the xml parser, then use the parser to get your comment out - that way you'll know exactly what file you just accessed.

Upvotes: 1

Related Questions