Missing SherlockActionBar dropdownmenu on phone, works on emulator - android

I added the SherlockActionBar to my project, added a few menu items and it works fine in the emulator (4.0.3), but it does not work on my phone (Galaxy S3); it just doesn't show up.
I'm targeting 15 with a minimum sdk version of 9 in my manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/dimmiicon"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar" >
Code is rather straightforward, but perhaps i'm missing something. Relevant bits...
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
public class Launcher extends SherlockFragmentActivity implements OnUserAuthenticatedListener, OnDialogChatterListener, OnAnimationCompleteListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
final ActionBar ab = getSupportActionBar();
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.title_bar);
ab.setStackedBackgroundDrawable(getResources().getDrawable(R.drawable.titlebarbackground));
TextView txt = (TextView) findViewById(R.id.myTitle1);
Typeface font = Typeface.createFromAsset(getAssets(), "caviar_dreams_bold.ttf");
txt.setTypeface(font);
TextView txt2 = (TextView) findViewById(R.id.myTitle2);
Typeface font2 = Typeface.createFromAsset(getAssets(), "caviardreams.ttf");
txt2.setTypeface(font2);
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.commonmenus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return false;
case R.id.search_for_product:
performProductReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_business:
performCompanyReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_service:
performServiceReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_person:
performPersonReview(findViewById(android.R.id.content));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the commonmenus.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item android:id="#+id/search_for_product" android:showAsAction="never" android:orderInCategory="10" android:title="#string/menu_search_for_product"></item>
<item android:id="#+id/search_for_business" android:showAsAction="never" android:orderInCategory="11" android:title="#string/menu_search_for_business"></item>
<item android:id="#+id/search_for_service" android:showAsAction="never" android:orderInCategory="12" android:title="#string/menu_review_service"></item>
<item android:id="#+id/search_for_person" android:showAsAction="never" android:orderInCategory="14" android:title="#string/menu_review_person"></item>
<item android:id="#+id/view_my_reviews" android:showAsAction="never" android:orderInCategory="15" android:title="#string/menu_view_my_reviews"></item>
</menu>
I'm using the sherlock actionbar to customize the title using some custom font's and that is working on the emulator AND the phone, but the menu only shows up on the emulator. I did try it with the custom font code commented out and it made no difference.
Just so I'm clear: the 3 menu dots don't even show up.
After spending a couple of hours searching and looking for a clue, I'm at a loss. Any insight would be appreciated.

For devices with Android Honeycomb+ ActionBarSherlock will use stock ActionBar features to work with, which means you're left with official ActionBar, which only puts the legacy menu button in very specific scenarios, see here:
http://android-developers.blogspot.co.il/2012/01/say-goodbye-to-menu-button.html
Under "Action overflow button for legacy apps":
If your app runs on a device without a dedicated Menu button, the
system decides whether to add the action overflow to the navigation
bar based on which API levels you declare to support in the
manifest element. The logic boils down to:
If you set either minSdkVersion or targetSdkVersion to 11 or higher,
the system will not add the legacy overflow button.
Otherwise, the system will add the legacy overflow button when running
on Android 3.0 or higher.
The only exception is that if you set minSdkVersion to 10 or lower,
set targetSdkVersion to 11, 12, or 13, and you do not use ActionBar,
the system will add the legacy overflow button when running your app
on a handset with Android 4.0 or higher.

Related

How do I get my ActionBar Items to show as icons, and not in the overflow menu?

