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);
}
Related
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);
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)
I am trying to make a shopping cart android app and i want to show the cart with number of items.
attaching the screenshot of app below .
When clicking on the cart icon its does not going any where.. :(.
here is my code.
MainActivity.java
public class PandaActivity extends ActionBarActivity {
TextView notifCount;
RelativeLayout count;
int mNotifCount = 0;
protected ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.topbar));
actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backbutton));
}
#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_panda, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.badge) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_panda.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.sha.netstager.pandafoods.activity.PandaActivity">
<item
android:title="demo"
android:id="#+id/badge"
app:actionLayout="#layout/feed_update_count"
app:showAsAction="always">
</item>
</menu>
feed_update_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="#drawable/cart" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="18dp"
android:layout_marginRight="4dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:text="99"
android:textSize="16dp"
android:background="#f7134e"
android:textColor="#fff"/>
</RelativeLayout>
I tried many ways to solve this problem with onclick methord and some others. but dont know what is the error is happening here.
There is no messages or warnings in logcat when I click on the menu item in action bar.
Many Thanks..
Try to add onclick in your onCreateOptionsMenu, instead of current solution as given below.
#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, menu);
RelativeLayout rl_viewBag = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
txt_bagCount = (TextView)rl_viewBag.findViewById(R.id.actionbar_notifcation_textview);
txt_bagCount.setText("10");
rl_viewBag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
}
});
return true;
}
for better look of actionbar use extends AppCompatActivity this is the same look and feel will be provided which is been used by gmail.
My android application has an action bar item to add employees.
So here what I did is call custom layout named add_employee when this action bar item clicked.
<item android:id="#+id/action_add"
android:icon="#drawable/ic_action_add_person"
android:title="#string/action_add"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/add_employee"/>
Following image shows that custom layout:
This is a loyout xml file named add_employee. Add and cross marks are image views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="12dp"
android:src="#drawable/ic_action_cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="228dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="40dp"
android:layout_toLeftOf="#+id/imageView1"
android:ems="10"
android:inputType="numberSigned" />
<ImageView
android:id="#+id/addemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_marginRight="26dp"
android:layout_toLeftOf="#+id/imageView2"
android:src="#drawable/ic_action_add_person" />
</RelativeLayout>
Now I want to get the entered value to this edit text field when add image view clicked. This operation should be handled in the same java class where I set the option menu also.
public class MainPortal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main_portal, menu);
return super.onCreateOptionsMenu(menu);
}
}
I have used setOnItemClickListener for imge view but it ends with a forced closed error. How can I do this task?
Please help me...
Thanks in advanced.
Did you create the actions in onOptionsItemSelected? Where is the code showing the implementation of the Action bar tabs?
this is how to implement an option on actionbar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
there you can implement whatever and however...
plus showing more code and LogCat would help understanding your exact issue