how to add image buttons in each Android activity?

can any one guide me how to add some menu related buttons at the bottom of each activity?


Asked by: Roland850 | Posted: 25-01-2022






Answer 1

Do you mean the Options Menu?

alt text

If so, you'll need to add some code like this to your Activity:

private static final int MENU_SEARCH = Menu.FIRST;
private static final int MENU_PREFERENCES = Menu.FIRST + 1;
private static final int MENU_HELP = Menu.FIRST + 2;

/* Creates the menu items */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "Search")
            .setIcon(android.R.drawable.ic_menu_search);
    menu.add(Menu.NONE, MENU_PREFERENCES, Menu.NONE, "Preferences")
            .setIcon(android.R.drawable.ic_menu_preferences);
    menu.add(Menu.NONE, MENU_HELP, Menu.NONE, "Help")
            .setIcon(android.R.drawable.ic_menu_help);
    return true;
}

/* Handles item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_SEARCH:
            search();
            return true;
        case MENU_PREFERENCES:
            preferences();
            return true;
        case MENU_HELP:
            showHelp();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

Note that Menu.add() returns the MenuItem created so you can chain the call to setIcon().

Answered by: Sawyer258 | Posted: 26-02-2022



Similar questions

eclipse - How to display custom list activity in Android?

I want to display data in list activity. How do I achieve this? TITLE Date Sub Title Button TITLE Date Sub Title Button TITLE Date Sub Title Button . . . . and so on Does a tutorial exist related to custom list activity?


With lots of help, I have gotten this android class activity down to only one error. Eclipse ask me to add ")" ,"}", and ";" why though?

package com.android.drinksonme; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Screen2 extends Activity { // Declare our Views, so we can access them later private EditText etUsername; pr...


java - Errors in Eclipse and Android activity can't seem to figure it

I keep getting errors trying to implement a couple of OnLongClick buttons. Eclipse keeps telling me to "add unimplemented methods". I'm very new to developing and I'm asking for some help. :D What am I doing wrong here? This community helped me out before and as soon as I get to where I can give back I will. I thank you all for it. Here's my code for a simple app I'm working on. package com.kc.eas; imp...


android - Error in eclipse when running activity

I am starting to program for android, when I first try to run the application from eclipse IDE it worked fine, but now I am getting the following error: emulator: ERROR: unexpected qemud char. channel close I didn't change the configuration or the code i had, also the activity is not starting in the emulator and I can't debug. Can someone tell me what this error means? Than...


Android Eclipse Add New Activity

I am using Win 7.0, Eclipse and android SDK. I want to add new activity in AndroidManifest.xml Application tab as it is shown in this tutorial Android Development – Adding Screens & Button Handlers I add an Activity name to my manifest but it does not automatically turn it into a link. e.g. ...


eclipse - how can i debugg an android service - activity that extends service class -

colleagues, how can i debug an activity that extends the service class? Its not working with the regualar breakpoints of the eclipse debugging tool! I know that you can work with logtags but is this really the only debugging tool?? heres a little code sniped to be able to post this question public class HUJIDataCollectionService extends Service { @Override publ...


Add activity node android eclipse

I understand an activity needs to be defined/described in the AndroidManifest xml file. I do not understand why this does not work as many of the tutorials say it should. If I start a new android project in eclipse running on ubuntu 11.10. Then add a new class derived off (extends) android.app.Activity - then click on AndroidManifest.xml file in the package explorer - I can see the Application nodes box an...


eclipse - Easy Android Activity

I am creating an android app in eclipse and for some reason the .AgainActivity is always launching rather than .Splash. I deleted the .AgainActivity and ran it in the emulator AND the .Splash worked, but it will not run first when both activities are present. Does anyone know what I need to do? AndroidManifest XML: <application android:icon="@drawable/test_logo" android:label="@st...


Eclipse Juno won't create Android Activity

I downloaded Eclipse Juno Java EE edition and installed ADT plugin. I created a new Android Application Project from Eclipse and in the wizard I created an activity called TaskariActivity. After I pressed finish, it created the project but not the activity and the wizard didn't close. I pressed cancel. No activity or anything in the src folder. I created a new activity by...


eclipse - Android Activity Not found - never had before this error

I have a problem that i never had before. I have created an App with eclipse, so when I was finished I started it with eclipse,and everything does work. But when I start it by my self with the icon, there comes a toast message which says: activity not found. I haven´t any idea what to do because the Log says everything goes right. Eclipse can start my App, but i can´t?! Please Help me!! `<?xml...


android - Launch activity at certain time

I'm new to Android development, so I might be missing something obvious. I want to launch an Activity when the user's phone clock hits a specified time (similar to an alarm). However, I'm not sure how I would go about doing this as constant polling of the clock seems inefficient and a waste of resources. Do I need to capture broadcast events from the clock, or use PendingIntents? If someone could ...


android - How to to make 2 MapView on one Activity?

Is it possible to make 2 MapView on one Activity ? If so, How to make it ? I've tried but no luck. Thanks in advance.


android - Need advice in how to update my Activity due to contact info changes

I have an activity which queries and display some Contact Information. It has a button which launches the intent for 'Edit Contact' (the default Android activity). What should I do to refresh my view in case user edits Contact Information? re-query in the onResume() of my activity? add a content observer?


How to change title of Activity in Android?

I am using Window w = getWindow(); w.setTitle("My title"); to change title of my current Activity but it does not seem to work. Can anyone guide me on how to change this?


android - Replace current activity

I need to replace the current activity with a new one. That is, I want to start a new activity and remove the current activity from the task stack. Based on the documentation, it seems the best way would be to start the activity using Activity.startActivity as per usual, and then cal...


android - How to enforce activity if app is running?

I start an Activity from my Widget, which should start a special view. But if the app is already running (not left with back button), Android instead activates the activity that was last shown. Is there any flag or other way to avoid this behaviour? Closing the previous activity wouldn't be a problem in my app, there's no user input that would be lost. I tried a workaround with finish() in onStop(), but onS...


eclipse - How to display custom list activity in Android?

I want to display data in list activity. How do I achieve this? TITLE Date Sub Title Button TITLE Date Sub Title Button TITLE Date Sub Title Button . . . . and so on Does a tutorial exist related to custom list activity?


android dialog activity position

I created an non-maximized activity using android:theme="@android:style/Theme.Dialog" to make it looks like a dialog. I need to change the position of the activity on screen but I didn't find how to do this...


How can android find a certain activity or service based on a given Intent?

Suppose that I installed app A and app B, app A has a main activity, and app B wants to show that activity by sending a intent. My question is that how android knows I have installed app A and is able to map the intent to its activity? Is there any windows-registry-like store saving such information? Thank You


Android - Make sure activity is alive before firing Intent?

I am currently firing an Intent to a Broadcast Receiver which in turns starts an Activity. Then from the same Service another Intent is fired to a Broadcast Receiver thats in the Activity. The problem is that the Activity isn't getting the Intent meant for it because it is fired before it is alive and the Broadcast Reciever is registered. I was wondering is there anyway to make sure an Activity is a...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top