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
Views:

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

        EditText val1Edit = (EditText) main.findViewById(R.id.EditText1);
        val1Edit.setText("3");

        Button addButton = (Button) main.findViewById(R.id.ButtonAdd);
        addButton.performClick();

        TextView resultText = (TextView) main.findViewById(R.id.Result);

        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);
...
}

Thanks.

--
Android Academy  http://www.androidacademy.com 
--~--~---------~--~----~------------~-------~--~----~



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> {

@UiThreadTest
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:

@UiThreadTest
public void testAbout() {
MainActivity main = (MainActivity) getActivity();
sendKeys(KeyEvent.KEYCODE_MENU);

Menu menu = main.getMenu();
menu.performIdentifierAction(R.id.about, 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
at android.app.Instrumentation.validateNotAppThread
(Instrumentation.java:1500)
at android.app.Instrumentation.sendKeySync(Instrumentation.java:933)
...

Thanks

On Aug 18, 8:59pm, Dianne Hackborn <hack...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. splash screen and low memory

In my game a splash screen is shown which preloads all the sound/
bitmap resources to be used by the game. The game consists of other
activities that all depend on resources i.e menu activities, game
activity etc.

However when simulating low memory conditions in the emulator, I
noticed that our game process was being killed and when pressing the
back key the last activity that was on screen of the game is loaded,
however it crashes as the resources are not in memory.

Does that mean every activity in my game needs to check is the
resources are loaded and if not show up the splash screen activity and
load the resources before returning to the activity? Is there a nicer
way of doing this?

Can't I just make the system follow the normal pattern of starting my
game i.e splash screen and then display the menu and then the user can
choose to resume the game themselves. Is this possible?

-- 

2. single-line text horizontal scroller

is there a trick to getting a single line of text to scroll 
horizontally? the options available off <TextView> look promising, 
but the view won't take any touch input, preferring instead to give 
it to the parent view. and sticking it in a <ScrollView> didn't 
change the behaviour at all.

one wrinkle is that this bundle is inside a much bigger view that 
scrolls vertically. i did set "scrollsHorizontally" to false in the 
outer view, but i'm not sure whether i need to do more to isolate the 
two scrollers.

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:layout_marginRight="10px"
        android:layout_marginBottom="10px"
        android:scrollbars="horizontal"
        >
        <TextView
                android:id="@+id/tag_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:scrollHorizontally="true"
                android:padding="5px"
                android:textColor="#2f4f4f"
                android:textSize="24px"
                android:background="#e0ffff"
        />
</ScrollView>

thanks for any help here,
-- 
jason.vp.engineering.particle

-- 

3. Can I update my Motoblur firmware 1.5 to the 2.0?

4. Javadoc missing in Eclipse for 2.0, API 5

5. Strange exception that doesn't cause any harm apparently!

6. Join over 3,000 - The Android Developer Group on Linkedin

7. Join over 3,000 developers on Linkedin