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
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>
