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
Related
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 have a problem, I want to add item in a toolbar (material design) programatically .
This is a my menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
I know change text , color , background, and #override listener
toolbar.setBackgroundColor
toolbar.setTextColor
toolbar.setNavigationIcon
toolbar.setText
I don't know how I can add a menu ítem with a category " android:orderInCategory="300"
Thanks.
Note : I have all fragment, without 1 Activity
Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx ..
Try this Toolbar has option getMenu() which return menu
private static final String placeholder1="Something";
private static final int FIRST_ID=Menu.FIRST;
private static final int SECOND_ID=Menu.FIRST+1;
//To add an item
toolbar.getMenu().add(Menu.NONE,FIRST_ID,Menu.NONE,R.string.placeholder1);
toolbar.getMenu().add(Menu.NONE,SECOND_ID,Menu.NONE,R.string.placeholder2);
//and to remove a element just remove it with id
toolbar.getMenu().removeItem(FIRST_ID);
First of all put the item you want to display/hide in your xml file.
Let's say it's "action_settings" as mentioned in your code, override the "onCreateOptionsMenu"
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.action_settings);
if (yourConditionToShow) {
item.setVisible(true);
} else {
item.setVisible(false);
}
return true;
}
If you do not have access to the toolbar, you can do the following:
1 - Add the item to the menu through onCreateOptionsMenu():
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_item_id, menu);
return true;
}
2 - Create a boolean equal to true if the item must be visible
3 - Hide or show the item according to the value of your boolean in onPrepareOptionsMenu()
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mi = menu.findItem(R.id.my_item_id);
if(displayItem)
mi.setVisible(true);
else
mi.setVisible(false);
return super.onPrepareOptionsMenu(menu);
}
In your onCreateOptionmenu, try this code
//menu.add("groupId", "ItemId", "OrderID", "title")
MenuItem item = menu.add(1, 100, 300, "Settings");
item.setIcon(R.drawable.ic_launcher);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
you can also add setting like xml. You can check by itemid("100") in onOptionsItemSelected.
If you don't want to use 100 as ItemId, you can create ids resource file. And can use like this,
//R.id.action_settings is from ids resource file
MenuItem item = menu.add(1, R.id.action_settings, 300, "Settings");
1 Solution :
#Chitrang Thanks to idea, I solved with this line :
toolbar.inflateMenu(R.menu.menu_activity1);
toolbar.inflateMenu(R.menu.menu_activity2);
toolbar.inflateMenu(R.menu.menu_activityx);
Options select R.menu.item1,2,x
2 Solution :
Create custom toolbar : into "android.support.v7.widget.Toolbar" and can you managment all icons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:id="#+id/imageView10"
android:src="#drawable/sun"
android:adjustViewBounds="true" />
</LinearLayout>
</LinearLayout>
In your menu XML file, you can add a menu item not visible (android:visible=false) and render visible when you want.
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.
I'm trying to implement a notification icon in my actionbar to show the count of notifications. Something like
I've added a custom layout file for it NotificationIcon.xml:
<!-- Menu Item Image -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:clickable="true"
android:src="#drawable/notification" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:minWidth="20dp"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:background="#drawable/circle_green"
android:fontFamily="sans-serif-black"
android:textStyle="bold"
android:text="0"
android:textColor="#FFFFFF" />
</RelativeLayout>
And used it in my menu as main_activity_actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_add"
android:title="#string/AddTag"
android:icon="#+drawable/ic_action_new"
android:showAsAction="always" />
<item
android:id="#+id/notification_icon"
android:title="#string/PendingJobs"
android:actionLayout="#layout/notificationIcon"
android:icon="#+drawable/notification"
android:showAsAction="always" />
<item
android:id="#+id/gps_status_icon"
android:title="#string/GPS"
android:icon="#+drawable/gps_grey"
android:showAsAction="always" />
</menu>
The UI looks fine but the OnOptionsItemSelected is not being called for the notification icon. It works fine for the other two.
I did google this and ound this link: onOptionsItemSelected not getting called when using custom action view
I tried to implement it in my main activity:
public override bool OnCreateOptionsMenu(IMenu menu)
{
actionBarMenu = menu;
MenuInflater.Inflate(Resource.Menu.main_activity_actions, menu);
var notificationMenuItem = menu.FindItem(Resource.Id.notification_icon);
notificationMenuItem.ActionView.Click += (sender, args) => {
this.OnOptionsItemSelected(notificationMenuItem);
};
return base.OnCreateOptionsMenu(menu);
}
but it does not works for me. It never fires the click event.
Please help.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
final View notification_icon= menu.findItem(R.id.notification_icon).getActionView();
notification_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write ur code here
}
});
return super.onCreateOptionsMenu(menu);
}
use this code......hope it helps :)
i struggle with the same Problem and found following Solution:
First of all: i really don't know why but if you delete the
android:clickable="true"
then ... and ONLY then you get the click event!!!
Second: you have to set the Listener...
...
item.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do Your Stuff.....
}
});
May someone can Explain why this is with the "android:clickable" Thing... i tough the standard value IS true??
You had made ImageButton clickable, and then written the clickListener for the Layout of menuItem. You can observe that if you click outside the ImageButton but inside the MenuItem layout, your listener will work. The reason is simple clickListener for ImageButton has nothing to do, on setting Clickable = true defines whether this button reacts to click events. Just simply set Clickable = false, your button will not react to its click event, eventually your whole layout will react to your defined clickListener and then your problem will be solved.