0

well my problem is that i want to create two strings.xml each contaniing different text and i want to read these texts from different activities but am getting 1 error(HELP ME PLEASE).By the way i tried to create a new resourse file named strings2.xml...here is the error:

Error:Execution failed for task ':app:mergeDebugResources'.

[string/action_settings] C:\Users\DELL\AndroidStudioProjects\TestScrollView\app\src\main\res\values\strings.xml [string/action_settings] C:\Users\DELL\AndroidStudioProjects\TestScrollView\app\src\main\res\values\strings2.xml: Error: Duplicate resources [string/app_name] C:\Users\DELL\AndroidStudioProjects\TestScrollView\app\src\main\res\values\strings.xml [string/app_name] C:\Users\DELL\AndroidStudioProjects\TestScrollView\app\src\main\res\values\strings2.xml: Error: Duplicate resources

akshay
  • 1
  • 1
  • 2

1 Answers1

0

If it says that action_settings or app_name are defined in both, it pretty sure is. :-) Why do you need two string XMLs in the first place? No matter what activity you call them from, all strings can go easily into one. If you want to differentiate the strings (not for the compiler but for yourself), use different something_ prefixes.

To put it into perspective, an activity is more or less like a window in other operating systems. You have one application that can have several pages or windows or activities, whichever you name it. Your resources (strings, layouts, drawables, menus, etc) belong to the application, not to one of the activities. So, from any activity in the application, you simply refer to the same strings, same resources. Inside an activity there is a helper function that allows you to get a string directly, without calling it through getResources() because it's such an often needed operation but your program does the same in both cases:

getResources().getString(R.string.xxx)
getString(R.string.xxx)
getResources().getDrawable(R.drawable.xxx)

So, if you have a string called actions_settings:

<resources xmlns:tools="http://schemas.android.com/tools">
  ...
  <string name="actions_settings">...........</string>
  ...
</resources>

you just refer to it with:

getString(R.string.actions_settings)

no matter which of your app's activities you're currently in.

Gábor
  • 9,466
  • 3
  • 65
  • 79
  • can i get a preview of how can we put all the strings i want in only one Xml.file?? i mean some sort of codes... – akshay Oct 10 '15 at 14:33
  • Well, that hardly needs code. :-) You just say `getString(R.string.xxxxx)` in your activitiy. You don't have to specify *where* it comes from, just how you named it. – Gábor Oct 10 '15 at 14:55
  • Worked perfectly! thank u sir :) By the way i have an other question..how can i do this:Suppose i have a button( say bradpitt) and when i click on it i get another button again in an other activity(say biography)and when i click on it i get a description text of him(string)...is it possible to do it?? if yes how..if no how to deal with it?? thnks :) – akshay Oct 11 '15 at 06:15