I am trying to implement a custom layout for my menu item and went through many solutions by I am getting Null pointer Exception whenever i try to fetch TextView inside the layout specified for actionLayout
for my menuItem as the getActionView returns null.
home_menu:
<?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">
<item android:id="@+id/action_notifications"
android:title=""
app:showAsAction="always"
android:visible="true"
android:icon="@drawable/ic_notifications_black_24dp"
android:orderInCategory="3"
android:actionLayout="@layout/counter_action_bar_notification_icon"
/>
<!--android:icon="@drawable/ic_notifications_black_24dp"-->
<!--android:actionLayout="@layout/bage_layout"-->
</menu>
counter_action_bar_notification_icon
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
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/ic_notifications_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"
android:contentDescription="bell"
/>
<TextView
android:id="@+id/hotlist_hot"
android:layout_width="wrap_content"
android:minWidth="17sp"
android:textSize="12sp"
android:textColor="#ffffffff"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@null"
android:layout_alignTop="@id/hotlist_bell"
android:layout_alignRight="@id/hotlist_bell"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="@drawable/bage_circle"/>
</RelativeLayout>
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
item = menu.findItem(R.id.action_notifications);
if(loginStatus==false){
item.setVisible(false);
return false;
}
if(user!=null && !user.getUserType().equals("3")){
item.setVisible(false);
return false;
}
final View menu_hotlist = MenuItemCompat.getActionView(item);
TextView ui_hot =(TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
ui_hot.setText(Integer.toString(13));
return true;
}
Try this
item.setActionView(R.layout.counter_action_bar_notification_icon);
final View menu_hotlist = MenuItemCompat.getActionView(item);
TextView ui_hot =(TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
ui_hot.setText(Integer.toString(13));
Use
app:actionLayout
instead of
android:actionLayout
in your layout file and dont forget to define app
xmlns:app="http://schemas.android.com/apk/res-auto"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With