Friday, 23 October 2015

android display numbers on action bar item

http://stackoverflow.com/questions/17696486/actionbar-notification-count-icon-like-google-have



if you use appcompat, the code does not work..

below is my solution:


package com.marksandspencer.androidtest1;

import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    static Button notifCount;
    static int mNotifCount = 0;

    @Override    public boolean onCreateOptionsMenu(Menu menu) {



        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.xx, menu);
////        View count = menu.findItem(R.id.badge).getActionView();//        notifCount = (Button) count.findViewById(R.id.notif_count);

        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        notifCount = (Button) MenuItemCompat.getActionView(item);

        notifCount.setText(String.valueOf(mNotifCount));
        

        return super.onCreateOptionsMenu(menu);
    }

    private void setNotifCount(int count){
        mNotifCount = count;
        invalidateOptionsMenu();
    }



    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_refresh) {
            setNotifCount(1);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



AndroidTest1/app/src/main/res/drawable/shape_notification.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">
    <stroke android:color="#22000000" android:width="2dp"/>
    <corners android:radius="5dp" />
    <solid android:color="#CC0001"/>
</shape>




AndroidTest1/app/src/main/res/layout/feed_update_count.xml:

<?xml version="1.0" encoding="utf-8"?><Button xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/notif_count"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:minWidth="32dp"    android:minHeight="32dp"    android:background="@drawable/shape_notification"    android:text="0"    android:textSize="16sp"    android:textColor="@android:color/white"    android:gravity="center"    android:padding="2dp"    android:singleLine="true">
</Button>



AndroidTest1/app/src/main/res/menu/xx.xml:
<?xml version="1.0" encoding="utf-8"?><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">

    <item        android:id="@+id/badge"        android:title="badge"        android:actionLayout="@layout/feed_update_count"        android:icon="@drawable/shape_notification"        app:showAsAction="always">
    </item>

    <item        android:id="@+id/action_refresh"        android:title="xxx"        app:showAsAction="always" />
</menu>




















No comments:

Post a comment