0

I have a string that i get from asynctask, i would like to set that string as title of menu item in actionbar.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        this.menu = menu;

        if (menu!= null) {
               menu.findItem(R.id.azzera).setVisible(false);
            }

        MenuItem giornataItem = menu.findItem(R.id.giornata);
        giornataItem.setTitle(giornataTitle);

        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view

        MenuItem giornataItem = menu.findItem(R.id.giornata);
        giornataItem.setTitle(giornataTitle);

        return super.onPrepareOptionsMenu(menu);
    }

OnPrepareOptionsMenu works fine, when i change fragment or drawernav is open the title is still correct, but onCreateOptionsMenu does not set the title on application start because in that moment the string is null, because it's called before asynctask. How can i set that string as title?

1 Answer 1

2

call asynctask while create menu

    String tital=new Asynctask().execute("").get();

    MenuItem giornataItem = menu.findItem(R.id.giornata);
    giornataItem.setTitle(tital);

if you are talking about making a string to tital of your activity

then call

 String tital=new Asynctask().execute("").get();
 setTitle(tital);

 setContentView(R.layout.MainActivity);
Sign up to request clarification or add additional context in comments.

3 Comments

Still does not work. I'm talking about making a string as title of menu item in actionbar @raj
then show your activity code where you creating actionbar
Calling invalidateOptionsMenu() in onPostTaskExecute of AsyncTask did the trick, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.