8

I am working on application where themes and strings differs when user is male or female. I could control colors depending on user gender but now i can't solve problem of strings for example if user is a male, I will use

<string name="welcome">Welcome boy</string>

but if user female it will be

<string name="welcome">Welcome girl</string>

that is just for example.so how can i load the correct file from resources depending on gender value which is stored in variable ??

Sarah Sami
  • 577
  • 1
  • 6
  • 20
  • 1
    You will have to add some logic to your activity or fragment to control this. Can you post the code for the activity or fragment which holds this? – Zerp Jul 29 '15 at 21:03
  • "welcome" key is used in TextView in xml file – Sarah Sami Jul 29 '15 at 21:19

3 Answers3

4

There's no direct support for what you are looking for in the plaform, so you need to write the logic yourself and have two strings per entry (i.e. welcome_m for males and welcome_f for females). But you need to set it on your widgets from code depending of who is using your app.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
2

Define array for gender-specific expressions, for example:

<string-array name="welcome">
    <item>Welcome Boy</item>
    <item>Welcome Girl</item>
</string-array>

Define for yourself the [0] item is male and [1] is female

Amir Hossein Ghasemi
  • 20,623
  • 10
  • 57
  • 53
1

You have to create 2 resources id for your string

<string name="welcome_boy">Welcome boy</string>

<string name="welcome_girl">Welcome girl</string>

if (isBoy())
    txtWelcome.setText(getResources().getString(R.id.welcome_boy));
else 
    txtWelcome.setText(getResources().getString(R.id.welcome_girl));

Is that what you want?

Banana droid
  • 690
  • 1
  • 9
  • 27