I am trying to use the menu inflator to give the user the option to email me for support, but everytime I hit the menu button on the emulator it will do nothing.
Would I need to edit this in my manifest? My xml has menu as the title and items for the well items.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/email" android:title="#string/email_menu"
android:icon="#drawable/ic_envelope" android:onClick="emailme" />
<item android:id="#+id/test1" android:title="#string/test1"
android:icon="#drawable/ic_dashboard" android:onClick="test1" />
</menu>
Activity:
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MenuButton extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.menu.menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.email:
emailme();
return true;
case R.id.test1:
test1();
return false;
default:
return super.onOptionsItemSelected(item);
}
}
private void test1() {
// TODO Auto-generated method stub
}
private void emailme() {
// TODO Auto-generated method stub
String domsEmail = "MYEMAIL#EXAMPLE.com";
String message = "Hello, I just want to let you know that your app";
String myemail[] = { domsEmail };
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, myemail);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "your app");
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(emailIntent);
}
}
Here are various issues with your code:
Please have onCreateOptionsMenu() use return(super.onCreateOptionsMenu(...)) with the same parameters that you receive -- in other words, chain to the superclass when you are done
Your android:onClick attributes will only work on API Level 11 and higher, and your methods that you have tied to them need to be public and have a MenuItem parameter (which yours do not)
Your MIME type in emailme() needs to be text/plain, not plain/text
Related
This is the program i am doing I have created two items in main.xml i.e "About Us" And "Preferences" which i want to use in my program but when i am trying to access the main.xml using inflate method in MenuInflater it cannot find my resource.
package com.sagar.firstapp;
import android.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[]={"StartingPoint","TextPlay","Email","Camera","Data","example5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(Menu.this,android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese=classes[position];
try {
Class ourclass=Class.forName("com.sagar.firstapp."+cheese);
Intent ourintent=new Intent(Menu.this,ourclass);
startActivity(ourintent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowup=getMenuInflater();
blowup.inflate(R.menu.main, menu);/*Can't find my main.xml resource*/
return true;
}
}
here is my main.xml code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="About Us"
android:id="#+id/aboutus"
android:numericShortcut="1"
android:alphabeticShortcut="a"/>
<item
android:title="Preferences"
android:id="#+id/preferences"
/>
R.java(Generated lines)
public static final int aboutus=0x7f09001d;
public static final int preferences=0x7f09001e;
public static final class menu {
public static final int main=0x7f080000;
}
Create a main.xml under res/menu folder
main.xml
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1"
android:title="About Us"/>
<item android:id="#+id/item2"
android:title="Preferences"/>
</menu>
and in your Activity call inflate() in onCreateOptionsMenu(menu menu)
#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 Resource, Menu
return true;
}
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.
I'm trying to display the Search Icon in the top right of my Actionbar.
See Code below for the PublicVideos.java that extends an ActionBarActivity
The getoverflow manages to display the overflow menu, but it does not display the search icon. I have added the different sizes search icons in their respective drawables folders.
package faith.faithconnect;
import java.lang.reflect.Field;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
public class PublicVideosActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_public_videos);
centreLogo();
getOverflowMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.faithmenu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void getOverflowMenu() {
// TODO Auto-generated method stub
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void centreLogo() {
// TODO Auto-generated method stub
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#F7CE04")));
// getOverflowMenu();
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setCustomView(R.layout.public_videos_view);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent backhome = new Intent(this, MainActivity.class);
startActivity(backhome);
}
// public void mingleSwipe(View view)
// {
// Intent mingleintent = new Intent(getApplicationContext(),
// MingleActivity.class );
// startActivity(mingleintent);
//
// }
public void ratingSelected(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// Try Google play
intent.setData(Uri
.parse("market://details?id=com.cubeactive.qnotelistfree"));
startActivity(intent);
}
public void shareSelected(View view) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
shareIntent.putExtra(Intent.EXTRA_TEXT, "body here");
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
The faithmenu.xml below
<?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/action_search"
android:title="search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" ></item>
</menu>
You should use name space app for showAsAction, so it should be:
<?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/action_search"
android:title="search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" > </item>
</menu>
When your item have android:showAsAction="ifRoom" If there's not enough room for the item in the action bar, it will appear in the action overflow.
You can also use "always" (android:showAsAction="always") to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room.
Going by the guidelines and conventions in the StopWatch GDK example, I cannot get the MenuOption to open.
My app compiles and able to output the embedded log statement of ""####TEST", but no OptionsMenu appears.
https://developers.google.com/glass/develop/gdk/ui/immersion-menus
This is the method in the Android API for menu's.
openOptionsMenu();
I'm basing the code off of stopwatch's conventions:
/*
*
* Menu Code
*/
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
openOptionsMenu();
Log.v("####","TEST");
return true;
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Implement if needed
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection. Menu items typically start another
// activity, start a service, or broadcast another intent.
switch (item.getItemId()) {
case R.id.stop:
//startActivity(new Intent(this, StopStopWatchActivity.class));
Log.v("####","HI");
return true;
case R.id.read_aloud:
Log.v("####","READ_ALOUD");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//open the optionsMenu to make sure
#Override
public void openOptionsMenu() {
super.openOptionsMenu();
}
the XML for menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/read_aloud"
android:title="#string/read_aloud"
android:icon="#drawable/ic_launcher"/>
<item
android:id="#+id/stop"
android:title="#string/stop"
android:icon="#drawable/ic_launcher"/>
</menu>
edit: I discovered what my issue was. I commented out the prepared option which was returning false, which caused my options to not engaged.
Here in reference is a good example to do it properly.
Thanks #w9jds for his help.
I can't find what the issue was in my code, but here is a working example solutions with the correct menu creation and one tap.
https://github.com/w9jds/GlassMenuExample
What the program does is show a glass app that renders a hello world card. On Tap the card creates an options menu that has the 'share' option.
This functionality is a lot similar to how cards work on the timeline.
Menu - Main.Xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/share_menu_item"
android:title="#string/share_label"
android:icon="#drawable/ic_share_50"/>
</menu>
Main Activity
package com.example.glassmenuexample;
import com.google.android.glass.app.Card;
import com.google.android.glass.media.Sounds;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
public class MainActivity extends Activity
{
private GestureDetector mGestureDetector;
private AudioManager maManager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//initialize the audio manager
maManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
//create gesture listener
mGestureDetector = createGestureDetector(this);
//create a new card for the view
Card cView = new Card(this);
//set the text of the card to the hello world string
cView.setText(R.string.hello_world);
//set the card as the content view
setContentView(cView.toView());
}
#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);
return true;
}
private GestureDetector createGestureDetector(Context context)
{
GestureDetector gdDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gdDetector.setBaseListener( new GestureDetector.BaseListener()
{
#Override
public boolean onGesture(Gesture gesture)
{
if (gesture == Gesture.TAP)
{
//play the tap sound
maManager.playSoundEffect(Sounds.TAP);
//open the menu
openOptionsMenu();
return true;
}
return false;
}
});
return gdDetector;
}
#Override
public boolean onGenericMotionEvent(MotionEvent event)
{
if (mGestureDetector != null)
return mGestureDetector.onMotionEvent(event);
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection. Menu items typically start another
// activity, start a service, or broadcast another intent.
switch (item.getItemId())
{
case R.id.share_menu_item:
//do something on menu item click
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
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);
}
}