MenuBar never option - android

I'm building an app with AndroidStudio.
I have a problem with my Menu.
This is my menu file .xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_logout"
android:orderInCategory="100"
android:title="Logout"/>
</menu>
This is that I can see from Preview:
In my activity main, I have this code to create this menu:
public class MainActivity extends Activity {
public MyDatabase db;
public ProgressDialog dialog;
public String url="";
private static final Intent SCAN_INTENT = new Intent("com.google.zxing.client.android.SCAN");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
if (menu != null) {
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
But if I try to start my application I can see this:
As you can see, I can't see icon of application and I can't see Option menu.
How can I do this?

You may use lower version android device for that press hardware menu button of device it will shows your menu.My suggestion is to test it on at least above on jelly bean .

Extend AppCompatActivity not Activity. and create menu like below:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
return return super.onCreateOptionsMenu(menu);;
}
and care_home.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/action_settings"
app:showAsAction="always" />
<item
android:id="#+id/action_logout"
android:orderInCategory="100"
android:title="Logout"
app:showAsAction="always" />
For setting AppBar you can read Here.

you are extending Activity only that's why you are unable to see the toolbar or action.
public class MainActivity extends Activity {}
instead use this :
public class MainActivity extends AppCompatActivity{
For more please read the android documentation about appBars:
Android Docs

First extend your activity with AppCompatActivity rather Activity like:
public class MainActivity extends AppCompatActivity{
And return return (super.onCreateOptionsMenu(menu)); it rather true like:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.care_home, menu);
if (menu != null) {
}
return (super.onCreateOptionsMenu(menu));
}

Related

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

Android Action buttons remain from previous view

I have created a master detail view in Android using fragments. (For now I'm considering the phone layout and not the tablet layout).
In my master view, I have a list of items and I show a "add" button in the action bar.
In my detail view, I show the details for the selected item, a "delete" button and a "edit" button in the actionbar.
Now the way I have done it, the "add" button is still present in the action bar in my detail view.
Master class:
public class AvailableZonesFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_available_zones, menu);
}
}
Master menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Add">
</item>
</menu>
Detail class:
public class ZoneDetailsFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_zone_details, menu);
}
}
Detail menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:orderInCategory="1"
app:showAsAction="always"
android:title="Edit">
</item>
<item
android:id="#+id/remove"
android:icon="#android:drawable/ic_menu_delete"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:title="Delete">
</item>
</menu>
Did you try to call
menu.clear();
as first line in onCreateOptionsMenu in your details fragment, to remove menu items from previous fragment.

Android action buttons in action bar not appearing?

I want to create an add button in the action bar but it doesn't seem to appear when i run thee code.
Here is my main_activity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_add) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This is my 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:icon="#drawable/ic_add"
android:title="Add"
android:showAsAction="always"
/>
</menu>
Please Help
I cant figure out what's wrong !
Since your Activity extends ActionBarActivity you would be using AppCompat from support library.
So change to
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_add"
android:icon="#drawable/ic_add"
android:title="Add"
yourapp:showAsAction="always"
/>
...
</menu>
Quoting docs
Notice that the showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
Change your menu_xml to this :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:clickable="true"
android:icon="#drawable/ic_add"
android:showAsAction="always"
android:title="action_location_found">
</menu>
Replace your menu file with below menu file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Theme.AppCompat.Light"
xmlns:tools="http://schemas.android.com/tools"
tools:context="yourpackagename.Mainactivity" >
<item android:id="#+id/action_add"
android:icon="#drawable/add_plush" app:showAsAction="always"></item>
</menu>
thats it...

extend actionbar menu items

I create base activity with actionbar menu functional and extended another activities from it.
public class BaseActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
return true;
}
}
Now I want to add some buttons to the actionbar in some activities. How can i add elements to actionbar and use element from base activity?
You can do that as simple as in BaseActivity just don't forget to call super.onCreateOptionsMenu().
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help" />
</menu>
home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new" />
</menu>
In BaseActivity you are using base menu config main.xml.
public class BaseActivity extends SherlockFragmentActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In child activity you are using another config from home.xml only with additional menu items, no duplicates. But don't forget to call super.onCreateOptionsMenu(menu) with the same menu instance to add elements from BaseActivity (parent menu items would be added after child menu items if you call super's method after inflate, and before otherwise).
public class HomeActivity extends BaseActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
}
You can specify your actions for the actionbar in the main.xml
example :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
And call from your subclass
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Make different xml files containing the buttons you need specific to those Activity. Say for Activity1 you have two buttons and for Activity2 you have one, then you will create 2 xml files like below.
action_activity1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
action_activity2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
</menu>
Then in the onCreateOptionsMenu(Menu menu) method inflate the desired xml file. Like,
Activity1:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity1, menu);
return true;
}
Activity2:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity2, menu);
return true;
}
Please be care full with the syntax (as I used appcompat actionbar). :)
This is what I use. Hope you find it helpful. And, I will be happy to see an easier way than this. :)

How To Creating Dynamic Menu to show on Fragment

I have a class BaseFragment i was create a private menu and set that menu from
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
this.menu = menu;
}
in main.xml i have :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"/>
<!-- Refresh -->
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="ifRoom" />
<!-- Download -->
<item android:id="#+id/action_download"
android:icon="#drawable/ic_action_download"
android:title="#string/action_download"
android:showAsAction="never"/>
<!-- Favorite -->
<item android:id="#+id/action_favorites"
android:icon="#drawable/ic_action_favorites"
android:title="#string/action_favorites"
android:showAsAction="never" />
</menu>
and i have another fragments FragmentA and FragmentB extends to that BaseFragment.
In FragmentA i want to show All menu and all is OK when running the app, but in FragmentB i want to show the download and and favorite whitout search and refresh.
I was try to create a method on base to manipulate the visibility on menuitem and my menu always return null.
protected void setSearchable(boolean isSearchable){
menu.findItem(R.id.action_search).setVisible(isSearchable);
}
protected void setHasRefreshFuction(boolean isHasRefreshFunction){
menu.findItem(R.id.action_refresh).setVisible(isHasRefreshFunction);
}
protected void setDownloadable(boolean isDownloadable){
menu.findItem(R.id.action_download).setVisible(isDownloadable);
}
protected void setFavorite(boolean isFavorite){
menu.findItem(R.id.action_favorites).setVisible(isFavorite);
}
i call that method on onCreate() in FragmentB like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDownloadable(true);
setFavorite(true);
setHasRefreshFuction(false);
setSearchable(false);
}
Can someone help me please.... i was read many tutorial and doesn't find an answer to make this.
note : I was setHasOptionsMenu(true); in oncreate in BaseFragment
The easiest way would be to override onCreateOptionsMenu in FragmentB again.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
menu.findItem(R.id.action_search).setVisible(false);
menu.findItem(R.id.action_refresh).setVisible(false);
this.menu = menu;
}
You can't access menu outside it's callbacks. For you can implement ActionProvider subclass to handle visibility of needed menu items.
Or you can call invalidateOptionsMenu() method of your activity to request Options Menu recreating, and change visibility of items in onCreateOptionsMenu()
In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.
<item
android:id="#+id/action_newItem"
android:icon="#drawable/action_newItem"
android:showAsAction="never"
android:visible="false"
android:title="#string/action_newItem"/>
Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remoev) by overriding onPrepareOptionsMenumethod available in Fragment.
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_newItem).setVisible(true);
super.onPrepareOptionsMenu(menu);
}

Categories

Resources