Menu key not working in AppCompat - android

I have been porting Material to an open sourced app. I have used the AppCompat v21 library. I hit the menu key and I get nothing. Everything else works.
What am I doing wrong? I will attach code later as I am sending this from my phone.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_view_source" android:title="#string/action_view_source" />
<item android:id="#+id/action_view_translation" android:title="#string/action_view_translation" yourapp:showAsAction="never" />
<item android:id="#+id/action_view_bugs" android:title="#string/action_view_bugs" yourapp:showAsAction="never" />
</menu>
Java
public class MainActivity extends ActionBarActivity
…
#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);
return true;
}

Nevermind, I figured it out. It appears that the Menu key was disabled in the new AppCompat. I actually found a way to fix it and I guess I will share it with you.
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
public void onCreate(Bundle icicle){
...
setContentView(R.layout.main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar.isOverflowMenuShowing()) {
toolbar.dismissPopupMenus();
} else {
toolbar.showOverflowMenu();
}
return true;
}
return super.onKeyUp(keyCode, event);
}

Related

Contextual Overlay stacking on top of toolbar

I have read about every answer on this and still can't seem to make it work. When I call startSupportActionMode, the action mode menu is stacked on top of the toolbar instead of replacing it. This graphic is from another user that was having the same issue.
Most of the other questions were resolved by using windowActionModeOverlay in the styles. This doesn't seem to be working for me. I'm also sure I'm importing the correction action mode.
(import android.support.v7.view.ActionMode;
)
My situation is a little different. My main activity is a AppCompatActivity. I load different fragments based on choices made from a navigation drawer. The actionbar is changed based on the navigation item selected.
This code all worked before I changed over to appCompat-v7. I am trying to convert to the new material design and am therefor replacing the base actionbar with a toolbar. Here are some code snippets:
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.acme.common.MINApplication"
android:largeHeap="true">
<activity android:name="com.acme.common.MINMainActivity"
android:theme="#style/AppTheme"
android:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/ColorPrimary</item>
<!-- Support library compatibility -->
<item name="colorPrimary">#color/ColorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Support library compatibility -->
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
Main Activity:
public class MINMainActivity extends AppCompatActivity
{
private final String TAG = ((Object) this).getClass().getSimpleName();
private View mMainView;
// New Toolbar and Navigation View
public Toolbar toolbar;
public RecyclerView recyclerView;
private MINPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public DrawerLayout drawerLayout;
public android.support.v7.app.ActionBarDrawerToggle drawerToggle;
volatile public boolean isDrawerOpen = false;
private Menu mMenu;
#Override
public void onCreate(Bundle savedInstanceState)
{
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS | Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
// Inflate main view
mMainView = getLayoutInflater().inflate(R.layout.material_design_drawer_layout, null);
setContentView(mMainView);
sharedInstance = this;
// Setup New Toolbar implementation
setupToolbar();
// Setup Recycler Adapter
setupAdapter();
// Setup Navigation View
initNavigationDrawer();
// Show startup screen/fragment
showStartupScreen();
// Color components based on json settings
setCustomAttributes();
init();
}
Fragment:
public class MINPageTypeGridFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate view
gridView = (StickyGridHeadersGridView)inflater.inflate(R.layout.page_gridview_fragment, container, false);
// Set up specific controls/views
initializeGridView(inflater, container);
...
return gridView;
}
#Override
public void onResume()
{
ActionBar actionBar = MINMainActivity.getSharedInstance().getSupportActionBar();
if(actionBar != null)
{
actionBar.show();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setTitle(pageDefinition.pageName);
}
...
super.onResume();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int count = 0;
...
freezeGridView();
mActionMode = MINMainActivity.getSharedInstance().startSupportActionMode(mActionModeCallback);
...
return super.onOptionsItemSelected(item);
}
public ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
MenuItem shareMenuItem;
...
actionMode.getMenuInflater().inflate(R.menu.gridview_edit_menu, menu);
shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
if(mShareActionProvider != null)
{
mShareActionProvider.setShareIntent(Share(null));
mShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
{
#Override
public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
{
if(mActionMode != null)
{
currentMode = MODE_STANDARD;
clearSelectedItems();
mActionMode.finish();
mActionMode = null;
}
return false;
}
});
...
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch(menuItem.getItemId())
{
case R.id.MenuItemEdit:
launchAlbumItemDetails(MINPageTypeGridFragment.this, currentAlbumItem, pageDefinition.pageConfigFileName);
return true;
case R.id.MenuItemDelete:
deleteItem(MINPageTypeGridFragment.this, currentAlbum, currentAlbumItem);
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode)
{
clearSelectedItems();
}
}
I'm sure that I'm missing something obvious but I'm out of ideas.
You should remove 'duplicate' definition: android:windowActionModeOverlay -> windowActionModeOverlay; android:colorPrimary -> colorPrimary and so on. This is an example that should work:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="colorControlNormal">#color/appColorControlNormal</item>
<item name="colorControlHighlight">#color/appColorControlHighlight</item>
<item name="colorControlActivated">#color/appColorControlActivated</item>
<item name="colorPrimary">#color/appColorPrimary</item>
<item name="colorPrimaryDark">#color/appColorPrimaryDark</item>
<item name="colorAccent">#color/appColorAccent</item>
</style>
Be sure to set the toolbar as current actionbar (maybe your method setupToolbar already do this):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
After that, change your code and start the action mode directly from the toolbar reference:
toolbar.startActionMode(mActionModeCallback)
Let us know if this fix your problem.

