B4A Library AppCompat - Make Material Design apps compatible with older Android versions

This library is a wrapper for the Google AppCompat v7 support library.

B4A V6.0 or newer is required!
From V3.52 on B4A V6.80 or newer is required! If you still use B4A < V6.80, use V3.51 of this library.

The main reason for this library is to make your apps compatible with older Android versions and to use features in old Android versions which were implemented in newer Android releases. One big advantage of this library is that you can bring Material Design to pre Lollipop Android devices.

Setting up the library
Note: Please, Please read these instructions carefully. AppCompat depends on many things like a special theme with special features. Even special versions of build tools are required and last but not least there are often bugs in the Google support libraries.

I created this instructions to help you getting things ready for using AppCompat. So please follow these instructions carefully and all should work as expected and you don't have to ask in the forum.

Thanks.

The library uses the Android Support Repository. Please install it from the Extras Section of the Android SDK Manager. If you upgrade from an earlier release you can deinstall the "Android Support Library" since it is outdated and marked as obsolete in the Android SDK.
  1. First copy the AppCompat.jar, AppCompat.aar and AppCompat.xml files to your additional libraries folder.
  2. Start the Android SDK-Manager and download/update the latest Extras->Android Support Repository. Use at least support repository 36!
  3. In Android SDK-Manager be sure that the "Android SDK Build-tools" 25.x.x is installed! Deinstall all versions prior to version 25! If you leave older versions than 23.0.3 of build tools installed, your compiled app may work on Android 5.0 and above but it will fail on older Android versions like KitKat.
  4. In the B4A IDE in your projects check the AppCompat library on the libraries tab.
  5. Make sure that you reference the latest android.jar in the IDE under the "Tools/Configure Paths" menu. Don't worry. The android.jar is only used for compilation. It does not mean that you can't run your app on lower Android Versions. The Support libraries require to compile against the same API version as the library itself. So for Support library 25.xx which is the current now you should compile with API25 android.jar.
  6. If you are updating from a version older than v3.20 then remove the b4a_appcompat resource folder delivered with older versions of this library.
To use the AppCompat library your activities MUST extend android.support.v7.app.AppCompatActivity (extending the old android.support.v7.app.ActionBarActivity still works.)

B4X:
 #Extends: android.support.v7.app.AppCompatActivity

Additionally you MUST use one of the AppCompat themes like Theme.AppCompat. You can set it directly in the manifest editor or create your own theme that has an AppCompat theme as its parent. The easiest way is to set the AppCompat theme in the manifest:

B4X:
SetApplicationAttribute(android:theme, "@style/Theme.AppCompat")

If you have upgraded this library from an older version than 3.20 then you have to remove the #AdditionalRes: attribute related to the AppCompat library from your projects. They are not needed anymore.

Some Tutorials for this library
Material Design 2 - Using the AppCompat library
Material Design 3 - Using a ToolBar as ActionBar
Material Design 4 - Modifyable and advanced Menu

Troubleshooting Guide
Because it is not so easy to setup this library I will try to give a troubleshooting guide here for the most common problems.

1. Compile always with the newest android.jar file. This can be configured in the IDE under "Tools/Configure Paths". Download the latest platform version with the SDK Manager and configure it with the IDE. Don't worry, your app will still run on older platform versions. AppCompat support library needs this only at compile time.

2. Be sure you have a correct theme file for your project. If the app seems to crash immediately without a stack trace it is most probably a problem with the theme file.

3. If you post your issue in the forum please provide the stack trace of the error! And before you post, please do a forum seach. Most probably the same error was previously reported by another user. If you don't see a stack trace in the log window, please try the unfiltered logs. Unfortunately most errors only show when you have your device connected via USB in the unfiltered logs.

4. If you post your issue in the forum please provide as many information as possible. The error you receive, screenshots if possible and most important: the stack trace.

Your support:
Creating libraries and wrappers for existing library projects is a lot of work. The use of this library is totally free and you even don't need to mention in your app that you use it.
But if you use this library in your projects and you think it is useful to you please consider to make a donation:

Thanks very much for your support.

Version History

V1.00
  • Object AppCompat - Utilities which are useful for working with AppCompat
  • Object ACActionBar - Like StdActionBar or StdActionBarHelper to control some features of the ActionBar.
  • Object ACEditText - Replacement for EditText with Material Design colors.
  • Object ACCheckBox - Replacement for CheckBox with Material Design colors.
  • Object ACRadioButton - Replacement for RadioButton with Material Design colors.
  • Object ACSwitch - Wrapper for a Switch.
  • Object ACFlatButton - Borderless Button for Dialogs etc.
