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,707
  • AppCompatLib3_51.zip
    87.2 KB · Views: 2,473
  • AppCompatLib3_52.zip
    87.3 KB · Views: 4,399
  • AppCompatLib4_00.zip
    181.8 KB · Views: 4,569
Last edited by a moderator:

kkolle

Member
Licensed User
Longtime User
Hi after an update of the SDK to Support Lib 22.2 and Android 22

I could not start any AppCompat App.


I now uninstalled the SDK and reinstalled it.
My Sdk is now 22 and Supportlib 22.2.
What did i missing?

The steps i changed after reinstall:
- copy C:\Android\android-sdk-new\extras\android\support\v7\appcompat\libs\android-support-v4.jar to C:\Android\additional libs
- copy C:\Android\android-sdk-new\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar C:\Android\additional libs

- downloading AppCompat_v1_10.zip
- copy AppCompat.jar and AppCompat.xml to C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries
- copy b4a_appcompat to C:\Android\Projekte\Material\CustomRes\b4a_appcompat

- downloading ACSearchViewExample.zip for having a clear project
- copy it AppCompatToolbarSearchView to C:\Android\Projekte\Material\AppCompatToolbarSearchView

This is my IDE config:
- C:\Program Files (x86)\Java\jdk1.7.0_67\bin\javac.exe
- C:\Android\android-sdk-new\platforms\android-22\android.jar
- C:\Android\additional libs

Here is my changed code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: AppCompatSearchView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'#AdditionalRes: ..\resource
'#AdditionalRes: C:\Users\stm\Dropbox\Basic4Android\CustomLibsRes\b4a_appcompat, de.amberhome.objects.appcompat
'#AdditionalRes: C:\Android\android-sdk\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
'#Extends: android.support.v7.app.ActionBarActivity

#AdditionalRes: ..\resource
#AdditionalRes: C:\Android\Projekte\Material\CustomRes\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\Android\android-sdk-new\extras\android\support\v7\appcompat\res, android.support.v7.appcompat   
#Extends: android.support.v7.app.ActionBarActivity

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region


And my Stack:
B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.amberhome.appcompat.serachviewexample/de.amberhome.appcompat.serachviewexample.main}: java.lang.ClassCastException: android.support.v7.internal.widget.ContentFrameLayout cannot be cast to android.support.v7.internal.widget.NativeActionModeAwareLayout
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
   at android.app.ActivityThread.access$1000(ActivityThread.java:198)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6837)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)~e:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.ContentFrameLayout cannot be cast to android.support.v7.internal.widget.NativeActionModeAwareLayout
   at android.support.v7.app.ActionBarActivityDelegateHC.onSubDecorInstalled(ActionBarActivityDelegateHC.java:41)
   at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:362)
   at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:216)
   at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:107)
   at de.amberhome.appcompat.serachviewexample.main.onCreate(main.java:59)
   at android.app.Activity.performCreate(Activity.java:6500)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
   ... 10 more

 

kkolle

Member
Licensed User
Longtime User
After a long time searching i found the reason.

I have found old support jars in my custom lib folder.
After deleting the old files i was able to run my app.
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
I get this error when trying to run your SerarchView example

I'm able to run all the other examples, but for some reason i can't run this specific one, i have all resources folder, and also the b4a_appcompat folder as well, any idea why i get this error:

Never mind i have solved this issue, i had to update my android sdk to get the latest v7 and v4 jar files.

Thanks,
Walter
 

Attachments

  • searchview error.jpg
    searchview error.jpg
    43.1 KB · Views: 298
Last edited:

chefe82

Member
Licensed User
Longtime User
Hello There,

I get the following message recently always


Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. Error
C:\Android\SDK\extras\android\support\v7\appcompat\res\values-v23\styles_base.xml:20: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
C:\Android\SDK\extras\android\support\v7\appcompat\res\values-v23\styles_base_text.xml:19: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.

