I am using the new Android BottomNavigationView but the bottom navigation MenuItem action view is not showing.
After debugging I found that the menu item is not null and the visibility is visible.
After checking height and width of the root view it's coming 0 even after hardcoding the values in layout layout_width=50dp and layout_height=50dp of the root element.
Here is my bottom navigation menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nearby_fragment"
android:enabled="true"
android:icon="#drawable/homenearbyfragment"
android:title="#string/bottom_nearby"
app:showAsAction="always"/>
<item
android:id="#+id/route_fragment"
android:title="#string/bottom_homescreen"
android:enabled="true"
app:showAsAction="always"
android:icon="#drawable/home_mycommute" />
<item
android:id="#+id/newsfeed_activity"
android:title="#string/bottom_news"
android:enabled="true"
app:showAsAction="always"
android:icon="#drawable/newsfeed"
app:actionLayout="#layout/bagde_layout"/>
</menu>
My action layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<ImageView
android:id="#+id/menu_badge_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/newsfeed" />
<TextView
android:id="#+id/menu_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginTop="4dp"
android:background="#drawable/circle_bg"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="8sp"
/>
In my Activity I am trying to set:
private void setCountOnNews(Menu menu) {
mReportMenu = menu.findItem(R.id.newsfeed_activity);
FrameLayout count = (FrameLayout) mReportMenu.getActionView();
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewsFeedActivity.class);
startActivity(intent);
MainApplication.getInstance().trackScreenView(getString(R.string.tracknewsfeedmenu));
}
});
TextView notifCount = (TextView) count.findViewById(R.id.menu_badge);
//if (Utils.getNewsfeedCountPreference(MainActivity.this) > 0)
notifCount.setText(String.valueOf(Utils.getNewsfeedCountPreference(MainActivity.this)));
notifCount.setText("asd;jfnapsdifnaspdifnasdpifnaspdfnapsdifnaspidfnapsidfnaspdiufif");
Log.d(TAG, "setCountOnNews:" + notifCount.getText().toString());
//else
// notifCount.setVisibility(View.GONE);
}
Did you forget to add that in your MainActivity?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
if you are using fragment than you need to set has option menu true.
setHasOptionsMenu(true);
Related
I've created a custom ActionBar with a simple TextView, the text is supposed to be aligned to the center. Then I added a MenuItem with onCreateOptionsMenu method. The result was that the each of the components were at each side of the ActionBar, the menu-item was placed at the left corner and the text-view was placed at the right corner. How can I make the text-view remain centered despite adding menu-items to the action-bar?
Here is my custom action_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/app_name"
android:textColor="#ffffff"
android:id="#+id/action_bar_text"
android:textSize="18sp"/>
</LinearLayout>
Here is my menu.xml
<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"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/back_action"
android:orderInCategory="100"
android:title="#string/back"
app:showAsAction="always"/>
</menu>
Here is my onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, mMenu);
MenuItem item = menu.findItem(R.id.back_action);
item.setVisible(true);
return true;
}
and this is how I use my custom action bar in onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
//MORE CODE
I've been trying to load an image into a menu item which has been created through a custom layout and have been checking on the official documentation and on posts such as these Custom view for Menu Item but..
That's my onCreateOptionsMenu ond OnPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
final MenuItem alertMenuItem = menu.findItem(R.id.action_my_personal);
FrameLayout rootView = (FrameLayout) alertMenuItem.getActionView();
redCircle = (FrameLayout) rootView.findViewById(R.id.view_alert_red_circle);
countTextView = (TextView) rootView.findViewById(R.id.view_alert_count_textview);
ImageView hc_image_menu_inflated = (ImageView) rootView.findViewById(R.id.hc_image_menu);
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(alertMenuItem);
}
});
if (menu.findItem(R.id.hc_image_menu) != null) {
loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu_main, menu);
return true;
}
As you can see I have added these lines to avoid the app crashing, as menu.finditem(R.id.hc_image_menu) is returning as null and that's where the problem is, as I want to populate the ImageView that has the id hc_image_menu but am not being successful despite so many attempts
if (menu.findItem(R.id.hc_image_menu) != null) {
loadMenuIcon(menu.findItem(R.id.hc_image_menu), my_image_url);
}
My code for my toolbar_main_menu layout is this:
<item
android:id="#+id/action_my_personal"
app:actionLayout="#layout/toolbar_menu_hc"
android:orderInCategory="100"
android:title="My Personal Trainer"
app:showAsAction="always" />
And for my custom layout (toolbar_hc_menu) is this one:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="48dp"
android:layout_height="32dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/hc_image_menu"
android:layout_marginRight="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/gavin"/>
<FrameLayout
android:id="#+id/view_alert_red_circle"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_gravity="top|end"
android:background="#drawable/date_circle"
android:visibility="gone"
tools:visibility="visible">
<TextView
android:id="#+id/view_alert_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/white"
android:textSize="10sp"
tools:text="3"/>
</FrameLayout>
Thanks very much for any help on that. Really much appreciated!! :)
Issue fixed. :)
At the end I just had to load the image into the ImageView after having got the alertMenuItem.
if (my_image_url!= null) {
Picasso.with(getApplicationContext()).load(my_image_url).into(hc_image_menu_inflated);
}
I have a TextView functioning as a notification badge on a menu item. The problem is that the findViewById method always returns null after an orientation change, so when I try to modify its visibility with setVisibility it throws a NullPointerException. I've tried calling the onCreateOptionsMenu again by calling invalidateOptionsMenu in onRestart but it doesn't appear to help.
From what I can tell every other view is found and it's just this TextView that is being a nuisance.
The part where it crashes (this is called in onCreate) :
public void updateUnreadNotificationCount(final int unreadNotificationsCount) {
unreadNotificationCount = unreadNotificationsCount;
if (unreadNotificationCount == 0) {
notificationCounter.setVisibility(View.INVISIBLE);
}else {
notificationCounter.setVisibility(View.VISIBLE);
notificationCounter.setText(String.valueOf(unreadNotificationCount));
}
}
How the Textview gets created:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View count = menu.findItem(R.id.action_notifications).getActionView();
notificationCounter = (TextView) count.findViewById(R.id.textview_notification_count);
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mCtx, NotificationListActivity.class);
startActivity(i);
}
});
notificationCounter.setText(String.valueOf(unreadNotificationCount));
return super.onCreateOptionsMenu(menu);
}
The XML for the menu item:
<item
android:id="#+id/action_notifications"
android:title="#string/action_notifications"
android:icon="#drawable/ic_mail_white_48dp"
android:actionLayout="#layout/actionbar_notification_icon"
android:showAsAction="always"
app:showAsAction="always"
android:orderInCategory="60"/>
The action layout for the TextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
style="#android:style/Widget.ActionButton">
<ImageView
android:id="#+id/imageview_notification"
android:src="#drawable/ic_mail_white_48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview_notification_count"
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/imageview_notification"
android:layout_alignRight="#id/imageview_notification"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="#drawable/rounded_square"/>
</RelativeLayout>
Managed to fix the issue by moving the initialization of the TextView into onPrepareOptionsMenu and calling invalidateOptionsMenu() during onResume().
I tried to find an answer for myself but couldn't find it.
I need make badge on the MenuItem icon in the Toolbar, like this:
How can I make this?
Here is step by step functionality:
add menu.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">
<item
android:id="#+id/actionNotifications"
android:icon="#drawable/ic_info_outline_white_24dp"
android:menuCategory="secondary"
android:orderInCategory="1"
android:title="Cart"
app:actionLayout="#layout/notification_layout"
app:showAsAction="always" />
</menu>
Then add notification_layout.xml, this layout will be used as the notification icons layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:clickable="true"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:contentDescription="Notification Icon"
android:gravity="center"
android:src="#drawable/ic_info_outline_white_24dp" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/x5dp"
android:layout_marginLeft="#dimen/x10dp"
android:layout_marginRight="0dp"
android:background="#drawable/pointer_"
android:gravity="center"
android:minWidth="17sp"
android:text="0"
android:textColor="#ffffffff"
android:textSize="12sp" />
</RelativeLayout>
now inside Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
final View notificaitons = menu.findItem(R.id.actionNotifications).getActionView();
txtViewCount = (TextView) notificaitons.findViewById(R.id.txtCount);
updateHotCount(count++);
txtViewCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateHotCount(count++);
}
});
notificaitons.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO
}
});
return true;
}
You can put following function (taken from stackoverflow) inside the activity to update counter:
public void updateHotCount(final int new_hot_number) {
count = new_hot_number;
if (count < 0) return;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (count == 0)
txtViewCount.setVisibility(View.GONE);
else {
txtViewCount.setVisibility(View.VISIBLE);
txtViewCount.setText(Integer.toString(count));
// supportInvalidateOptionsMenu();
}
}
});
}
I think it is possible with :
<ImageView
android:id="#+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/unread_background" /> <!-- your icon -->
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="8sp"
android:layout_centerInParent="true"
android:textColor="#FFFFFF" />
And then using it for that icon as background.also, you need to remove/disable the default icon too.
You may want to take a look:
Remove App Icon from Navigation Drawer ActionBar Does not work
Remove the navigation drawer icon and use the app icon to open
https://stackoverflow.com/a/29160904/4409113
Also, in the android-sdk/platforms/android-22/data/res, there should be that icon. You just need to find that and use it for your purpose (for example, adding that ImageView and adding it asbackground)
Take a look: https://stackoverflow.com/a/34999691/4409113
MaterialToolbar has ability to add BadgeDrawable:
For example in your Activity you can add the badge like this:
val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
val badgeDrawable = BadgeDrawable.create(this).apply {
isVisible = true
backgroundColor = neededBadgeColor
number = neededNumber
}
BadgeUtils.attachBadgeDrawable(badgeDrawable, toolbar, R.id.item_in_toolbar_menu)
In my action bar, I have defined a menu item that can show text "DONE" by the code below:
Menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_register_text"
android:actionLayout="#layout/action_done_text"
android:title="#string/action_done"
android:showAsAction="always"/>
</menu>
action_done_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/expand_activities_button"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">
<TextView
android:id="#+id/register_action_bar_done"
android:layout_width="53dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginRight="10dip"
android:gravity="center"
android:text="DONE" />
</FrameLayout>
I have onCreateOptionsMenu implement properly in the code, and the view can show the text correctly, but just when I tap on the DONE text, onOptionsItemSelected is not called. To me, it seems like the click event is not recognized.
I was wondering if the above way is not a good way to add a text menu item?
Use this as shown in onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.map_menu, menu);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.menu_more) {
itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(this);
}
}
}
return super.onCreateOptionsMenu(menu);
}