Access to global variable in menu XML file in Android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m creating an app in which an user must connect himself. I m trying to implement a menu, in which the first item text would be the name of the connected user and once clicked on, it would bring to the user profile. To store the connected user information, I created my own subclass of android.app.Application, which contains, amongst others, the connected user:

private User currUser;

public User getCurrUser() {
    return this.currUser;
}

public void setCurrUser(User currUser) {
    this.currUser = currUser;
}

I was wondering how can I access to this global variable, in my menu XML file, so that the title of the first item would be the connected user name? Logically, I would do something like that:

<item android:id="@+id/action_profil" android:title=(MyApp)this.getApplication().getCurrUser().getM_UserName()
    android:orderInCategory="100" app:showAsAction="never" />

But I know this is not doable in XML. How would I do this? Perhaps, I should re-title the item somewhere else? I found a solution where I change the title in the onCreateOptionsMenu event of the activity, but is there a better way than having to change the item title in every activity containing the menu?

  Edit - Here s my XML file   
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.aramjeremie.liftm8.HomeActivity">

    <item android:id="@+id/action_profil" android:title="Mon_Nom"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_home" android:title="@string/action_home"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_notifications" android:title="@string/action_notifications"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_passenger" android:title="@string/action_passenger"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_driver" android:title="@string/action_driver"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_search" android:title="@string/action_search"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/action_signOut" android:title="@string/action_signOut"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

Thanks.

Answers

Define subclass of Activity and override onPrepareOptionsMenu to set the profile item s title to the current user s username.

public class MyActivity extends Activity {

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        CharSequence currentUser = ((MyApp) this.getApplication()).getCurrUser().getM_UserName();
        MenuItem profileMenuItem = menu.findItem(R.id.action_profil);
        if(profileMenuItem != null) {
           profileMenuItem.setTitle(currentUser);
        }
        return super.onPrepareOptionsMenu(menu);
    }
}

Every activity that has this menu should then extend MyActivity.

If you re using the compatibility library, MyActivity should extend FragmentActivity or ActionBarActivity.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/28530868/access-to-global-variable-in-menu-xml-file-in-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils