I'm trying to create an android context menu (the one that pops-up when you press the 'menu' button'). I've read all the tutorials I could find and nothing helped. I'm new to android developing.
I've created the menu.xml file but I don't understand how to give functionality to ID's. This is how my code looks:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing that I don't get: what to do with 'newGame();' and 'showHelp();'. I wish that when I click on a menu button a new activity starts. How do I do it?
First thing is you have code is for option menu not for context menu
you can call new activity like below
You can directly call a new activity without using option menu by
Intent myIntent = new Intent(this, NewGame.class);
startActivity(myIntent);
if you want to give option to user on press menu button then try below code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "New Game");
menu.add(0, 1, 1, "Help");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().toString.equalsIgnoreCase("New Game")) {
Intent intent = new Intent(this, NewGame.class);
startActivity(intent);
finish();
}
else if(item.getTitle().toString.equalsIgnoreCase("Help")) {
Toast.makeText(getBaseContext(), "Help", 2000).show();
}
}
Intent intent = new Intent(this, NewGame.class);
startActivity(intent);
Have you read about how activity works?
This starts the activity NewGame
Intent myIntent = new Intent(this, NewGame.class);
startActivity(myIntent);
Related
I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?
Following are my code snippets.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi") #Override
public boolean onOptionsItemSelected(MenuItem item) {
View menuItemView = findViewById(R.id.action_button);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup);
popupMenu.show();
return true;
}
and my popup menu is as follows,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/one"
android:title="About"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
<item
android:id="#+id/two"
android:title="Contact Us"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
</menu>
What i want to do is, when i click on these menu items another activities has to be opened. How could i do that?
Can someone help me please. Thanks in advance.
Use the id to launch the activity using switch statement with menu itemId
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.one:
Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
startActivity(intent1);
return true;
case R.id.two:
Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
To open activity on popup menu click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
A sliding menu was implemented using google navigation drawer with actionbar class. My problem is onCreateOptionsMenu is being shown in every activities. how can i make onCreateOptionsMenu icon visible and invisible at will. Any idea please.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Either you create menu runtime or have different menu layouts as per your need and in onCreateOptionsMenu of your activity set that layout or runtime create those menus or if you want to show no menu icons then just do menu.clear()
Activity A
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_a, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Activity B
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.clear();
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_b, menu);
// OR
Drawable tmpDrawable = getResources().getDrawable(R.drawable.share_sharingicon);
// tmpDrawable.setColorFilter(getResources().getColor(R.color.colorGrayFont), PorterDuff.Mode.MULTIPLY);
menu.add("ShareMap").setIcon(tmpDrawable).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Directions").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ShareMap:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
case R.id.Directions:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
To hide or show some icon on action bar, you need override method:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem your_icon = menu.findItem(R.id.action_your_icon);
//show icon
your_icon.setvisible(true);
//hide icon
your_icon.setvisible(false);
...
}
Besides you need 'supportInvalidateOptionsMenu()' to Invalidate the activity's options menu when action bar items have some change
I am creating a menu, but both of menu options were intent to same class so how to fix this? Sorry I am a starter developer, so, please give me solution of that problem.
public void btnclick(View v){
openOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0, 1, 0, "What's in it");
menu.add(0, 2, 0,"send");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intt=new Intent(this,Help.class);
startActivity(intt);
return true;
}
Since you are adding 2 menu items at the onCreateOptionsMenu: 1st with ItemId = 1 and 2nd with ItemId = 2, you can use getItemId() to distinguish between menu items, like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case 1:
//do stuff like:
Intent intt=new Intent(this,Help.class);
startActivity(intt);
break;
case 2:
//do another stuff, like launching another activity
break;
default:
break;
}
return true;
}
I'd like to customize options menu in my app. After some googling and making a lot of effort I am still unable to do it. Here is what I want:
menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_first"
android:icon="#drawable/phone_selector"
android:title="#string/first"/>
<item
android:id="#+id/menu_second"
android:icon="#drawable/butterfly_selector"
android:title="#string/second"/>
<item
android:id="#+id/menu_third"
android:icon="#drawable/umbrella_selector"
android:title="#string/third"/>
</menu>
OptionsMenuActivity.java:
public class OptionsMenuActivity extends Activity {
private Menu currentMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
this.currentMenu = menu;
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
setMenuItemIconState(menu, MyApplication.getSelectedMenuItemDrawIcon(), false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_phone:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.phone_active);
Intent intent = new Intent(OptionsMenuActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_butterfly:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.butterfly_active);
intent = new Intent(OptionsMenuActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_umbrella:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.umbrella_active);
intent = new Intent(OptionsMenuActivity.this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setMenuItemIconState(Menu menu, int menuItemIconRes, boolean switchLastActiveMenuItemIcon) {
int menuItemId = 0;
int lastDeActiveDrawIcon = 0;
switch (menuItemIconRes) {
case R.drawable.phone_active:
menuItemId = R.id.menu_first;
lastDeActiveDrawIcon = R.drawable.phone;
break;
case R.drawable.butterfly_active:
menuItemId = R.id.menu_second;
lastDeActiveDrawIcon = R.drawable.butterfly;
break;
case R.drawable.umbrella_active:
menuItemId = R.id.menu_third;
lastDeActiveDrawIcon = R.drawable.umbrella;
break;
}
if (switchLastActiveMenuItemIcon) {
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(lastDeActiveDrawIcon);
return;
}
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(MyApplication.getSelectedMenuItemDrawIcon());
}
}
In MyApplication.java I have only a getter and a setter of selectedMenuItemDrawIcon.I managed to set the icons properly, but failed with the others.
Is it possible to customize options menu like that? If not, is there any other approaches for creating an xml file and make it appear and disappear smoothly just like basic menu of android.
Any help would be appreciated. Thanks in advance, (please provide with code example).
Hi i have Use Menu in 2 class this is my Menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/setting"
android:icon="#drawable/ic_seting"
android:title="Setting">
</item>
</menu>
in class A i have use this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options, menu);
return true;
}
public boolean onCreateOptionsMenu(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.SavedList:
Intent intent = new Intent(
A.this,
SetPreference.class);
startActivity(intent);
return true;
}
return true;
}
and in class b i have use
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options, menu);
return true;
}
public boolean onCreateOptionsMenu(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.SavedList:
Intent intent = new Intent(
b.this,
SetPreference.class);
startActivity(intent);
return true;
}
return true;
}
in one class b its working fine but in class its not working please tell me where i m doing wrong please help me i am new n android
When you are using Intent that time why need two different class with same body. What I suggest is In case body write a Intent for class B menu.. see whether work or not .
Go through the documentation of the Intent in Android and Also menu Android documentation you will find more information .