testing menu_main.xml (Android) - android

I'm attempting to write unit tests for a very simple app. Here is the menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_refresh" android:title="#string/action_refresh"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
App runs fine, both items show up. MainActivity.xml can clearly find them because
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
works just fine.
I have unit tests that descend from ActivityInstrumentationTestCase2, one of which checks the menu_main.xml.
public void testSettingsMenuItemExists() {
Object view = activity.findViewById(R.id.action_settings);
assertNotNull("Settings menu item does not exist", view);
}
R.id.action_settings obviously exists because the code compiles, but when the tests run, they do not find item. They do find elements from activity_main.xml. I've tried getting the action bar via activity.getActionBar(), it is null. I thought maybe the action bar was owned by the application, not the activity, but Applications don't even have getActionBar().
How do I find menu items so I can test them in unit tests?

I think you have to declare activity class with theme in Manifest file which have action bar default like..
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light"/>
then you can get the action bar and it will not null.

Related

Icons not showing up in Action bar

I am trying to get an action bar within my android app to show up with icons. No matter what I do, I only see the overflow icon with a dropdown, while I want to see the Home and Logout icons next to each other on the action bar menu. I have read through most posts on SO for a solution but am unable to find a solution. Can anyone please help ?
Below is my code :
AndroidManifest.xml
<application
android:name=".application.MySampleApplication"
android:icon="#drawable/letter"
android:label="My Sample"
android:theme="#android:style/Theme.WithActionBar">
Menu_Main.xml
<?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_logout"
android:icon="#drawable/logout"
android:title="#string/logout"
android:orderInCategory="0"
app:showAsAction="always" />
<item
android:id="#+id/action_home"
android:title="#string/gohome"
android:icon="#drawable/ulogo"
android:orderInCategory="0"
app:showAsAction="always" />
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
logout();
return true;
}
if (id == R.id.action_home) {
goHome();
return true;
}
return super.onOptionsItemSelected(item);
}
How it shows up now
How I want it to show up
MainActivity extends Activity
This means that you are using the native action bar, not the backport offered by appcompat-v7. In that case, replace the app: prefixes in your menu resource with android:, and you will have better luck.
Was googling for a solution and saw someone use it
That is surprising. Your question is the first time I have ever seen anyone use that theme. I didn't even know it existed until I looked it up as part of checking your question. Most likely, you will want to use more modern themes.

Explicitly starting activity from menu item not working

using android 2.3.3 and Android API 25.
having 3 activities activity_main , activity_details and activity_settings.
I've used android studio auto generating for creating these activities as blank activity with fragment, So manifest code is generated automatically.
activity_main and activity_details have menu option Settings that's for opening the activity_settings.
clicking on Settings option menu for activity_details works and opens activity_settings but not working for activity_main
and here is my code:
MainActivityFragment.java and DetailActivityFragment.java (same)
`#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
}`
menu_detail.xml
`<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.poula.sunshine.DetailActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never" />
</menu>`
menu_main.xml
`<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.poula.sunshine.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never" />
</menu>`

MenuBar sometimes shows icon and sometimes not

