krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
01/16/14 04:40 PM (11 years ago)

Inflating different XML files for different devices - Interactive quiz (android)

Further to an earlier thread ... I am trying to inflate different XML files to work with different sized devices, notably tablets and low/med res smartphones. ~Line 250 of the screen_quiz.java file we have the BT code //Original BT - inflate this screens layout file... LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View thisScreensView = vi.inflate(R.layout.screen_quiz, null); which inflates the original screen_quiz.xml layout file. I have replaced the above lines with an IF - ELSE IF - ELSE statement, to take account of the additional XML files that I have created. See Dropbox screenshot which shows 13 identical errors that now follow this new statement. All stating "thisScreensView cannot be resolved". <a href="https://www.dropbox.com/sh/9hrv1kkw4a8ymwo/wAiyX1bjwS" target="_blank" rel="nofollow">https://www.dropbox.com/sh/9hrv1kkw4a8ymwo/wAiyX1bjwS</a> Any ideas? Receding hair is receding further.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
01/16/14 07:19 PM (11 years ago)
How did you add the other XML files? Similar to iOS/Xcode, if you created the files outside of Eclipse, you need to 'drop' them into the project (in your layout directory) and have the Eclipse/ADT environment 'recognize' that the new XML files are there. If you're not doing this, I would suggest something along the lines of: LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View thisScreensView; if (conditional statement) { thisScreensView = vi.inflate(R.layout.screen_quiz, null); } else if (another conditional statement) { thisScreensView = vi.inflate(R.layout.someOtherXml, null); } else { something else… } Cheers! -- Smug Looking at your image, this should fix you up. The reason it 'cant be resolved' is you're trying to declare a View 'within' the if statement, and use it 'outside' the if statement. Declaring the view at 'the same level' as the other statments will cure this ill. You should never declare the same thing more than once (in the same class). If you have to, you're doing it wrong. ;) Also, LOVE Wallace and Grommet :)
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
01/17/14 02:30 AM (11 years ago)
Smug, thank you - you are a legend. The View needed to be declared outside the IF statement. Now you have said it - it seems obvious. But it is only obvious after you have said it. Red errors removed, so on with some emulator testing to see if it will inflate the appropriate files. Bristol is home of Aardman Animation creators of W&G. The photo is of "What a Wind Up" - one of 80 different Gromits forming an arts trail across the city last summer. They were eventually sold at charity auction for £2.3M. http://www.gromitunleashed.org.uk/
 

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.