0

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.

sandalone
  • 41,141
  • 63
  • 222
  • 338

2 Answers2

3

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`
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • what is `path`? If I use static class `Path`, I can do nothing. – sandalone Jul 03 '12 at 16:44
  • Yes, this is the trick I am using right now. It hard as you have to manually hardcode the text into each TextFile. It seems that this is the only solution so far – sandalone Jul 04 '12 at 14:56
1

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.

Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59