1

I would like to include the UserName in the AppBar label / title area at the top of the layout. To my understanding res\values\strings.xml is a great place to put some constant values like the AppName. So I have

<resources>
<string name="app_name">MyFirstApp</string>...

In the manifest I have @string/app_name for the Label field.

So how do I go about getting the username added to the label?

I thought about creating a second string res constant named str_UserName. The value would be blank until authentication then programatically update str_UserName after succesful login. The trouble I have there is concatenating in the manifest. It doesn't like any combination I come up with. (for example @string/app_name + @string/str_UserName or @string/app_name + str_UserName etc.)

Then Googling I read up on string arrays. But in my attempts to use a string array it apears to not properly work for the manifest. I'm assuming because you can't just reference the array without and indexer for which element to display??

So in the end what I want is for the App Name to be left justified and preferrably use a constant value and the username be right justified and of course dynamic based upon the user.

TIA JB

GPGVM
  • 5,515
  • 10
  • 56
  • 97
  • 1
    You can't change the AndroidManifest.xml contents or another XML files of your application at runtime, but you can change the title bar text as described in this answer: http://stackoverflow.com/questions/3438276/change-title-bar-text-in-android – Egor Mar 20 '12 at 22:07

2 Answers2

2

The getString method takes parameters, and works similarly to the String.format method. This is not very often used, but it is documented and works fine. So you can do something like that:

<string name="my_custom_title">MyFirstApp for user %s</string>

And, once you have your username:

setTitle(getString(R.string.my_custom_title, username));

Source: The Context API

Guillaume
  • 22,694
  • 14
  • 56
  • 70
  • 1
    one of your `)` betrayed you and moved to the wrong place: `setTitle(getString(R.string.my_custom_title, username));` – zapl Mar 20 '12 at 22:26
  • Nice to know as well. Does it also support the string.format such as {1,8:yyyy} would == 1990? I'll read the reference next so don't worry if the answer is in there. – GPGVM Mar 20 '12 at 22:28
  • @zapl, true, thanks! fixed now. These damned ) have a life of their own, I swear! – Guillaume Mar 21 '12 at 07:54
  • @user1278561 yes, it's just wrapping `String.format(getString(), args)` so you can use any thing that – zapl Mar 21 '12 at 18:40
1

You can't change values / strings in res/values/ and you can't concatenated them either.

But it's not required. Once you have the username:

setTitle(getString(R.string.app_name) + " " + username);

and the title will change.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • I played around with both methods but in the end went with this for now as all I'm trying to do at this point is get feet wet with Android development. I know long term you woudn't want something like the username stored in the strings.xml. setTitle(getString(R.string.app_name) + " ::: " + getString(R.string.str_UserName)); – GPGVM Mar 21 '12 at 17:19