ActionBar menuitems doesnt work using support library - android

my menu layout looks like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:blabla="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/msg_action_sections"
android:title="Sections view"
android:orderInCategory="100"
blabla:showAsAction="never" />
<item android:id="#+id/action_search"
android:title="Search"
blabla:showAsAction="collapseActionView"
blabla:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
but eclipse keeps giving me this error : "Should use android:showAsAction when not using the appcompat
library"
I don't know how to tell Eclipse that I'm using it!!! In android studio it worked, but I can't use Android studio. So what should I do?
Thank you
PS: Im using support.v7.app.ActionBar and my Activity extends from ActionBarActivity

Ok I figured it out, problem is, that I had bad theme using in AndroidManifest. You have to use Theme which uses Appcompat as parrent:)

Related

Android: Design view of XML file is white and blank

I created a menu folder under res folder and, in that menu folder, I created a file called drawer_menu.xml. This is the text of that file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkableBehavior="single">
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_profile"
android:title="Message" />
<item
android:id="#+id/nav_my_events"
android:icon="#drawable/ic_my_events"
android:title="Chat" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="Share" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="Send" />
</menu>
</item>
</menu>
But the design view is white and blank. No element shows up.Also I don't get to see AppThemes option which usually shows up in layout xml files.
I am using Android Studio Canary 3.2 and looks like that.
So maybe is a bug of your Android Studio version.
I believe there is a problem with the Android Studio Preview which didn't load properly and needs to be checked. If you're using the latest Android Studio, try reopening-refreshing preview by clicking on Preview (in the right side of the app).
If this didn't solve the issue, double clicking on the Preview in the right side of the app when using code editor should show the preview or errors but i think it will be fixed by doing that since I've had the same issue and after reopening it, there were a force refresh for the layout and it fixed the issue somehow.
Try changing support libraries version to more stable as 27.1.1
if you have 28 SDK version change to 28.0.0-alpha1
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1

getActionView() of my MenuItem return null in AppCompat

I had this menu xml that works fine:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/context_menu_save"
android:actionViewClass="my.app.TextViewPlus"
android:showAsAction="always"
android:title="#string/logout"
android:visible="false"/>
</menu>
But when i started using AppComap v7 i hade null exception when using getActionView().
I change my menu layout to:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/context_menu_save"
myapp:actionViewClass="my.app.TextViewPlus"
myapp:showAsAction="always"
android:title="#string/logout"
android:visible="false"/>
</menu>
And now it works fine. Can anyone explain, why is it happens?
xmlns:myapp this is used when you create your own (or use others) customized views.
xmlns:android this is used when default android views.
so as ur question, i hope ur using your own (or use others) customized views. so u got error.
In Android Studio v1.5, I see the following message for this scenario:
When using the appcompat library, menu resources should refer to the app: namespace, not the android: namespace.
When not using the appcompat library, you should be using the android: namespace.

menu icon not displaying on action bar

Android Studio 0.5.8
Hello,
For some reason the icon never displays on the ActionBar, I have used a combination of ifRoom|withText but still doesn't display. I have also tried rotating in Landscape. I am using genymotion 4.4.2
<?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:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
app:showAsAction="always"/>
</menu>
I am inflating the menu in a fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list, menu);
}
Here is a screenshot:
I have tried hardware nexus5 in portrait and landscape mode, but no icon.
I have also tried using the following, but didn't work either:
android:icon="#android:drawable/ic_menu_add"
Many thanks for any suggestions,
I have come across this issue once myself. Try this:
<?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:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
app:showAsAction="always"/>
</menu>
I don't know why it would be necessary to have both, but that fixed it for me for some reason.
You are using Android Studio, as explained here by blackfizz: "the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error."
I had the exact problem. Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow. If I ignored the error, I got the expected behavior, but the error still bothered me.
The real culprits turned out to be the following lines in the file build.gradle:
dependencies {
…
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
}
which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!
I wasted a few hours to figure it out myself before reading blackfizz's answer, so I am posting my answer here in hopes of saving other developers a few hours.
When you encounter a similar situation, first check your build.gradle to see if you have inadvertently imported the appcompat library.
You need to use Theme.Holo style and not AppCompat.
In order to do so, just change the style of the application at AndroidManifest.xml
If you get error:
should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto
Then you need to change the module settings:
1 - Right Click on your app and Select Open Module Settings (Or just Press F4)
2-In the depencencies, add a support module newer than V7 (for example com.android.support:support-v13:22.0.0)
in the menu.xml, dont write:
app:showAsAction="ifRoom"
but write
android:showAsAction="ifRoom"

SearchView in ActionBar using support-v7-appcompat

I've been trying very hard to get the SearchView widget to expand in the actionbar using the support-v7 libraries. I've managed to get it working without the support libraries when I target 4.0+ but I want to write the app for 2.3+ so I need to use the support libraries.
I created a blank new activity with the following menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
    
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
yourapp:showAsAction="always"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
This does not even show the search button, let alone expand it upon clicking. It simply add's the search into the menu instead of showing it in the actionbar.
Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
And it works perfectly fine, and even expands to the search text input widget upon clicking.
I want the searchview as available in the second picture while using the appcompat library, but for some reason it doesn't seem to be working. I'm using eclipse and I've included the Support libraries with resources exactly as specified in Support Library Setup[developer.android.com].
My manifest file has minsdk version as 7, targetsdk version as 18, and the build target is also 18.
I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!
Maybe SearchView wasn't showed because you missed to add a collapseActionView in this line: yourapp:showAsAction="always".
Also, your activity must extends AppCompatActivity. So, add AppCompat library to project
More details you can read on this link
Hope it will help you.

Android Actionbar compatibility alternate xml for pre-honeycomb

I am using the actiobarcompat sample in my application and I am trying to implement search for pre 3.0 devices.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="1"
android:title="#string/menu_search"
android:icon="#drawable/ic_home"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
On Honeycomb+ this works fine, the searchview widget appears in the actionbar. What I am trying to do is have a second menu XML so I can fall back to the old search activity way of doing it. However, there is no such thing as menu-v11 folder as the menu folder is essentially menu-v11 because that is the version it started supporting this.
My question is, using the actionbar compatibility sample, is there a way to declaratively add an alternate button for pre-honeycomb?
Can you please be more specific about what you are trying to achieve?
It it's about calling different activities depending on API version, the action bar has nothing to do with that.
You analyse API version in onOptionsItemSelected and act accordingly.
If you want different menu items depending on API version, just create a folder menu-v11 (or menu-v14) and put
version-specific xml-s- there.
BTW, I use com.android.actionbarcompat and it works great for me!

Categories

Resources