How to access an Activites Menu programatically?

by Carl Whalley » Wed, 19 Aug 2009 03:31:12 GMT

Sponsored Links
 I'm writing unit tests in a class derived from
ActivityInstrumentationTestCase2. I know I can access anything defined
in the applications XML using findViewById(), but this application
defines its Menu programmatically. I'm trying to access that menu so I
can fire click() events to it in my tests the same way I'm doing with
Button Views etc.

To illustrate, this works fine and shows I can control/test various

public void testMyAppEdit() {
        MyAppActivity main = (MyAppActivity) getActivity();

        EditText val1Edit = (EditText) main.findViewById(;

        Button addButton = (Button) main.findViewById(;

        TextView resultText = (TextView) main.findViewById(;

        assertEquals("Result incorrect", resultText.getText(), "3");

But I need to do the same to fire Menu events:

public void testMyAppAboutBox() {
        MyAppActivity main = (MyAppActivity) getActivity();

        // Huh? this menu has no id, and no obvious way of accessing it...
        //Menu menu = (Menu) main.findViewById(id);


Android Academy 

How to access an Activites Menu programatically?

by Carl Whalley » Wed, 19 Aug 2009 18:11:04 GMT

 hanks - just establishing theres no framework way to get menus.
So I did as you suggested and there still some wierdness. I added a
getter for the menu but it always returns null:

public class MyAppActivityTest extends
ActivityInstrumentationTestCase2< MyAppActivity> {

public void testMyAppAboutBox() {
MyAppActivity main = (MyAppActivity) getActivity();
Menu menu = main.getMenu();

Could it be the Activities onCreateOptionsMenu() hasn't been called?
Is there more I should be doing in my test to help establish the
Activities runtime context?

On a different thread (:-)), I tried this:

public void testAbout() {
MainActivity main = (MainActivity) getActivity();

Menu menu = main.getMenu();
menu.performIdentifierAction(, 0);

Hoping the key event would cause the menu to be inflated, but that
just causes:

java.lang.RuntimeException: This method can not be called from the
main application thread


On Aug 18, 8:59pm, Dianne Hackborn <> wrote:

Sponsored Links

Other Threads

1. Resizing an ImageView within a TableLayout programmatically

If I have a TableLayout structure such as below which defines an
ImageView width/height, then I see the expected result:

<TableLayout android:layout_width="wrap_content"
                <ImageView android:src="@drawable/pic1" 
android:layout_height="50px" />
                <ImageView android:src="@drawable/pic2"
android:layout_height="wrap_content" />

I need to do the above programmatically so...

TableLayout tableLayout = new TableLayout(ctx);
TableRow tableRow = new TaleRow(ctx);

imageViewOne = new ImageView(ctx);
imageViewTwo = new ImageView(ctx);
imageViewOne .setImageResource(R.drawable.pic1);

imageViewOne.setLayoutParams(new LayoutParams(50, 50));


... The above shows pic2, but pic1 dissapears completely.

What am I missing here?

Eurig Jones


2. Emulate keypress from ImageButton in non application code

Porting the android onto the tablet got the need for  navigation
buttons in the status bar. I came to the decision that it is better to
do it through ImageButton. I edit and added
buttons. However, there was a problem - how to emulate keypress event?


3. mysql connection from android using soap

4. Downloading pictures from camera to Android device

5. SeekBar and setThumb

6. "Socket Not Connected" Exception

7. onDraw method of CustomView do not redraw Canvas.