Actionbar menu items not visible - android

I want icons on action bar, but I am getting them in the menu options...
Here is my xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="always" />
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always" />
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always" />
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always" />
</menu>
and here is the java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
menu.toggle(true);
return true;
}
if(id == R.id.coc){
startActivity(new Intent(Dashboard.this,CircleOfCare.class));
}
return super.onOptionsItemSelected(item);
}
What am I missing.. I am extending Activity.. If I extend ActionBarActivity, it says, use theme.appcompat or descendant even when I am using that theme only. Please help, thanx in advance

If you aren't using ActionBarActivity then try using
android:showAsAction="always"
instead of
app:showAsAction="always"

Related

how to add checkboxes in navigation drawer as a menu items in android with custom styles

I have created widgets_chkboxes file in layout
In menu create widgets.xml file
enter code here
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Filtered"/>
<item android:title="SortedBy">
<menu>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="Low To High"/>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="High To Low"/>
</menu>
</item>
</menu>
try this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check2"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check3"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_check) {
if(item.isChecked());{
// your code or action
}
}
return super.onOptionsItemSelected(item);
}

onOptionsItemSelected not called

I have a list of menu items in my actionbar. Each item click should trigger a different method. But onOptionsItemSelected is never called.
This is how the actionbar is defined in MainActivity:
public class MainActivity extends AppCompatActivity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings_1) {
//do something
return true;
} else if (id == R.id.action_settings_2) {
//do something
return true;
} else if (id == R.id.action_settings_1) {
//do something
return true;
}
return super.onOptionsItemSelected(item);
}
...
}
This is the actionbar menu layout menu_main:
<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.MainActivity">
<item
android:id="#+id/action_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never" />
</menu>
How can I set up the actionbar so that onOptionsItemSelected is called when an actionbar item is clicked?
Inside your onCreateOptionsMenu, return true instead of calling super. That should do it
In the onCreate(), call setSupportActionbar(), like so
toolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
Just do the change as 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_main, menu);
return true;
}
This problem can potentially also happen if using the menu attribute in XML:
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:menu="#menu/your_menu"
app:navigationIcon="#drawable/..."
app:title="#string/..." />
So app:menu="#menu/your_menu" doesn't work with the overridden fragment method onOptionsItemSelected. The menu item click listener has to be set to the Toolbar
private fun setMenuClickListener(toolbar: MaterialToolbar) = with(toolbar) {
setOnMenuItemClickListener { menuItem ->
if (menuItem.itemId == R.id.yourId) {
//do something
return#setOnMenuItemClickListener true
}
//this is a lambda so it can be just false,
//added return to make it explicit
return#setOnMenuItemClickListener false
}
}
So what we are looking for is the setOnMenuItemClickListener from MaterialToolbar
Try this:
Instead of implement
#Override
public boolean onCreateOptionsMenu
...
In activity_main.xml add:
<androidx.appcompat.widget.Toolbar
android:id="#+id/myToolbar"
app:title="#string/myTitle"
app:menu="#menu/menu_main"
>
</androidx.appcompat.widget.Toolbar>
Next, in menu_main.xml add in each item:
android:onClick="onOptionsItemSelected"
Like this:
<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.MainActivity">
<item
android:id="#+id/action_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
</menu>

Toolbar icons disappearing after expanding the SearchView in Android

here's my problem:
I have that nice toolbar with the icons in landscape mode:
after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):
then returning with the back arrow key, as you see, the add button goes:
and you won't find it in the popup menu anymore:
I'm using support:appcompat-v7:25.1.0, and here's my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="ifRoom"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
</menu>
I can set the add button showAsAction to "always" but I know that this is discouraged.
Does anyone here know why is there this behavior? and how can I prevent that?
Thanks in advance.
You can try to use this:
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
//or use invalidateOptionsMenu();
return true;
}
});
So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. I had this bug too and solved it this way.
Try this
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
Set app:showAsAction to always to make sure it will be visible always.
Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
#Kovy has answered above with the fix for this bug and I confirm it fixes the bug. Thank you very much, mate! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener. Example of it is as posted below:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});

ActionBar icons not visible

I am adding icons to ActionBar, but instead of getting them on ActionBar, I get them in the menu option..
My Java Code-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.report) {
Toast.makeText(getApplicationContext(),"Report",Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
My dashboard.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="never"/>
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always"/>
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always"/>
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always"/>
</menu>
Please tell what am I missing..
I have appcompat as library.
Thank you
Extended ActionBar or AppCompactActivity
First menu item has app:showAsAction="never", so others menu items will always be shown only in overflow menu.

getItemId function returns huge numbers - action bar buttons

I am trying to add submenus , I created xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_overflow"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="ifRoom"
android:title="#string/action_overflow">
<menu>
<item android:id="#+id/item1" android:title="test"></item>
<item android:id="#+id/item2" android:title="test2"></item>
<item android:id="#+id/item3" android:title="test3"></item>
</menu></item></menu>
in mainactivity.java:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// int menuid = item.getItemId();
Toast.makeText(MainActivity.this, "id: "+item.getItemId(), Toast.LENGTH_SHORT).show();
return true;
}
it shows me numbers like 2131034200...... etc..
please help :(

Categories

Resources