I did this menu and when I click a menu item nothing happens, it doesnt't even shows the default toast! Maan I dont know what is going on!
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuLapiz" android:alphabeticShortcut="l"
android:icon="#drawable/pencil" />
<item android:id="#+id/menuCirculo" android:alphabeticShortcut="c"
android:icon="#drawable/circulo" />
<item android:id="#+id/menuTriangulo" android:alphabeticShortcut="t"
android:icon="#drawable/triangulo" />
<item android:id="#+id/menuCuadrado" android:alphabeticShortcut="s"
android:icon="#drawable/cuadrado" /> </menu>
DrawActivity.java
... Here I got the menu onOptionsSelected which appereantly is not working
import android.app.Activity
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast; `
public class DrawActivity extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Title
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuLapiz:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
default:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
}
}}
I have no idea why the onOptionsItemSelected(MenuItem item) method is not working I already tried without the S of Options because I read that suggestion in other post but it did not work.
I'm new to android programming so I really have no clue of what is going on .. any help will be appreciated, thanks a lot!! pd: let me know if you want to see the DrawView.java class
Add a return true; to the end of each case
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mAdd:
startActivity(new Intent("com.example.ADD"));
return true;
case R.id.mAbout:
startActivity(new Intent("com.example.ABOUT"));
return true;
}
return false;
}
EDIT: toast code
Toast t = Toast.makeText(getApplicationContext(),
"This is a toast",
Toast.LENGTH_SHORT);
t.show();
Related
I'm trying to create the Action Bar by following the Android Studio tutorial. I am familiar with VBA, C++, and Python, but this is the first time I've ventured into an App, which uses java, xml, etc. in one. I say that because I think I've misplaced (or misnamed) something in my code, as when I try to run this, I get the error error: duplicate class: com.example.batman.myfirstapp.MyActivity.
I don't understand how it's a duplicate, I only have one "MyActivity.java", so am thinking that I can't do more than one public class MyActivity extends ... per .java file?
Here's what I have (please let me know if I need to include other code):
MyActivity.java
package com.example.batman.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v7.app.*;
public class MyActivity extends ActionBarActivity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
/**
* Called when the user clicks the Send button
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myfirstapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
myfirstapp:showAsAction="never" />
</menu>
Again, I'm not sure what other files (strings.xml, DisplayMessageActivity.java, etc.) would be helpful to debug the above, so let me know what else I can include.
Thank you for any advice/help!
You're intuition is correct! You can only have one MyActivity class in your file, here's what it might look like:
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Your assumption is correct. In your .java file you have two classes named MyActivity. You can only have one.
In my app menu icon is not displaying, For me at present text/string is displaying but i need icon/image intent of text. Also try using xml format menu calling.
Android version is ver_4
cordova_2.5.0
Image/Icon size=32*32
testapp.java
package com.example.testapp;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import org.apache.cordova.*;
import android.view.Menu;
import android.view.MenuItem;
public class testapp extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
private static final int HOME = 0;
private static final int SETTINGS = 1;
private static final int HELP = 2;
#SuppressLint("NewApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, HOME, 0, "Home")
.setIcon(R.drawable.icon_home)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SETTINGS, 0, "Settings")
.setIcon(R.drawable.icon_settings)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, HELP, 0, "Help")
.setIcon(R.drawable.icon_help)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case HOME:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, testapp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case SETTINGS:
this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
return true;
case HELP:
this.appView.sendJavascript("navigator.notification.alert('No help')");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
My testapp.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_home"
android:icon="#drawable/icon_home"
android:showAsAction="ifRoom|withText"
android:title="Home" />
<item android:id="#+id/menu_settings"
android:icon="#drawable/icon_settings"
android:showAsAction="ifRoom|withText"
android:title="Settings" />
<item android:id="#+id/menu_help"
android:icon="#drawable/icon_help"
android:title="Help" />
</menu>
May be... You should remove: android:theme="#style/AppTheme" in AndroidManifest.xml
Or edit: android:theme="#android:style/Theme"
I have made an app with a lot of buttons and activities. I'm having trouble though understanding how to start a new activity through a button that is in my menu (when the menu button is clicked on phone) (inflatable menu). This is my code for the menu connected to my activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1" android:title="#string/menu_home"></item>
</menu>
Here is my activities in Java:
package com.gmail.derekcraigsmith.nanaimobus;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class Route1TimesCcMonfriAActivity extends Activity implements
OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route1_times_cc_monfri_a);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
return true;
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
}
int id = item.getItemId();
switch (id) {
case R.id.your_menu_item_id : {
startActivity(new Intent(start_activity, next_activity));
}
where start activity is your main activity and next_activity is the activity you want to start.
Give a look on my blog here for more info.
activity_main.xml in menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/next"
android:title="Next" />
</menu>
In your activity inflate the menu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) //get the id which is an int
{
case R.id.next: // check if its the menu item next selected
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
break;
default:
return super.onOptionsItemSelected(item);
}
}
I'm using SlidingMenu implementation in my app, I want the android.R.id.home button to open/close the side menu. Inside the Activity I use Fragment to display information. I want the home button to act as a back button.
The problem is that Activity's onOptionsItemSelected in Activity get invoked before the Fragment's one. Is this ordinary behavior? Or am I doing something wrong?
I'm using ActionBarSherlock in my project as well, but I don't think that matters tho.
Is implementing my own interface the only solution here?
Exactly last night I was struggling with this but eventually managed to solve it, so here is my solution:
These are the relevant parts from MainActivity:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class MainActivity extends SherlockFragmentActivity {
.
.
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
return super.onOptionsItemSelected(item);
}
}
Here is my menu main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
and here is my Fragment:
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class TestFrag extends SherlockFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(Menu.NONE, android.R.id.home, 100, "Home");
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId())
{
case android.R.id.home:
// Do whatever you want when Home is clicked.
Toast.makeText(getSherlockActivity(), "Home is clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This is what I am getting:
I hope this helps in any way.
Ended up moving the onOptionsItemSelected from Activity to my base Fragment class instead.
Inside base fragment class, I have these:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
// Toggle slide menu
getBaseActivity().getSlidingMenu().toggle(true);
break;
}
return super.onOptionsItemSelected(item);
}
protected boolean useHomeAsBack(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
Log.v(TAG, "useHomeAsBack - onOptionsItemSelected");
getSherlockActivity().onBackPressed();
return true;
}
return false;
}
Also call setHasOptionsMenu(true); inside onAttach as well.
Inside the actual Fragment that I wanted to use back instead, I have:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(useHomeAsBack(item)) return true;
return super.onOptionsItemSelected(item);
}
Still, I'd expect Fragment's onOptionsItemSelected to be able to override or take priority over Activity's. Wonder what the reason for that would be.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android, How to create option Menu
i have tried all the things but i am not able to create option menus.can u please give me full code for that ? should i make any changes in manifest file to create menus ? should i create another class only for menu or i can write it in any class created before ? please help me.can i write like this :
package com.example.FirstProject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
//import android.widget.EditText;
//import android.widget.RadioButton;
//import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class FirstProjectActivity extends Activity {
/** Called when the activity is first created. */
protected ListAdapter adapter;
Cursor cursor;
protected String[] cities = {"Mumbai"};
ListView lv ;
ListView stations;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void myClickHandler(View view)
{
Intent i=new Intent(this,City.class);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
{
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
if( R.id.icon==item.getItemId()) Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
}
return true;
}
}
Use code like below:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
try with this...
public boolean onOptionsItemSelected(MenuItem item)
{
System.out.println("entered to option selected");
switch(item.getItemId())
{
case R.id.icon:
System.out.println("entered to icon");
Toast.makeText(this, "icon clicked", Toast.LENGTH_LONG).show();
break;
case R.id.text:
Toast.makeText(this, "text clicked", Toast.LENGTH_LONG).show();
break;
case R.id.iconandtext:
Toast.makeText(this, "icon and text clicked", Toast.LENGTH_LONG).show();
break;
}
return true;
}
refer this link.. also it will be usefull enter link description here
You can use the following code inside the menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu1"
android:alphabeticShortcut="s"
android:title="menu1 title"
/>
<item
android:id="#+id/menu2"
android:alphabeticShortcut="t"
android:title="menu2 title"
/>
</menu>
now the two methods for the invoking the menu and function which is to be called on the item selected.
#override
public boolean onCreateOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
//menu.add(R.menu.main_menu);
//menu.add(Menu.NONE, 101,Menu.FIRST,this.getResources().getString(R.string.Title));
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.main_menu, menu);
return true;
}
you can use the both the method of invoking the menu. First one by the menu.add and second one by the inflating the menu.xml file
Now on the itemselectedlistner is used by the following code
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu1:
// your code here
case R.id.menu2:
// your code here
}
return false;
}
just pass the id in switch-case and do whatever you want by selecting the those menu.