V1.10
  • New object ACToolBarLight - Toolbar for light themes
  • New object ACToolBarDark - Toolbar for dark themes
  • New object ACActionMenu - Create action menus (action items and/or overflow menu everywhere in your layout. (The view needs to have a minimum width of 56dip to work properly. This seems to be a bug and hopefully will change in later versions of the AppCompat library)
  • New object ACActionModeWrapper - Change ActionBar/Toolbar to an ActionMode ActionBar. Mostly used for multiselect lists.
  • New object ACMenu - Access and modify the ActionMode, ActionMenu or Toolbar menus.
  • New object ACMenuItem - Access and modify the ActionMode, ActionMenu or Toolbar menu items.
  • New object ACPopupMenu - Implements a popup menu.
  • New object ACSearchView - Add a search view to your ActionBar/Toolbar
  • Add: AppCompat object has new methods for getting height of system components like StatusBar, ActionBar and NavigationBar.
  • Add: AppCompat object has a new method for setting the elevation of every view. With this all views (like Panels) can cast shadows. Unfortunately the shadow is only visible on Android 5.0 or above.
V2.00
  • #Extends: android.support.v7.app.AppCompatActivity should now be used (ActionBarActivity still works)
  • New object ACSpinner - AppCompat Spinner with additional support to show an icon in front of the text.
  • New object ACSubMenu - Adding Submenus to menus.
  • New Event ACSearchView_QueryChanged
  • Some internal changes (xml files for Views are not needed anymore)
  • Support for tinting Drawables.
  • Possibility to set a click effect to any view - This is experimental. It should add a ripple effect in Lollipop and above and a color change in previous versions.
  • To update from V1.xx to V2.00 delete the b4a_appcompat resource folder and copy the new one from the archive. The old xml files for buttons, edittext etc. are not needed anymore. Now there is only one xml for the spinner needed.
V3.00
  • Requires B4A 6.0 or above
  • References AppCompat from Maven repository
  • Full designer custom properties support for all views
  • Fix: ACRadioButton now supports grouping in a panel
  • Desupport of the Activity Entry animation.
  • New: ACSeekbar
  • Fix: ACSearchview closed() event does not fire (AppCompat bug). Added workaround for this.
  • Fix: Fixed several minor and major bugs
  • New example App which shows main features
V3.01

  • Fix: additionally depends on com.android.support:support-v4 which should make it work better if you mix old and new libraries.
  • Fix: ACSearchview_Closed event will not crash on API < 12 but it will not fire on these devices. Sorry, there is no workaround for this.
V3.20
  • Moved resource files to aar library file - see new setup instructions
  • Fix: ACButton ButtonColor attribute now works on all Android versions.
V3.30
  • Fix: Remove log output from ACSpinner
  • Fix: Hopefully fixed crashes with missing designer properties
  • Fix: Mixed case Eventnames in Toolbars now working
  • New: Support for ActionViews in MenuItems (Needed for CustomViews in NavigationView of DesignSupport library)
  • Change: Minimum Support Repository 36
V3.41:
  • Fix: Change packagename to anywheresoftware.b4a.orbjects to reduce resouce fields
V3.42:
  • Fix: ToolBars should raise their events correctly again.
V3.50:
  • New: Support for CharSequences wherever possible.
V3.51
  • Fix: Getters return String instead of CharSequence again to avoid problems.
V3.52
  • Fix: Compiled against B4A 6.80 Core library to avoid some problems with CharSequences.
V4.00
  • Change: Compiled against 28.0.0 Support library
  • New: Styles for most Views
 

Attachments

  • AppCompatBaseExample3_20.zip
    10.9 KB · Views: 4,702
  • AppCompatLib3_51.zip
    87.2 KB · Views: 2,469
  • AppCompatLib3_52.zip
    87.3 KB · Views: 4,396
  • AppCompatLib4_00.zip
    181.8 KB · Views: 4,564
Last edited by a moderator:

bryantm1122

Member
Licensed User
Corwin, thanks for all your work in putting together this series on Material Design. I'm following from Part 1 where I created a Material theme XML in values-v21. Am I correct in that after set up, I need to edit that file by replacing Material with AppCompat, as shown in the following pictures:

vu287GL.png

Dg0gHSQ.png
 

corwin42

Expert
Licensed User
Longtime User

acorrias

Member
Licensed User
Longtime User
Hi
I cannot run the app AppCompatSearchView. I crashes both on ADV and my phone.
I'm using 64 bit javac.exe and android-23 as android jar.

C:\java\Android\android-sdk\platforms\android-23\android.jar
C:\Program Files\Java\jdk1.8.0_74\bin\javac.exe

this is resource configuration
#AdditionalRes: ..\resource
#AdditionalRes: $AdditionalLibs$\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: $AndroidSDK$\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#Extends: android.support.v7.app.ActionBarActivity
#Extends: android.support.v7.app.AppCompatActivity


this my mainfest:

AddManifestText(
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="23"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

SetApplicationAttribute(android:theme, "@style/MyAppTheme")
'SetApplicationAttribute(android:theme, "@style/Theme.AppCompat")

'SetApplicationAttribute(android:theme, "@style/Theme.AppCompat.NoActionBar")

but I don't know how to manage style. I used this syntax
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#F57C00</item>
<item name="colorAccent">#FFA726</item>
</style>
<style name="MyAppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>

and since I ma using version 23 I created 3 folders
  • values
  • values-v21
  • values-v23
with the same theme.xml file.

this is my sdk setting:
upload_2016-2-27_14-58-0.png

and this is my customlibs content
upload_2016-2-27_14-58-51.png

Please, could you advice me about the right settings?
 

Attachments

  • upload_2016-2-27_14-57-39.png
    upload_2016-2-27_14-57-39.png
    230.5 KB · Views: 214

itgirl

Active Member
Licensed User
Longtime User
new update to appcompact AppCompat v23.2  using now vectors ... after update now it breaks alot of things even though i copied the jars to my additional lib

B4X:
main_activity_create (java line: 729)
android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_go_search_api_material.xml from drawable resource ID #0x7f020019
    at android.content.res.Resources.loadDrawable(Resources.java:2164)
    at android.content.res.Resources.getDrawable(Resources.java:717)
    at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
    at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
    at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
    at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
    at android.support.v7.widget.SearchView.<init>(SearchView.java:306)
    at android.support.v7.widget.SearchView.<init>(SearchView.java:274)
    at android.support.v7.widget.SearchView.<init>(SearchView.java:270)
    at de.amberhome.objects.appcompat.ACSearchViewWrapper.Initialize(ACSearchViewWrapper.java:33)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #16: invalid drawable tag vector
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:958)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:893)
    at android.content.res.Resources.loadDrawable(Resources.java:2160)
    ... 28 more