Adding Action Icons to toolbar in an activity that extends ListActivity

I am trying to add action icons to my toolbar but in vain. I have followed all the suggestion from this thread.
Maybe the problem is that my Activity doesn't extend ActionBarActivity directly because it already extends ListActivity as follows:
public class ShopsCatalogueActivity extends ListActivity {
private Toolbar toolbar; // Declaring the Toolbar Object
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//using delegates because this activity already extends ListActivity and we want to add toolbar
AppCompatCallback callback = new AppCompatCallback() {
#Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
#Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
//get the layout
delegate.setContentView(R.layout.activity_shops_catalogue);
//adding toolbar following material design approach
toolbar= (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
delegate.setSupportActionBar(toolbar);
...
}
here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyOwnAppNameSpace="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
MyOwnAppNameSpace:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:orderInCategory="200"
android:title="Search"
MyOwnAppNameSpace:showAsAction="always"></item>
<item
android:id="#+id/action_user"
android:icon="#drawable/ic_user"
android:orderInCategory="300"
android:title="User"
MyOwnAppNameSpace:showAsAction="ifRoom"></item>
</menu>
In the Activity:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shops_catalogue_toolbar_menu, menu);
return true;
}
In build.gradle I am using:
compile 'com.android.support:appcompat-v7:22.2.0'
One approach you could use is to use a FragmentActivity as your base activity then have it load in a ListFragment. In the list fragment you can simply add this line in the onCreate method:
getActivity().getActionBar().setIcon(R.drawable.ic_main);
However Budius is right you should use a RecyclerView, its a new android class which simplifies the old viewholder pattern.
Android List and Card Guide ,
RecyclerView Android Doc

NullPointerException after replaceing actionbar with toolbar

I'm using badge layout in one menu item
here is some menuItems
<item
android:id="#+id/action_rdvmed"
android:title="#string/rdv"
android:icon="#drawable/ic_action_action_event"
android:showAsAction="always" />
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout" <!--this one causes the problem-->
android:showAsAction="always" />
<item
android:id="#+id/action_actualiser"
android:title="#string/actualiser"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="always" />
i'm getting a null pointer exception in my onCreateOptionMenu
#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_activity_med, menu);
this.MenuMed = menu;
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_msg).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.hotlist_hot); //the error in this line
imgMessage = (ImageView) badgeLayout.findViewById(R.id.hotlist_bell);
badgeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listMessages.clear();
msg();
}
});
return true;
}
and here is the part of muy onCreate methode whare i'm using toolbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_med);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
thanks in advance.
I believe you're facing the same problem as one user faced here.
The trick is you need to change your android:actionLayout to app:actionLayout.
So, change your
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout"
android:showAsAction="always" />
to,
<item
android:id="#+id/action_msg"
android:title="#string/msg"
app:actionLayout="#layout/badge_layout"
android:showAsAction="always" />

ActionBarActivity doesn´t shows my AppIcon

I´m trying to add in my ActionBar my app icon, but I was reading on Google Developers and I can get the solution. I´m doing this on my ActivityMain:
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
But it doesn´t work.
This is my first day with Android and I just want make an ActionBar with the main icon on the left and an icon search.
Thank You.
With API21 you should use the new Toolbar class.
Put the Toolbar in your layout:
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="attr/actionBarSize"
android:background="?attr/colorPrimary" />
Then in your code (onCreate in your Activity for example):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_myNavigationIcon);
You can find more info in the official post.
Add:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher)
You should be changing the logo, which is by default the same as the launcher icon.
You should do the following steps to implement ActionBar :
1 - Extend ActionBarActivity like this :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Notice to import this :
import android.support.v7.app.ActionBarActivity;
2 - Add lines below into onCreate :
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
3- Create your xml menu under res/menu/your_menu.xml
somthing like this :
your_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_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
4- Inflate the menu to the action bar, and handle the action bar item clicks :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.your_menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Hope this help you!!!

Android - cant add button to Actionbar

I am trying to add a button to my action bar in android
Below is my code in the menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_compose"
android:icon="#drawable/help"
android:title="help"/>
</menu>
and I added this to my Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
But when I run the app the Actionbar is still the same, I'm not sure if it matters but I have a Navigation drawer in my app to.
use actionbar sherlock library and use below code.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class HomeActivity extends SherlockFragmentActivity implements TabListener
{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issue240);
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(YOUR_BUTTON_LAYOUT);
}
}

Categories

Resources