krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
01/11/14 06:24 AM (11 years ago)

Calling different xml layout files - Interactive Quiz (Android)

I am creating different xml layout files within the same app to cater for different devices - notably tablets. I am saving the xml files within new folders e.g. Res/layout-w720dp as per the advice from http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts But how and where do I call the use of these xml files? Is it within the java source code (screen quiz file) using some kind of if-else statement? Does the manifest file need to be altered also? Your help much appreciated.
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
01/11/14 09:17 AM (11 years ago)
yep, exactly right. If you look at the onCreateView method (or onCreate for 2.0 apps), you'll see something like this: //inflate the layout file for this screen... View thisScreensView = inflater.inflate(R.layout.bt_screen_quiz, container, false); If you notice, it inflates (or opens the file for viewing) the XML file "bt_screen_quiz", located in the "R.layout" folder ("R" stands for "res"). So, if you wanted to decide between 2 XML files, one for large and one for small devices, you could do something like this: View thisScreensView = inflater.inflate(R.layout.bt_screen_quiz, container, false); if (isLargeDevice) thisScreensView = inflater.inflate(R.layout.bt_screen_quiz_large, container, false);
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
01/11/14 09:30 AM (11 years ago)
Thanks @Chris1. I'm on the right track then. I'll have more than 2 xml files, so will have to expand your statement to cover all options. Some thoughts - please correct if i'm heading in the wrong direction ... The 'LargeDevice' is established in the BT_device.java file, which uses some deprecated language (getWidth) so this may need updating manually. Would I be right to assume the BT quiz uses width in pixels rather than dp? If so, then I'll need to update this to establish the device dp width (not px) and take it from there ...
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
01/11/14 09:45 AM (11 years ago)
Yep - device width (I think on the appDelegate actually) is based on width in pixels. Not always the best formula depending on what you're trying to do.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.