does not work on devices running KitKat or lower
 

DonManfred

Expert
Licensed User
Longtime User
android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_go_search_api_material.xml
Have you also updated the res folder location?

Can you post the #additionalres commands you use in your project?

Or maybe a small example which shows the problem
 

itgirl

Active Member
Licensed User
Longtime User
I have deleted the whole support library from sdk manager and i re downloaded it...
i use this additionalres in my apps

B4X:
    #AdditionalRes: ..\resource
    #AdditionalRes:  ..\b4a_appcompat, de.amberhome.objects.appcompat
    #AdditionalRes: c:\android-sdk\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
    #AdditionalRes: c:\android-sdk\extras\android\support\design\res, android.support.design
    #AdditionalRes: c:\android-sdk\extras\android\support\v7\recyclerview\res, android.support.v7.recyclerview
    #AdditionalRes: ..\md-app\res
    #AdditionalRes: ..\md-lib\res
    #Extends: android.support.v7.app.ActionBarActivity

and it works perfect on android +5 but kitkat and older does not work
 

DonManfred

Expert
Licensed User
Longtime User
android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_go_search_api_material.xml from drawable resource ID #0x7f020019
You need to figure out to which library the missing resource belongs..

Maybe it is appcompat, maybe it is the lib which uses
#AdditionalRes: ..\md-app\res
#AdditionalRes: ..\md-lib\res

Please note that you also can use shortcuts for the paths

$AdditionalLibs$ and $AndroidSDK$ are possible

B4X:
#AdditionalRes: ..\resource
    #AdditionalRes:  ..\b4a_appcompat, de.amberhome.objects.appcompat
    #AdditionalRes: $AndroidSDK$\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
    #AdditionalRes: $AndroidSDK$\extras\android\support\design\res, android.support.design
    #AdditionalRes: $AndroidSDK$\extras\android\support\v7\recyclerview\res, android.support.v7.recyclerview
    #AdditionalRes: ..\md-app\res
    #AdditionalRes: ..\md-lib\res
    #Extends: android.support.v7.app.ActionBarActivity

Sorry, all my devices already are updated to Android 5+
I do not have any devices with KitKat anymore
 

itgirl

Active Member
Licensed User
Longtime User
Thank you for the helpful infos ;) i think i know whats the problem now if you read more about the new version of support lib HERE you can find that section about the new vector support and how you can use aapt to exclude it