Support Libery is Version 23

who can help me because
 

corwin42

Expert
Licensed User
Longtime User
Make sure that everything is up to date if you update the support library with SDK Manager.

For the update to Support library 23 (from 22.x or older) do the following:

1. Update Support library with SDK Manager to v23
2. Install new Android 6.0 (API23) / SDK Platform (can be done together with first step)
3. Copy v4 and v7 support libraries from <...>\android-sdk\extras\android\support\v7\appcompat\libs to your CustomLibs folder
4. In the B4A IDE select Tools/Configure Paths and setup the path for API23 android.jar (<...>\android-sdk\platforms\android-23\android.jar)

After that everything should compile fine.

Note: Compiling against the newest (API23) android.jar does NOT mean that your app will run only against this version. It will run on oder versions, too. The support libraries just depend on the newest android.jar at compile time.
 

pauloterasystem

Member
Licensed User
Longtime User
It's still not working, "AppCompatSearchView hass stopped"

paths config
1- C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe
2 - C:\Androide\platforms\android-21\android.jar
3 - C:\b4x\lib

#AdditionalRes: ..\resource
#AdditionalRes: C:\b4x\lib\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\Androide\extras\android\support\v7\appcompat\res, android.support.v7.appcompat

I've copied the android-support-v4 and android-support-v7-appcompat to C:\b4x\lib
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Please do all the things listed in the troubleshooting guide of the first post.

For a stack trace of the error you may have to connect your device via USB (or use an emulator). The error should show in the unfiltered logs.
 

pauloterasystem

Member
Licensed User
Longtime User
Please do all the things listed in the troubleshooting guide of the first post.

For a stack trace of the error you may have to connect your device via USB (or use an emulator). The error should show in the unfiltered logs.

I've done all the things listed in the troubleshooting and still the same error.
Using an emulator : "Unfortunately AppCompatSerearchView has stopped." and there's no stack trace
 

corwin42

Expert
Licensed User
Longtime User
There should be a stack trace in unfiltered logs.

Do you use the latest support library and platform version (android-23). From your above path config you are not.
 

Walter Adriano da Silva

Member
Licensed User
Longtime User
Hello,

I'm having any problems with AppCompat.

The error started after I upgraded the Android Support Library to version 23, consequently I'm using the API 23 too.

I got a "Unfortunately has stopped" dialog when the app is started. In debug mode, the app crash before of show me the exception message.

I got it the exception message using "adb logcat" by prompt:
java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }

The app already used AppCompat and worked perfectly.

Can anybody help me?
 

corwin42

Expert
Licensed User
Longtime User

pauloterasystem

Member
Licensed User
Longtime User
In the Log-Windows check the "Show unfiltered logs" checkmark.


java.lang.NoClassDefFoundError: android.support.v7.app.AppCompatDelegateImplV14
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:135)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:456)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at de.amberhome.appcompat.serachviewexample.main.onCreate(main.java:33)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 

pauloterasystem

Member
Licensed User
Longtime User
Your support libraries are not up to date.

1-I uninstalled the Android-SDK, java sdk and reinstalled it.
2 - Download the exemple / AppCompat 1.01
3 - Copy android-support-v4.jar and android-support-v7-appcompat.jar to support lib

And I got this error

B4A version: 5.02 (1)
Parsing code. (0.02s)
Compiling code. (0.09s)
Compiling layouts code. (0.01s)
Generating R file. Error
C:\Androide\extras\android\support\v7\appcompat\res\values-v23\styles_base.xml:20: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
C:\Androide\extras\android\support\v7\appcompat\res\values-v23\styles_base_text.xml:19: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.
 

corwin42

Expert
Licensed User
Longtime User
Now you have configured an old android.jar in B4A IDE (Configure Paths dialog).

Be sure that the support library version fits the platform version (android.jar). For the latest support library you have to use android.jar from platform version 23.
This is all in the troubleshooting section of the first post.
 
Top