I am trying to put an actionBar with two icons (one going to the main activity, and the other one going the previous one)
The thing is that I have this as XML being used as menu:
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always|withText"
android:orderInCategory="1"
android:icon="#drawable/ic_home"/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always|withText"
android:orderInCategory="2"
android:icon="#drawable/ic_back"/>
And the code for using it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Main_App) {
Intent getNewMessageAct = new Intent(this,UpLinksActivity.class);
startActivity(getNewMessageAct);
finish();
return true;
} else if (id == R.id.back_App){
Intent getNewMessageAct = new Intent(this,NewMessageActivity.class);
startActivity(getNewMessageAct);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I always use the same menu, and sometimes icons fits on the ActionBar and sometimes not. I don't get why it is happening.
It's nothing to do about the size because it has space for fitting.
Any idea of why it is happening will be appreciated.
Actually try displaying only icon without text..because ActionBar allocates some fixed space in it for App Title even though it is small..so in small phones it cant display two icons....and even change order in Category to set the same to both the items..
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always"
android:orderInCategory="100"
android:icon="#drawable/ic_home"/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always"
android:orderInCategory="100"
android:icon="#drawable/ic_back"/>
For other ones with the same problem I found a solution:
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always|withText"
android:showAsAction="always|withText"
android:orderInCategory="1"
android:icon="#drawable/ic_home"
/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always|withText"
android:showAsAction="always|withText"
android:orderInCategory="2"
android:icon="#drawable/ic_back"/>
I added "android:showAsAction="always|withText" and it worked. Not really sure why is it working, something related with support libraries I guess. Anyway it gives an XML error but it compiles and works.
the chosen answer didn't work for me.
In my case when I enter in the activity for the first time the menu items don't show in any fragment. If I rotate the device everything works fine.
[I do not have reputation to add a comment!]

ActionBarSherlock top-left icon doesn't navigate up

I am using ActionBarSherlock to implement ActionBar for both Pre/post versions of Android HoneyComb.
My problem is that when I tap top-left icon on Android version 4.0.4, it did not respond.
Here is what I have done till now:
1) In all style folders "values/styles.xml" , "values-v11/styles.xml" & "values-v14/styles.xml"; I have done the following
<style name="ActivityTheme" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActivityActionBarStyle</item>
<item name="android:actionBarStyle">#style/ActivityActionBarStyle</item>
</style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
<item name="displayOptions">showHome|showTitle|homeAsUp</item>
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>
In any application activity (except Home activity since it should not have Up arrow), I have done the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
.....rest of my code ...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
/*app icon in action bar clicked; go home*/
intent = new Intent(this, MainActivity.class);
/* With this flag, if the activity you're starting already exists in the current task,
* then all activities on top of it are destroyed and it is brought to the front.
* you should usually not create a new instance of the home activity. Otherwise,
* you might end up with a long stack of activities in the current task with multiple
* instances of the home activity.*/
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
In the Manifest file, I make sure that I have applied the respective style to all activities (except main activity since it should not have Up arrow)
<activity
android:name="com.andrid.example.TestActivity"
android:label="#string/app_name"
android:theme="#style/ActivityTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.andrid.example.MainActivity" />
</activity>
So now when I test the application on Pre-HoneyComb version, the Up arrow is never shown which is correct since ABS can't respond at all if icon was tapped to navigate up.
But when I tried the application on Post-HoneyComb version like 4.1 on Emulator, the Up arrow is shown and when I tapped it, it works as expected and navigates up normally.
My problem is that when I tried the application on Android 4.0.4 emulator , the Up arrow is shown as expected but when I tapped it, it do nothing
I have found the fix:
Simply I should avoid set the Icon Home as Up using xml attribute: homeAsUp but instead I should have used setDisplayHomeAsUpEnabled(true) explicitly as follows:
In all style folders "values/styles.xml" , "values-v11/styles.xml" & "values-v14/styles.xml"; I have done the following
<style name="ActivityTheme" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActivityActionBarStyle</item>
<item name="android:actionBarStyle">#style/ActivityActionBarStyle</item>
</style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
<item name="displayOptions">showHome|showTitle</item>
<item name="android:displayOptions">showHome|showTitle</item>
</style>
in Each Activity (except main activity):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Beginning android: Working with the main pop-up menu

Apologies for what I am sure is a common question, but after Googling for a while it seems I don't know the proper Android term for what I want and so am stuck.
I would like to know how to work with the main pop-up menu associated with a view...that is, if you are sitting at some activity doing nothing and press the menu key on the phone, how to work with that menu that commonly opens on many apps containing "Settings", "Exit", etc.
I am not sure what this is called, but if someone can point me to the appropriate portion of the SDK, that would be appreciated. Also, if someone knows how to work this menu within the context of the Eclipse ADT plugin, that'd be great, too.
Cheers.
As CaseyB says, it's just called a menu.
For a quick start on using it, you could create a subfolder in the res folder of an eclipse project and call it "menu" add in some xml for the view, and the call a MenuInflator from the onCreateOptionsMenu function.
Ok, that might be a bit confusing, so i've included some sample code that should get you started. This code should make it so when you press the menu button you can select one of two new activities to get loaded.
In Main Activity add:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// Define whatever other activities you can to load in here or whatever.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, Info.class));
break;
case R.id.logs:
startActivity(new Intent(this, Logs.class));
break;
}
return true;
}
menu.xml file: Link to whatever icon images you want.
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<!-- Copyright © 2012 Tutela Technologies Ltd. -->
<!-- All Rights Reserved. -->
<!-- -->
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/info"
android:icon="#drawable/ic_menu_info_details"
android:title="Info"></item>
<item
android:id="#+id/logs"
android:icon="#drawable/ic_menu_report_image"
android:title="Logs"></item>
</menu>
Then in your AndroidManifest.xml make sure you remember to add in the new activities.
<activity
android:name="com.whatever.Gui.Info"
android:label="#string/appTitle">
</activity>
<activity
android:name="com.whatever.Gui.Logs"
android:label="#string/appTitle">
</activity>
Note: in this example the labels are defined in strings.xml
<string name="appTitle">Your App name</string>
Hope this helps you!
Cheers
It's just called the menu. Here is a tutorial to get you started. Things get a little weird in 3.0+ but once you get the basics down it shouldn't be too hard to pick up.
On versions prior to 3.0, these are menus. Menus are being replaced by the ActionBar in the more recent versions of Android.
About menus: http://developer.android.com/guide/topics/ui/menus.html
Tutorial about menus: http://www.androidhive.info/2011/09/how-to-create-android-menus/
Look at the example:
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SettingsMenuItem:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In xml: (in /res/menu/menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/SettingsMenuItem"
android:icon="#drawable/settings"
android:title="#string/settings"/>
</menu>

Categories

Resources