First off, I'm running the latest Lollipop - Minimum and targeted SDK are both 22
minSdkVersion 22
targetSdkVersion 22
in the gradle build. I've changed/re changed these to 22, as well as rebuilt/invalidated caches/cleaned. I'm extending Activity in my main:
public class login_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
And here are my menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="ifRoom"/>
</menu>
When I run my code, it keeps showing the items in the ActionBar overflow menu. How do I fix this? Thanks!
Try replacing "ifroom" with "always" for showAsAction like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="always"/>
</menu>
I actually spent nearly five hours trying to figure this out, but I actually figured it out just now going through my gradle files. Here's how you fix it:
Go to your Gradle Scripts folder
Open the build.gradle file with the SDK options in it
Remove the line under dependencies (Mine's this one, not sure how they vary):
compile 'com.android.support:appcompat-v7:22.2.0' (REMOVE THIS)
Go back to your menu file and REMOVE the namespace definition for the namespace you put before showAsAction
xmlns:app="http://schemas.android.com/apk/res-auto" (REMOVE THIS)
Go change the showAsAction line from something:showAsAction to android:showAsAction
Rebuild & compile. It should work! I wish changing the SDK cleaned up after itself, but hey, what can you do.

Android Custom ActionBar with Search View

I am using custom ActionBar library in my Android App. there i want to show a searchView but can not find any solution, i m working in library that is
https://github.com/johannilsson/android-actionbar
This is a class
private class ExampleAction extends AbstractAction {
public ExampleAction() {
super(R.drawable.ic_title_export_default);
}
#Override
public void performAction(View view) {
Toast.makeText(OtherActivity.this, "Example action",
Toast.LENGTH_SHORT).show();
}
}
//use for signle action how to use for multiactions
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
actionBar.addAction(new ExampleAction());
actually i found code for searchbar but it is in .cs format how to use this searchview
https://github.com/zleao/MonoDroid.ActionBar
Thanks bro & sis
Take a look at this answer.
According to this answer SearchView is available support library.
add dependency to build.gradle
compile 'com.android.support:appcompat-v7:22.0.0'
and make imports
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
According to this answer it worked on API 8 on emulator. For more details, check the link with the question and answer itself.
Hope it helps.
UPDATE
By adding this code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hmkcode="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
<item
android:id="#+id/action_copy"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_content_copy"
android:title="Copy"/>
<item
android:id="#+id/action_share"
android:orderInCategory="100"
hmkcode:showAsAction="always"
android:icon="#drawable/ic_social_share"
android:title="Share"/>
you can achieve this:
You can customize items in the way you want. For more details see this example.

Creating an Options Menu not working for me

Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and then inflating the menu in the activity as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.
My activity extends FragmentActivity, in case that matters.
The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.
I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.

Theme change doesn't work on <4.0 as it should

I have some difficulties with setting up a "theme switcher" programmatically.
I would like to switch themes from app (between White (Theme.Light.NoTitleBar) and Dark (Theme.Black.NoTitleBar)) and what I do is:
I set a SharedPreference:
final String PREFS_NAME = "MyPrefsFile";
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
and than I have a two buttons to switch themes (second one is almost identical)
Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
ThemeWhite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editor.putBoolean("Theme", false);
editor.commit();
System.exit(2);
}
});
and in begging of each activity I check SharedPreference
boolean theme = settings.getBoolean("Theme", false);
if(theme){
this.setTheme(R.style.Theme_NoBarBlack);
} else{
this.setTheme(R.style.Theme_NoBar);
}
setContentView(R.layout.aplikacja);
I define themes in file styles.xml in folder values:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.NoTitleBar" />
in values-v11:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.Holo.NoActionBar" />
in values-v14:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.DeviceDefault.NoActionBar" />
manifest file:
<application
...
android:theme="#style/Theme.NoBar" >
Everything is working excellent on android >4.0 but when I use 2.2 it doesn't change theme - just font is getting white as it should be but there is no dark background.
I tried checking if it at least works and changed Theme.NoBarBlack in values (for android <3.0) and its value the same as Theme.NoBar and then when I pressed button font wasn't changed -as it should do.
EDIT (PROBLEM FOUND):
So after trying it turn out that on Android 2.2 Manifest file set Background and rest, and programmatically changing theme affect only text color. any idea why that happens?
ANSWER (as I don't have 10 reputation):
On android <3.0 it matters if
super.onCreate(savedInstanceState);
Is before setting up theme or not.
So it should be:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
}
Sorry guys for making problem out of nothing but as it was working on >3.0 I was confused. Spend half of day on it but working. :)
There's a mistake in your styles.xml:
for Theme.NoBarBlack, you set the parent to #android:style/Theme.NoActionBar, but it doesn't exist.
I think you mean #android:style:Theme.NoTitleBar.

Android:Option menu appearance and icon not getting displayed

I am facing a few problems with the android option menu.
Here is my code:
Inside res folder, i created a menu folder containing menu.xml file with this below code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#drawable/green"
android:id="#+id/icon" />
<item
android:id="#+id/text"
android:title="Text"/>
<item
android:id="#+id/icon1"
android:title="Icon and Text"
android:icon="#drawable/icon"/>
</menu>
And this is my SimpleOptionMenuActivity.java file:
public class SimpleOptionMenuActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon:
Toast.makeText(this, "Icon menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.text:
Toast.makeText(this, "Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.icon1:
Toast.makeText(this, "Icon and Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
1) On clicking the menu button of emulator, the menu is getting displayed like a ListView i.e. one above another, but i want the options to appear one beside another, 3 items per row, i.e. if there are four items then there will be two rows (which is usual, i don't know how to explain this properly).
2) The image is not getting displayed. I checked the R.java file, the icon is present inside the drawable inner class but still it's not getting displayed in the menu.
Please help me to solve the two problems.
Change the API version from 15 to 11 or lower . Also , the image is not getting displayed because you might have selected an image too large for a certain drawable folder .
drawable-hdpi with 72 by 72 pixel icons
drawable-mdpi with 48 by 48 pixel icons
and
drawable-ldpi with 36 by 36 pixel icons
Question 1:
in the AndroidManifest.xml, find something like the following:
"
uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"
"
remove the android:targetSdkVersion="16"
Question 2:
my menu.xml file like this:
<item android:id="#+id/about"
android:icon="#drawable/about"
android:title="#string/about"
android:showAsAction="ifRoom|withText" />
it works, show the image icon, you can try it.

Categories

Resources