My menu_main.xml do not show on my main_activity - java

I just a newbie in android programming, i am building a new app from a tutorial.
I have make the main menu like this
<?xml version="1.0" encoding="utf-8"?> <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="id.go.kemenkeu.itjen_kemenkeu.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="Settings" />
<item
android:id="#+id/navigate"
android:title="#string/next"
android:orderInCategory="200"
app:showAsAction="always"
android:icon="#drawable/ic_next"/> </menu>
and the MainActivity.Java like this
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings){
Toast.makeText(this,"Hi, Anda Baru Saja Menekan"+item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
if(id==R.id.navigate){
startActivity(new Intent(this, SubActivity.class));
}
return super.onOptionsItemSelected(item);
} }
I call the +id/navigate in MainActivity.Java, but when i run in emulator, the style inside +id/navigate didn't show. What is wrong here?

you have to override createoptionmenu for intialize your own menu in toolbar.
try adding this in you activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu_main, menu);
return super.onCreateOptionsMenu(menu);
}

without onCreateOptionsMenu you cant show menu. so add onCreateOptionsMenu to your Activity Class like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
Happy Coding!!!

You must inflate the option menu
Add this line.
getMenuInflater().inflate(R.menu.main, menu);

You have to override the onCreateOptionsMenu in you Activity and inflate the menu and check it
more details you have to check in Developer Site

Related

Action Bar - Missing Menu

As I am new to Android Studio I have one question to ask here and I hope You guys don't mind.
So I created action menu that should have 3 doots at right upper end and that should call menu.
Anyway here is my menu:
In my main activity I am able to see menu but I am not able to see title and 3 doots.
Code of my main.java class (LocationInit):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.GoogleMaps:
Intent googlemaps = new Intent(this, googlemaps.class);
startActivity(googlemaps);
break;
case R.id.Settings:
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
break;
case R.id.LastLocation:
Toast.makeText(getApplicationContext(),"Help", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Finally my app_bar.xml (XML Of action bar):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/CustomToolbarStyle"
app:popupTheme="#style/CustomPopupTheme">
</android.support.v7.widget.Toolbar>
main_menu.xml
code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_main_setting"
android:icon="#drawable/ic_settings"
android:orderInCategory="100"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
<item
android:id="#+id/menu_main_setting2"
android:icon="#drawable/ic_settings"
android:orderInCategory="200"
app:showAsAction="always"
android:actionLayout="#layout/toolbar"
android:title="Setting" />
</menu>
and just override onCreateOptionsMenu like this in your MainPage.java
#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, menu);
return true;
}
and in onCreate() add this
toolbar.inflateMenu(R.menu.main_manu);

Items in menu not showing action bar, rather being displayed without any icons in three dots

Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"

Remove/Hide searchView on only one activity

I have 3 activities in my project and I would like to remove/hide the searchView in the actionbar on only one activity.
I tried searchView.setVisibility(View.GONE); but there is still the clickable icon.
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/item_clear_memory_cache"
android:title="#string/menu_item_clear_memory_cache"/>
<item
android:id="#+id/item_clear_disc_cache"
android:title="#string/menu_item_clear_disc_cache"/>
</menu>
menu.findItem(R.id.menu_search).setVisible(false); should be the way.
You can hide the searchview and the searchicon by doing this:
searchItem.setVisible(false);
searchView.setVisibility(View.GONE);
Then you can bring it back by:
searchItem.setVisible(true);
searchView.setVisibility(View.VISIBLE);
Well, after some researches here you can find my complete example. Don't forget to set visibility and visible back in activity or fragment where you need to show your search view back.
I was looking a way to hide search view in preferences.
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this means that we're telling the Activity that is hosting the Fragment that we want to display menus
setHasOptionsMenu(true);
}
/**
* ********************* SEARCH ********************
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) item.getActionView();
item.setVisible(false);
searchView.setVisibility(View.GONE);
}
}
And menu_main.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
/>

Cannot cast from ActionProvider to ShareActionProvider

I am trying to implement a ShareActionProvider in my ActionBarSherlock.
The following code gives me this error :
Cannot cast from ActionProvider to ShareActionProvider
ReadingActivity.java
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2b, (Menu) menu);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.getItem(1).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem menuItem = menu.findItem(R.id.share);
// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
// Set the default share intent
shareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
menu2b.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
...
<item android:id="#+id/share"
android:title="Share"
android:icon="#drawable/ic_share_action_bar"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
You should be using the ShareActionProvider from ABS, not the stock one.
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"

Android onCreateOptionMenu not being called

I am implementing an Android Activity from which other Activities will be derived from. So basically I have this setup of an InventoryActivity and its parent class, ListActivity:
public class MyListActivity extends Activity {
protected Context mContext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this.getBaseContext();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
Log.d("Creating options menu", "True");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d("Preparing options menu", "True");
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return(true);
case R.id.revert:
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
public class InventoryActivity extends MyListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
}
}
And I also have this in options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/card_list_save"
android:icon="#drawable/ic_menu_save"
android:title="Save"/>
<item android:id="#+id/card_list_revert"
android:icon="#drawable/ic_menu_revert"
android:title="Revert" />
</menu>
If necessary, this is my layout for inventory.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/callSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/inventory"/>
</ScrollView>
</LinearLayout>
However, when I press the menu button, nothing happens. The Log messages in the onCreateOptionsMenu method does not appear. Instead all I can see is the following:
02-04 11:36:58.313: W/KeyCharacterMap(31464): No keyboard for id 0
02-04 11:36:58.313: W/KeyCharacterMap(31464): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
But what baffles me the most is that this code works in other Activities, such as my launcher Activity. But by the concept of object oriented programming, the InventoryActivity should call the overriding methods in the MyListActivity. I am completely stuck and I need help.
ListActivity is already a class in the Android SDK. My guess is that you're importing android.app.ListActivity, and not your package.
Hmm...don't know why but removing the onCreate method in MyListActivity fixed the problem. So the class now looks like this:
public class MyListActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
Log.d("Creating options menu", "True");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d("Preparing options menu", "True");
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return(true);
case R.id.revert:
return(true);
}
return(super.onOptionsItemSelected(item));
}
}

Categories