Hi there, everything i click on my fragment. It adds a menu button to action bar . So if i click it 3 times. It adds the menu button 3 times..
Does anyone know where went wrong???
This is my code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i = null;
switch (item.getItemId()) {
case R.id.refresher:
i = new Intent(this.context, CameraInfoActivity.class);
startActivity(i);
return true;
case R.id.settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_indoor, menu);
}
and i added
setHasOptionsMenu(true);
at my OnCreateView .
this is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresher"
android:menuCategory="system"
android:orderInCategory="100"
android:icon="#drawable/ic_action_refresh"
android:title="#string/menu_add"
android:showAsAction="always" />
<item android:id="#+id/settings"
android:menuCategory="system"
android:orderInCategory="100"
android:icon="#drawable/ic_action_settings"
android:title="#string/menu_settings"
android:showAsAction="never" />
try to remove this code:
inflater = getActivity().getMenuInflater();
you're already have inflater
try to call
setHasOptionsMenu(true);
in fragment's constructor, not in onCreateView
Related
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.
I have an Activity Implementing a ViewPager. It has two Fragments. I want to add ActionItemson ActionBar According to the which Fragment is active currently. I am not Adding menu items on Activity. Here is my code for both fragments.
For Fragment A :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
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 Fragment B:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
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"/>
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)
}
}
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 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>