B4X:
To keep referencing attribute IDs on API level 20 or lower, add the following appt flag to your build,gradle file:
If you are building with Android Plugin for Gradle 1.5.0 or lower, add the following to your build.gradle file:
android {
  defaultConfig {
    // Stops the Gradle’s automatic rasterization of vectors
    generatedDensities = []
  }
   // Flag that tells aapt to keep the attribute ids
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

im not sure 100% but i think this is the problem because i've checked my res folder for this abc_ic_go_search_api_material.xml and it shows

B4X:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:tint="?attr/colorControlNormal">
    <path
        android:pathData="M10,6l-1.4,1.4 4.599999,4.6 -4.599999,4.6 1.4,1.4 6,-6z"
        android:fillColor="@android:color/white"/>
</vector>

so i think the problem is with the new vector support im not sure....
 

corwin42

Expert
Licensed User
Longtime User
new update to appcompact AppCompat v23.2  using now vectors ... after update now it breaks alot of things even though i copied the jars to my additional lib

The problem is that the aapt tool needs to be called with an additional option to make it work on KitKat and below. I just started a conversation with Erel to find a solution for the problem. Maybe Erel has to change the aapt call in B4A for this.

Just for info: support library 23.2 has some serious bugs regarding to vector drawables on older devices. So until 23.2.1 is available you shouldn't upgrade it. Hopefully until then Erel may have found a solution for the aapt problem.
 

itgirl

Active Member
Licensed User
Longtime User
The problem is that the aapt tool needs to be called with an additional option to make it work on KitKat and below. I just started a conversation with Erel to find a solution for the problem. Maybe Erel has to change the aapt call in B4A for this.

Just for info: support library 23.2 has some serious bugs regarding to vector drawables on older devices. So until 23.2.1 is available you shouldn't upgrade it. Hopefully until then Erel may have found a solution for the aapt problem.
Yes you are 100% right about the bugs, i've already downgraded to 23.1 , hopefully you and Erel can find a solution for this
 

shashkiranr

Active Member
Licensed User
Longtime User
The problem is that the aapt tool needs to be called with an additional option to make it work on KitKat and below. I just started a conversation with Erel to find a solution for the problem. Maybe Erel has to change the aapt call in B4A for this.

Just for info: support library 23.2 has some serious bugs regarding to vector drawables on older devices. So until 23.2.1 is available you shouldn't upgrade it. Hopefully until then Erel may have found a solution for the aapt problem.

Waiting for further info. Facing the same issue. kitkat and lower phones and tablets, the app crashes.
 

konradwalsh

Active Member
Licensed User
Longtime User
Hey
Could do with some help.. I searched for answers to this one

I see errors listed before my post but I cant tell if they are related. I say that because it was working fine on Friday.

I was using this fine until today..
but now I get this error:
B4X:
java.lang.NoSuchMethodError: No static method hasOnClickListeners(Landroid/view/View;)Z in class Landroid/support/v4/view/ViewCompat; or its super classes (declaration of 'android.support.v4.view.ViewCompat' appears in /data/app/de.amberhome.appcompat.serachviewexample-1/base.apk)

I get in my app and this sample app..

So I downloaded fresh copies of the library and reinstalled/copied..

still the same


If I run in DEBUG mode, it crashes on launch
If I run in RELEASE mode, it crashes on menu click
 
Last edited:

acorrias

Member
Licensed User
Longtime User
Hi all
I cannot manage using 23.2 support with my code posted in #124 post.
I Try to lauch the app but it crashes. I followed all steps and advices for post #1 without success.
I am a biut confused an I need some help to exec the app

please could you support mein
a) how must be named the directory for the theme file using 23.2 support library? Values, Values-v23 or other name?
b) is my theme correct?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#F57C00</item>
<item name="colorAccent">#FFA726</item>
</style>
<style name="MyAppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
c) provided that my style is the one above is correct my manifest section:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

Thanks in advance for the answers
alex
 

itgirl

Active Member
Licensed User
Longtime User
Hi all
I cannot manage using 23.2 support with my code posted in #124 post.
I Try to lauch the app but it crashes. I followed all steps and advices for post #1 without success.
I am a biut confused an I need some help to exec the app

please could you support mein
a) how must be named the directory for the theme file using 23.2 support library? Values, Values-v23 or other name?
b) is my theme correct?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#F57C00</item>
<item name="colorAccent">#FFA726</item>
</style>
<style name="MyAppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
c) provided that my style is the one above is correct my manifest section:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

Thanks in advance for the answers
alex
Please read the post 131
 

corwin42

Expert
Licensed User
Longtime User
Yes you are 100% right about the bugs, i've already downgraded to 23.1 , hopefully you and Erel can find a solution for this
23.2.1 of support library is available. I currently can't test it but looking on the changelog it should fix all problems.
 

itgirl

Active Member
Licensed User
Longtime User
23.2.1 of support library is available. I currently can't test it but looking on the changelog it should fix all problems.
Thanks for the info i will test the new update and i will get back with the results
 

xor83

Member
Licensed User
Longtime User
RemoveMenuItem of ACPopupMenu give me error:

B4X:
Dim ACM As ACPopupMenu
ACM.Initialize("my_menu", ImageOverflow)
ACM.AddMenuItem(1,"Test",Null)
ACM.AddMenuItem(2,"Test2",Null)
ACM.AddMenuItem(3,"Test3",Null)
ACM.RemoveMenuItem(2)  ' << Error here

Any help?
 
Top