How to fix getActionBar method may produce java.lang.NullPointerException

Actually Android Studio isn’t showing you an “error message”, it’s just a warning. Some answers propose the use of an assertion, Dalvik runtime has assertion turned off by default, so you have to actually turn it on for it to actually do something. In this case (assertion is turned off), what you’re essentially doing is … Read more

Handling ActionBar title with the fragment back stack?

In every fragment and every activity I change the title like this. This way the active title will always be correct: @Override public void onResume() { super.onResume(); // Set title getActivity().getActionBar() .setTitle(R.string.thetitle); } There is some cases where onResume isn’t called inside fragments. In some of these cases we can use: public void setUserVisibleHint(boolean isVisibleToUser) … Read more

How to display both icon and title of action inside ActionBar?

‘always|withText‘ will work if there is sufficient room, otherwise it will only place icon. You can test it on your phone with rotation. <item android:id=”@id/menu_item” android:title=”text” android:icon=”@drawable/drawable_resource_name” android:showAsAction=”always|withText” />

How To show icons in Overflow menu in ActionBar

The previously posted answer is OK, generally speaking. But it basically removes the default behaviour of the Overflow menu. Things like how many icons can be displayed on different screen-sizes and then they dropped off into the overflow menu when they can’t be displayed. By doing the above you remove a lot of important functionality. … Read more

Action bar Back button not working

I solved these problem by adding the below coding in GalleryActivity. ActionBar actionBar; actionBar=getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; } return super.onOptionsItemSelected(item); } In MainActivity: Previously, public class HomeActivity extends BaseActivity Then I change into public class HomeActivity extends FragmentActivity In GalleryFragment: I use Intent … Read more

Auto Collapse ActionBar SearchView on Soft Keyboard close

I’ll expand on @user1258568 ‘s answer for the lazy. This worked for me. Note that it clears your query when focus is lost. final MenuItem searchMenuItem = optionsMenu.findItem(R.id.search); final SearchView searchView = (SearchView) searchMenuItem.getActionView(); searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean queryTextFocused) { if(!queryTextFocused) { searchMenuItem.collapseActionView(); searchView.setQuery(“”, false); } } });

Notification Badge On Action Item Android

You can show custom MenuItem on ActionBar by creating a custom layout for MenuItem. To set a custom layout you have to use menu item attribute app:actionLayout. Follow below steps to create a Badge on Cart action item. See the attached image for result. Create a custom layout with ImageView(for cart icon) and TextView(for count … Read more

How to add a Dropdown item on the action bar

First option: menu/options.xml: <item android:icon=”@drawable/ic_menu_sort” android:showAsAction=”ifRoom”> <menu> <item android:id=”@+id/menuSortNewest” android:title=”Sort by newest” /> <item android:id=”@+id/menuSortRating” android:title=”Sort by rating” /> </menu> </item> Second option: menu/options.xml: <menu xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:id=”@+id/menuSort” android:showAsAction=”ifRoom” android:actionLayout=”@layout/action_sort” /> </menu> layout/action_sort.xml: <Spinner xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:background=”@drawable/ic_menu_refresh” android:entries=”@array/order” /> Docs for menu resources – http://developer.android.com/guide/topics/resources/menu-resource.html