I trying to attach an event listener to a custom button in my action bar, but the click event triggers no action. Can someone please help me to debug.
R.menu.address_menu
<item
android:id="#+id/address_menu_add"
android:showAsAction="always"
android:title="#string/add"
android:actionLayout="#layout/add"/>
<item
android:id="#+id/address_menu_setting"
android:showAsAction="collapseActionView"
android:title="#string/settings"/>
<item
android:id="#+id/address_menu_help"
android:showAsAction="collapseActionView"
android:title="#string/help"/>
<item
android:id="#+id/address_menu_privacy"
android:showAsAction="collapseActionView"
android:title="#string/privacy_policy"/>
actionview for the button
<Button
android:id="#+id/add_new_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_basket_full"
android:layout_gravity="center|center_vertical"
android:gravity="center|center_vertical"
android:text="Add"
android:textColor="#580606"
android:textAppearance="?android:attr/textAppearanceMedium" />
Event listener added
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.address_menu, menu);
MenuItem item = menu.findItem(R.id.address_menu_add);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addReview();
}
});
return true;
}
Related
how to go to main menu onclick on sub menu in android. i am trying this but unable to do that.
this is java
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
sub = menu.getItem(0).getSubMenu();
sub.setHeaderTitle("sub menu...");
sub.setHeaderIcon(R.drawable.ic_voice);
//Toast.makeText(this, "submenu label=", Toast.LENGTH_SHORT).show();
MenuItem item = menu.findItem(R.id.action_settings);
TextView iv= (TextView) item.getActionView().findViewById(R.id.action_settings);
sub.getItem(0).setActionView(iv);
Toast.makeText(this, "before onclick listener=", Toast.LENGTH_SHORT).show();
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "-submenu header title clicked", Toast.LENGTH_SHORT).show();
}
});
return super.onPrepareOptionsMenu(menu);
}
this 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">
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:title="Refresh" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_voice"
android:title="Settings"
app:actionViewClass="android.widget.TextView">
<menu>
<item
android:id="#+id/change_password"
android:title="change password" />
<item
android:id="#+id/user_details"
android:title="user details" />
</menu>
</item>
</menu>
code
showing is submenu when we click on submenu text it should go back to mainmenu
Android doesn't provide some mechanism to set onClickListener to SubMenu header, but you can use a hack.
First of all, get rid of SubMenu header:
SubMenu sub = menu.findItem(R.id.action_settings).getSubMenu();
sub.clearHeader();
It's not secure to use getItem(0), because the order of your items may change, so I used findItem(R.id.action_settings) instead.
Next, you should add one more item to your SubMenu:
<menu>
<item
android:id="#+id/sub_title"
android:title="sub menu..."/>
<item
android:id="#+id/change_password"
android:title="change password" />
<item
android:id="#+id/user_details"
android:title="user details" />
</menu>
But now all three items will looks the same, so you need to change color of first MenuItem
MenuItem headItem = sub.findItem(R.id.sub_title);
SpannableString s = new SpannableString(headItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.GRAY), 0, s.length(), 0);
headItem.setTitle(s);
Also, don't use onPrepareOptionsMenu() for that purposes, because it calls every time when you open menu, you can put all that code inside onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SubMenu sub = menu.findItem(R.id.action_settings).getSubMenu();
sub.clearHeader();
MenuItem headItem = sub.findItem(R.id.sub_title);
SpannableString s = new SpannableString(headItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.GRAY), 0, s.length(), 0);
headItem.setTitle(s);
return true;
}
And the last step - listen to clicks on that MenuItem:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.sub_title:
Toast.makeText(this, "-submenu header title clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
I am just starting learning Android and have some troubles with creating menu in app.
I tried all options to create menu but no one work for me.
When I run emulator or real device menu doesn't appear.
I have tried "Ctrl+M" and different devices but it doesn't work.
What is the problem?
My MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionMenu(final Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
my activity_main.xml
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.genaepic.p013_contextmenu.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just do it!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
my main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/item1"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings2"
android:orderInCategory="100"
android:title="#string/item2"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings3"
android:orderInCategory="100"
android:title="#string/item3"
app:showAsAction="never" />
<item
android:id="#+id/action_settings4"
android:checkable="false"
android:orderInCategory="100"
android:title="#string/item4"
app:showAsAction="always" />
enter image description here
The problem is due to the showAsAction tag. Your item that inflates your overflow menu MUST BE showAsAction="always", otherwise menu may hide in devices. An example snippet:
<item
android:id="#+id/settings"
android:icon="#drawable/my_drawable"
app:showAsAction="always"
android:title="Setting">
</item>
EDIT
You can override the onCreatOption to inflate the menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu: ");
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected: ");
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
I am trying to create android application that takes multiple parameters when searching. Currently, I have a searchable interface and am able search from my action bar. However, I want to create a yelp like search bar where I have 2 text fields to enter in data as shown in the image:
How can I add an additional text field when searching?
Here is the code I am using to initalize the search
#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_home, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
// set query change listener to hide keyboard after input has been
// submitted
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// hides and then unhides search tab to make sure keyboard
// disappears when query is submitted
searchView.clearFocus();
return false;
}
});
and here is my menu_layout:
<item
android:id="#+id/action_search"
android:title="#string/search"
android:orderInCategory="100"
impulz:showAsAction="always"
impulz:actionViewClass="android.widget.SearchView"/>
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" impulz:showAsAction="never" />
Thank you very much.
you can use
actionBar.setCustomView(R.layout.custom_search);
and set the actionBar programmatically "custom view"
like this:
custom_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.actionbarsherlock.widget.SearchView
android:id="#+id/search1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:iconifiedByDefault="false"
android:visibility="invisible" />
<com.actionbarsherlock.widget.SearchView
android:id="#+id/search2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:iconifiedByDefault="false"
android:visibility="invisible" />
</LinearLayout>
<ImageButton
android:id="#+id/btn_search_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/search_content" />
</LinearLayout>
menu.xml
<item
android:id="#+id/menu_search"
android:icon="#drawable/red_icon"
android:showAsAction="always"
android:title="search" />
use setCustomView into your onCreate()
MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
actionBar.setCustomView(R.layout.custom_search);
actionBarCustomView = action_bar.getCustomView();
searchView = ((SearchView) actionBarCustomView.findViewById(R.id.search1));
searchView2 = ((SearchView) actionBarCustomView.findViewById(R.id.search2));
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
searchView.setVisibility(View.INVISIBLE);
searchView2.setVisibility(View.INVISIBLE);
return false;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
//set visibilty
//do what ever you want
break;
}
return true;
}
Hope this will work out for you.
For some reason, I cannot get my button to appear on the Action Bar. I have defined it in an XML file in /res/menu, along with inflating it and listening for an action. The icon is present in /res/drawable-hdpi. And nothing of interest shows in logcat. :(
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
android:showAsAction="always" />
</menu>
Code in main activity:
public class MainActivity extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.logout:
//logout code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//rest of app
}
I followed this question for my initial problem, and it didn't help. How to add button in ActionBar(Android)?
Try with this change:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
yourapp:showAsAction="always" />
</menu>
I am trying to implement an action bar in which one of the buttons on click shows a popup menu.
Here's the menu. XML (menu items in the action bar)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_refresh"/>
<Item
android:id="#+id/popup"
android:icon="#drawable/ic_action_search"
android:onClick="showPopup"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search" />
I wish to show a popup menu on the click of the item having id "#+id/popup".
here is the XML for the popup menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:icon="#drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/item2"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_search"/>
here is the onClick method for the button
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
And the problem is that no popup shows up on click of that button. Need help folks.
I found this here: http://developer.android.com/guide/topics/ui/menus.html
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/selectImg"
android:icon="#android:drawable/ic_dialog_dialer"
android:showAsAction="always">
<menu>
<item android:id="#+id/top"
android:title="#string/topimg"/>
<item android:id="#+id/bottom"
android:title="#string/botimg" />
</menu>
</item>
</menu>
You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
// View v = findViewById(R.id.f);
switch (item.getItemId()) {
case R.id.top:
//action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.
Hi every one, that's my own solution : i created the showPopup method, and then i called it in the onOptionsItemSelected like this :
public void showPopup(){
View menuItemView = findViewById(R.id.menu_save);
PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.popup, popup.getMenu());
popup.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
showPopup();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
popup.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/decon"
android:showAsAction="ifRoom"
android:title="#string/decon"/>
<item
android:id="#+id/mRes"
android:showAsAction="ifRoom"
android:title="#string/mesRes"/>
</menu>
main.xml => it's called onCreateOptionsMenu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_save"
android:enabled="true"
android:icon="#drawable/action_save"
android:showAsAction="ifRoom|withText"
android:title="#string/action_save"
android:visible="true"/>
</menu>
Finally i
implements PopupMenu.OnMenuItemClickListener to #Override onMenuItemClick method.
As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.
Try to change 'this' to getActivity().
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
Hope it helps..!!
I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8b8989"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/menuItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu1" />
<TextView
android:id="#+id/menuItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu2" />
<TextView
android:id="#+id/menuItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/menu3" />
</LinearLayout>
and i changed the onClick method
public void showPopup(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(
R.layout.overflow_layout, null, false), 300, 400, true);
pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0,
0);
}
This solved the issue
android:onClick="popup"
may be you should change it to android:onClick="showPopup"?