onCreateOptionsMenu not showing up - android

I have activity with three tabs which contain fragment each. I have created menu but its not showing up , My min sdk version is 14, I have tried on different devices but can't figure out the error. Code is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_search:
Toast.makeText(getApplicationContext(), "search", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_add:
Toast.makeText(getApplicationContext(), "add", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_share:
Toast.makeText(getApplicationContext(), "share", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_feedback:
Toast.makeText(getApplicationContext(), "feedback", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_about:
Toast.makeText(getApplicationContext(), "about", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_quit:
Toast.makeText(getApplicationContext(), "quit", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
XML is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuitem_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/menu_search">
</item>
<item
android:id="#+id/menuitem_add"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="ADD">
</item>
<item
android:id="#+id/menuitem_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="ifRoom"
android:title="SHARE"
>
</item>
<item
android:id="#+id/menuitem_feedback"
android:icon="#android:drawable/ic_menu_help"
android:title="FEEDBACK"
android:showAsAction="ifRoom">
</item>
<item
android:id="#+id/menuitem_about"
android:icon="#android:drawable/ic_menu_info_details"
android:title="ABOUT">
</item>
<item
android:id="#+id/menuitem_quit"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:showAsAction="always"
android:title="QUIT">
</item>
</menu>
please help, Thanks in advance !!

Related

ActionItems of 2nd Fragment are only showing on both fragments in ViewPager

I have two Fragments in created by ViewPager's MainAvtivity. I want to add ActionItemson ActionBar According to the which Fragment is active currently. I am not Adding menu items on Activity. But only the ActionItems of 2nd Fragment are only showing on both fragments.
Here is my code :
For 1stFragment :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_one, menu);
MenuItem searchItem = menu.findItem(R.id.searchWidgetOne);
SearchView search = new SearchView(getActivity());
search.setOnQueryTextListener(this);
searchItem.setActionView(search);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_items_one:
Intent addCrInent = new Intent(getActivity(), ActivityA.class);
startActivity(addCrInent);
return true;
case R.id.action_items_two:
Intent addStIntent = new Intent(getActivity(), ActivityB.class);
startActivity(addStIntent);
return true;
default:
return false;
}
}
For 2ndFragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_second, menu);
MenuItem searchItem = menu.findItem(R.id.searchWidgetTwo);
SearchView search = new SearchView(getActivity());
search.setOnQueryTextListener(this);
searchItem.setActionView(search);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_items_one_secondMenu:
Intent addCrInent = new Intent(getActivity(), ActivityA.class);
startActivity(addCrInent);
return true;
case R.id.action_items_two_secondMenu:
Intent addStIntent = new Intent(getActivity(), ActivityB.class);
startActivity(addStIntent);
return true;
default:
return false;
}
}
For Both Fragments, in MY onCreate i have written this line :
setHasOptionsMenu(true);
Here is my menu_one.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/searchWidgetOne"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
android:title="#string/action_search"/>
<item
android:id="#+id/action_items_one"
android:icon="#drawable/ic_action_new_event"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="#string/add_course"/>
<item
android:id="#+id/action_items_two"
android:icon="#drawable/ic_action_add_person"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="#string/add_student"/>
And Here is my menu_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/searchWidgetTwo"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
android:title="#string/action_search"/>
<item
android:id="#+id/action_items_one_secondMenu"
android:icon="#drawable/ic_action_new_event"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="#string/add_course"/>
<item
android:id="#+id/action_items_two_secondMenu"
android:icon="#drawable/ic_action_add_person"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="#string/add_student"/>
I have also used ivalidateOptionMenu() in onPageSelected() but in vain.

How can I create android menu

