0

I am creating application where in top menu bar i created a wallet icon with balance on it! i retrieve balance from my prefrence!! But whenever i get a new update in balance from server, i need to update the balance in menu icon! How could i achieve it?

I am using firebase and i am able to get data from server to my activity and can save in preference! But cant update the menu icon unless i click the 3 dots! or change in activity! How could i update menu without any click or anything

my Menu code is:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Topup.this, "Clicked",Toast.LENGTH_LONG).show();
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Topup.this,BankActivity.class);
                startActivity(intent);
            }
        });
        return super.onPrepareOptionsMenu(menu);

    }

I also need to update menu on onResume function also

UPDATE Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu

UPDATE

I used the another function according to the answer from the link:

private void updateMenuTitles() {
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
    }

OnResume function i updates the amount value from server and updated my amount to reference and data is added to preference

Onresume function

@Override


       public void onResume(){
            super.onResume();

            getAmount(); //Updates amount to preference variable wallet_amount here
            updateMenuTitles(); //Calling this function to update the textview in menu
            invalidateOptionsMenu();

        }

The application force closes at updateMenuTitles

Menu XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:clickable="true"
    style="@android:style/Widget.ActionButton">

    <ImageView
        android:id="@+id/hotlist_bell"
        android:src="@drawable/wallet"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:layout_margin="0dp"
        android:contentDescription="bell"
        />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hotlist_hot"
        android:layout_width="wrap_content"
        android:minWidth="20sp"
        android:textSize="16sp"
        android:textColor="#ffffffff"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1221"
        android:layout_alignTop="@id/hotlist_bell"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="-3dp"
        android:paddingBottom="4dp"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:background="@drawable/rounded_square"
        android:paddingTop="4dp"
        android:textStyle="normal|bold" />
</RelativeLayout>

Menu:

<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/badge"
        android:actionLayout="@layout/feed_update_count"
        android:icon="@drawable/shape_notification"
        android:title=""
        app:showAsAction="always" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_transaction"
        android:orderInCategory="100"
        android:title="@string/action_transactions"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_pref"
        android:orderInCategory="100"
        android:title="Log out"
        app:showAsAction="never" />
</menu>
Community
  • 1
  • 1
  • The [`invalidateOptionsMenu()` method](http://stackoverflow.com/a/32072318) is what you're looking for. – Mike M. Mar 19 '17 at 14:54
  • If you don't want your menu item to be hidden in the 3 dot, set `app:showAsAction="always"` in your `menu_main`. – Zhi Kai Mar 19 '17 at 14:55
  • Please describe in more detail how the link doesn't help. – OneCricketeer Mar 21 '17 at 07:48
  • I tried the answer in that link by giving a function and changing value inside that function! But whenever that function is called athe app crashes –  Mar 21 '17 at 10:07
  • @MikeM. I tried invalidateOptions but though preference value is updated the menu text not changing and it changes only when 3 dots is clicked –  Mar 21 '17 at 10:08
  • Just saying something doesn't work, or didn't help, isn't sufficient. You need to explain in your post what you've changed, and provide a [mcve] that demonstrates exactly how you're trying to do this, how it's not working, and what you've done as far as debugging to determine that the suggested solution is what is failing. – Mike M. Mar 21 '17 at 10:47
  • @MikeM. Question updated plz go through –  Mar 21 '17 at 13:42
  • You don't need your `updateMenuTitles()` method. The `invalidateOptionsMenu()` method causes the `onCreateOptionsMenu()` and `onPrepareOptionsMenu()` methods to run again, so if you've updated the `SharedPreferences` before calling `invalidateOptionsMenu()`, the code you originally had in those methods will update the badge. – Mike M. Mar 21 '17 at 19:54
  • Yeah As you can see my onResume function, I called invalidateOptionsMenu() after getAmount function where sharedprefrence is updated but the badge is not updated –  Mar 22 '17 at 07:33
  • Then I suspect that you're performing an asynchronous operation in the `getAmount()` method, and `invalidateOptionsMenu()` is running before the `SharedPreferences` actually gets updated. You said you're using Firebase, and AFAIK, there is no synchronous, blocking API for that. To test this, change the `SharedPreferences` value directly in `getAmount()`, with some test value, instead of initiating a Firebase op. Btw, you need to use @username if you want me to be notified. I only saw this because I just happened to come back to check. – Mike M. Mar 22 '17 at 07:43
  • @MikeM. Yeah i am sorry for that!! Yeah getAmount is asynchronous! getAmount get value directly from server and save data to amount! here no firebase.. But i also want to update data using firebase! K let me try to call invalidateOptionsMenu() in getAmount onSuccess method! –  Mar 22 '17 at 09:14
  • @MikeM. I called as i mentioned above! But still menu isnt updated while prefrence value is saved each time!! –  Mar 23 '17 at 05:14
  • I don't know what to tell you. I tested your code days ago with a local incrementing preference variable and the `invalidateOptionsMenu()` method, and it works just fine. Do some debugging. Check to make sure `onCreateOptionsMenu()`/`onPrepareOptionsMenu()` is running when you think it is, and that the `SharedPreferences` value is correct in those methods. Also, you're apparently keeping a field `mytext` for that `TextView`, so try updating that directly with `setText()` in the same place you're calling `invalidateOptionsMenu()`. – Mike M. Mar 23 '17 at 05:24
  • Let me check again! –  Mar 23 '17 at 05:56
  • Finally i got it! I dont know why invalidateOptionsMenu() worked for me! I just updated the textview like regular setText() inside getAmount function! It then updated the amount!! Thanks everyone for suggestions and help –  Mar 23 '17 at 06:13

0 Answers0