String Resources in Android Development

Tim Buchalka
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 807K+ students
4.5 instructor rating • 10 courses • 812,028 students

Learn more from the full course

Android Java Masterclass - Become an App Developer

Improve your career options by learning Android app Development. Master Android Studio and build your first app today

60:07:47 of on-demand video • Updated July 2020

  • You’re taught step by step HOW to build Android 7 apps for Google’s Nougat platform that will work on older Android versions.
  • With each comprehensive step, the WHY you’re doing it is explained.
  • You’ll have EXPERT LEVEL knowledge of the Java programming language and know exactly how each process of Android Nougat development works.
  • The course is continually UPDATED, so you’ll learn the very latest as Android Nougat evolves.
  • NEW CONTENT is always being added, and you're covered with full lifetime access to the course.
  • SUPPORT is mind blowing – questions you have are answered that day.
  • The EXPERTISE to be an Android Nougat app developer as taught by the best.
  • The skills you’ll learn are in HIGH DEMAND. You’ve learned to program like an expert. Go get that job!
English so we've now got a pretty cool looking app for displaying flickr photos but it does have one serious shortcoming can only display photos that are tag with android and nougat now that's a bit boring so the last bit of functionality we need to provide is a search feature to allow users to search on different tags now if you recall we've already created the search activity so the next few videos were going to have a look at the android search view and see how we can use that to allow users to search for different photos now the first step is to change the main menu to include a search option so what we're going to do is close down this run windows and we're going to open the project window and we're going to open our res menu folder go to res up here which is already open and what I'm gonna do is create a new menu I'm going to open this folder and what we want to do is double click this menu_main.xml that's because we're going to add another menu option to it now as you can see at the moment the menu only has a single item for settings and we're not actually using that so we could delete it but a setting menu a settings menu is very common though i'm actually going to leave it but we will do is add another menu item by dragging it from the pallete and dropping it below settings so come up here to menu item just drag it as we've done before put in there below we need to specify an ID so that we can tell which menu items was chosen so I'm going to come to ID and will call this one action_search now the title if you recall the title property is the text that will appear with the menus expanded you can see that on the screen at the moment so this is actually a good time to start looking at string resources now you may have noticed that we had some warnings about hard-coded strings when we created the browser and content_photo_details layouts so let's go and look at one now we're going to open the browser XML which is in the layout folder so I'm going to open that make a space just open up to design view you can see this little red square that appears whenever theirs errors or warnings i'm going to click on that to see the warnings and we can see that we've got 2 their the first one is actually saying the hard-coded string text view should use @string resource now we don't actually display the text view so we can ignore that one that's because the correct text is provided by the title of the photo when this layout is use what it means though is that the text shouldn't really be hard-coded into layouts or into code for that matter now there's a couple of reasons for this the main reason is that by hard-coding it makes it very hard to translate the app into different languages but by using a string resource all the text is in one place and android can use a different resource file for different languages and the cool thing here is that Android will do this automatically if you provided a string resource for the users language that's pretty cool you still have to do the translation and create the alternate resource files but that's a lot easier than modifying all that text in the app and distributing different language versions of it so what we're going to do now is go through and move all our texts into a string resource so you can see how it's done but will actually start with this second warning and you can see there the warnings talking about content description and its a missing content description attribute on the image the content descriptions used by people who can't see the screen and use screen reading software to read about or read out what's appearing on the screen while the apps running so it's pretty cool feature that allows partially sighted people to use computers and mobile devices Android studio is encouraging us to provide a description of the image so that the screen reader can actually read out the description so let's provide some text for the content description and at the same time see how to store text into a string resource instead of hard-coding it so I'm going to come back here and i'm going to actually click on the image you can see here the content description field that's where we're going to put it in there but i'm not actually going to type as text in the property rather what I'm going to do is come over here and click on the ellipse that'll open up the resources dialogue and we haven't actually already stored these texts anywhere so if we click on over here the add new resource and click on new string value that enables to actually store that's so when i click on that and it's asking now for a resource name and a resource value so the resource name we're going to go with browse.... ....and for the resource value what we ultimately wanted to show on the screen will call notice how the files names automatically selected and it's actually clicked on the values folder so we don't need to anything their other than clicking on ok once i do that you can see that the content description property's been updated its got this @ string which is referring or referencing that string.xml file followed by the actual name that we assigned so basically you're recognizes this as a normal way of referring to a resource in this case it's a string which we haven't really talked about before string resource and we can now go actually have a look and see where that's been stored so we can open up our project pane and go into the values folder and open up the strings xml double clicking it and you can see the entry on line 6 was the one that's just been updated downloaded flickr photo is the value and browse_image under the description is the actual name that's been assigned and you can see there we've also got the app name and the titles of our activities stored here as well now the wizard did that automatically when we created the activities remember that we change the default title for the photo details activity and also for the search activity so that's the first reason for doing it but the second reason for doing it is to allow text to be reused and that can make things more consistent across activities when you refer to the same thing more than once and it also saves memory because the same string doesn't have to be stored repeatedly now although we're not actually really bothered about that text view we talked about briefly in browse.xml lets actually clear the warning anyway now the obvious way to do that would be to create a string resource for the text but we don't really want to use the string text view anywhere the only reason we left it in so you can see where the text view widget is on the design so if we just closes this actually I just go back over to the browse so i'm talking about this text view here obviously we're not actually using that text anywhere and it's just they're showing us where the actual text view is on the screen to show you what I mean I'm just going to delete the text view and now makes it hard to see where the text view is without selecting in the component tree obvious if i do that is very hard to figure out where it is until I actually go ahead and actually click it but having text in the app just so we can see where widget it is isn't an ideal and fortunately android studio provides another solution so when i select the text view in the component tree you can see their is actually two properties for the second one's got a spanner or wrench in front of it as you can see over here that property use the tools namespace and hence the icon of a wrench there and that won't get compiled into the app so any property that uses that tools namespace is just for use by the android studio tools such as this layout designer what that means i can put some text in there and it will show up in the designer but it won't actually be included in the finish app and for this particular case that's perfect for us i'm going to come in here into the second one second text property the one with the wrench i'm going to type in... can see the screen updated with the empty text view text and what I might do i just to make some space here close down the project pane I will temporarily close down the pallet for now just so we have more space so you can see we've now got an empty text view there the text is showing and again the tools text property won't be part of your compiled app but it is a useful way to check out things will look without risking having sample text in the finish app perfect in the situation now we can see the text view in the design and also the other thing is no longer any warnings about string resources so we actually got no errors anymore so that's browse xml let's have a look at the warnings in content_photos_ _details you can see we're actually we got five errors at the moment and you can see these warnings are about the three texts view widgets and we're not worried about them either because the text should never appear on the screen but we can clear the warnings by putting the text into the tools properties as we've seen and there's actually a quicker way to do that and i'm going to show you how to do so we're going to select one the text views and notice the text view is actually appearing in the text property so all you really need to do is click on that once and then hold the mouse button down over it and you can actually drag it into the next area the second text property the one with the wrench and release your mouse so you can see that move the text view text but I've actually found it necessary to click back into the normal text property after you do that to prevent android studio from text back in there by clicking in their while its blank that seem to get android studio to record the fact that the text has been deleted just to show you that if i click on the second text view and actually click it again once and hold my mouse button down and drag and if I actually click away and come back to the second text view again notice how the text has been duplicated at the top so it seems to need that extra click just to actually update it so do the third one now the photo tags and i'll just click it once dragged release click back in their just to confirm that it's been moved and been deleted and we should find our errors have been reduced and press enter to accept the change and we only got two errors left or two warnings left now theirs a warning there about the missing content description of the image view as well so we're gonna use the same resource that we created for the content description so let's go ahead and just do that to clear that one click on the actual one we want which is browser_image description click on ok you can see now we only got the one warning left now just make sure the other thing that's important to do is just make sure you actually clicked on the right field before you actually went to update it so I'm not sure if I click on the image before orR not correctly but that's what we need to do and that now leaves only one warning left and warning here is that this scroll layout or its linear layout parent is useless just open this up so we can read more now android studio is trying to prevent us from nesting layout too deeply but when we read the details of the warning down the bottom here it clearly only applies to a layout with children that has no siblings is not a scrollview or a root layout this is a scrollview and we need to scroll the card view when we view large images or photos with a lot of texts in the tags so we can safely ignore that warning ok so there's something else we can do with string resources we can have a look at that by starting with looking at the warnings in one of the java files i'm going to go over to the java code open that up again and the file we want to look at is photo details activity i'm going to double-click that to open it and come over here and look at some warnings over here you can see it says do not concatenate text display with set text we've got a similar error down here as well and we click on more it's suggesting we use a string resource instead of hard-coding the text and that's fine we know how to do that now but the first warning we can see on the screen here is talking about using a resource string with placeholders you can see it says they're placeholders so let's actually see how placeholders work and this will also let us see how to access string resources in code rather than layouts let's open up our strings.xml file and just in case you haven't got it open remembering its in the values folder under res so we need to create a new string resource called photo_title_text we're going to do in here by typing it manually... ..... .... here percent s is a placeholder that can be replaced with an actual value when the apps running now the s here specifies that it can be replaced with a string and if we used a d instead of an s so %d instead of %s that could be replaced with a decimal number and that's for title so lets actually add one for tags.... .... lets go back now to photos detail activity and look at how to use these resources in code so what I'm going to do first is commented out these two with the warnings we're going to add some code so we are going to start by putting this code after the code that starts with if photo is not equal to null actually under the text view for the first photo go to the next line we're going to type.... .... I've done this long hand so you can see what's going on i'm going to through to each line and will do the same thing in a shorter and more usual form for the photo tags so we start of on line 24 by calling get resources now the next line defines a string called text and uses the resources get string method to get the string with the ID photo_title_text from the strings.xml resource file now the get string method will replace any place holders with the actual value which we get by retrieving the title field of our photo objects so the gets string method takes care of replacing the placeholders with the values that we specify there's only one placeholder to replace here but you can replace several values at once with a single call to the get string method if you actually needed to so it's the same mechanism as the string. format method that we used in the top ten downloader app to display well whether we were showing the top 10 or top 25 results i'm going to finish the video here now in the next video we're going to do the same thing to the tags but this time we're going to do it in a much more concise manner rather than storing all the intermediate values let's work on that in the next video