I want to create menu like this menu
because when I did like in the link it is appear on the top and I want it down as in the link
This is my java File look at to these code and I am waiting for answers please
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.undo:
Toast.makeText(Map.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.track:
Toast.makeText(Map.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.Hyper:
Toast.makeText(Map.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my XML file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/undo"
android:icon="#drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="#+id/track"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/Hyper"
android:icon="#drawable/icon_search"
android:title="Search" />
</menu>
You're looking for Android Action Bar menu itens:
Take a look: http://developer.android.com/guide/topics/ui/actionbar.html

How to customize the ShareActionProvider in Android

How to customize the menus in this image. can anyone tell the solution
I want to display four submenus(FB, Google+, Twitter and SeeAll) If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,)
Code :
Menu.xml:-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
Activity:-
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
Try This :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_location_found"
android:clickable="true"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="Share">
<menu>
<item
android:id="#+id/facebook"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Facebook">
</item>
<item
android:id="#+id/twitter"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Twitter">
</item>
<item
android:id="#+id/gplus"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Google Plus">
</item>
<item
android:id="#+id/seeall"
android:orderInCategory="4"
android:showAsAction="never"
android:title="See All">
</item>
</menu>
</item>
</menu>
Activity (Java Code):-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.facebook:
Toast.makeText(getApplicationContext(), "Testing", 1000).show();
// write the code here, what you want the action when user click
// facebook submenu....
break;
case R.id.twitter:
break;
case R.id.gplus:
break;
case R.id.seeall:
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
}
Kotlin Code : -
class MainActivity:Activity() {
protected fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onCreateOptionsMenu(menu:Menu):Boolean {
val inflater = getMenuInflater()
inflater.inflate(R.menu.main, menu)
return super.onCreateOptionsMenu(menu)
}
fun onOptionsItemSelected(item:MenuItem):Boolean {
when (item.getItemId()) {
R.id.facebook ->
Toast.makeText(getApplicationContext(), "Testing", 1000).show()
R.id.twitter ->
{ //code
}
R.id.gplus ->
{ //code
}
R.id.seeall ->
{ //code
}
else -> return true
}// write the code here, what you want the action when user click
// facebook submenu....
return super.onOptionsItemSelected(item)
}
}

How to add an option menu item?

I'm totally new in android and I'm doing an app that change the background color when I press a button. I have already done that but now I want to add a menu item which will start the mode with or without buttons.
I was trying to do this but I don't know if I'm doing good.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item){
switch(item.getItemId()) {
case R.id.action_settings:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hidden").
setPositiveButton("OK", null).
create().
show();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
and XML
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Hidden"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Visible"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Cancel"/>
</menu>
First edit menu items id: from android:id="#+id/action_settings" to android:id="#+id/action_hidden"; from android:id="#+id/action_settings" to android:id="#+id/action_visible";
from android:id="#+id/action_exit" to android:id="#+id/action_cancel";
XML file:
<menu>
<item
android:id="#+id/action_hidden"
android:title="Hidden"/>
<item
android:id="#+id/action_visible"
android:title="Visible"/>
<item
android:id="#+id/action_cancel"
android:title="Cancel"/>
</menu>
Then use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater menuInflater = getMenuInflater();
getMenuInflater().inflate(R.menu.YOUR_ACTIVITY!!!!, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_hidden:
//code for action_hidden
return true;
case R.id.action_visible:
//code for action_visible
return true;
case R.id.menu_cancel:
//code for menu_cancel
return true;
default:
return super.onOptionsItemSelected(item);
}
}

when i select menu option once. onOptionsItemSelected is called twice. (Android)

When my menu is clicked two times onoptionitemselected is called. how to stop it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.docmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.upload:
Log.e("testing", "called");
return true;
case R.id.back:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
my menu xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/upload"
android:icon="#drawable/menu_upload"
android:title="#string/upload" />
<item android:id="#+id/back"
android:icon="#drawable/menu_back"
android:title="#string/back" />
</menu>
when upload icon is selected. In log testing called is printed two times.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.Aboutus:
final Dialog d1 = new Dialog(Welcome.this);
d1.setContentView(R.layout.aboutus);
d1.show();
break;
And make sure that you have created folder under res named menu. and make new menu.xml file
and put code like this in menu.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/Aboutus"
android:title="About Us" android:icon="#drawable/ic_menu_about_us" />
<item android:id="#+id/Settings"
android:title="Settings" android:icon="#drawable/ic_menu_settings"/>
<item android:id="#+id/help"
android:title="Help" android:icon="#drawable/ic_menu_help" />
onOptionsItemSelected return true is ok
Try this code...
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_LONG).show();
break;
case R.id.my_settings:
Toast.makeText(getApplicationContext(), "Home Page", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Exit", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
And create a new xml in menu folder and apply this code.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/my_settings"
android:title="#string/my_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

Categories

Resources