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

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.


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,712
  • AppCompatLib3_51.zip
    87.2 KB · Views: 2,479
  • AppCompatLib3_52.zip
    87.3 KB · Views: 4,403
  • AppCompatLib4_00.zip
    181.8 KB · Views: 4,573
Last edited by a moderator:

corwin42

Expert
Licensed User
Longtime User
When you use ACSpinner and click on it, in the log appears line "hasIcon: false". It shows in th log multiple times on every click. Is there any reason for it or can it be removed from the next version of the library?
Yes, this is debug code which was accidentally left in the release. Will be fixed in the next version.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
can you help me understand why it always fails compilation. See image.
I did sdk and B4A updates.

Thank you
 

Attachments

  • Immagine.png
    191.1 KB · Views: 248

corwin42

Expert
Licensed User
Longtime User
can you help me understand why it always fails compilation. See image.
I did sdk and B4A updates.

Thank you

Which android.jar do you use? You must use at least API23.
 

alimanam3386

Active Member
Licensed User
Longtime User
Hi

I use ver 3.2 of appcompact and b4a ver 6.0 when I use bellow extends in activity my app get a force close , how can I do

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

When I comment above line of code , my app run ok
 

corwin42

Expert
Licensed User
Longtime User
Hi

I use ver 3.2 of appcompact and b4a ver 6.0 when I use bellow extends in activity my app get a force close , how can I do

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

When I comment above line of code , my app run ok

If you don't use the #Extends line your app will crash if you add one of the AppCompat Views to your activity.

To get the stack trace connect the device via USB and look at the unfiltered logs (disable Filter checkmark in log window).
I'm not completely sure but if you connect with B4ABridge you may see the log (unfiltered) if you compile in debug mode.
 

alimanam3386

Active Member
Licensed User
Longtime User


Hi

I compiled in debug mode and get this exception :

B4X:
Exception while inflating <vector>
org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector> tag requires viewportWidth > 0
   at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:535)
   at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
   at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
   at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
   at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
   at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
   at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
   at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
   at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
   at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
   at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
   at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
   at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
   at com.manamsoftware.smilepro.main.onCreate(main.java:33)
   at android.app.Activity.performCreate(Activity.java:5231)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
   at android.app.ActivityThread.access$800(ActivityThread.java:135)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5017)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   at dalvik.system.NativeStart.main(Native Method)
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x415ec560)
FATAL EXCEPTION: main
Process: com.manamsoftware.smilepro, PID: 11137
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manamsoftware.smilepro/com.manamsoftware.smilepro.main}: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
   at android.app.ActivityThread.access$800(ActivityThread.java:135)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5017)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
   at android.content.res.Resources.loadDrawable(Resources.java:2096)
   at android.content.res.Resources.getDrawable(Resources.java:700)
   at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
   at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
   at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
   at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
   at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
   at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
   at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
   at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
   at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
   at com.manamsoftware.smilepro.main.onCreate(main.java:33)
   at android.app.Activity.performCreate(Activity.java:5231)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
   ... 11 more
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
   at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
   at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
   at android.content.res.Resources.loadDrawable(Resources.java:2092)
   ... 30 more
  Force finishing activity com.manamsoftware.smilepro/.main
GC_CONCURRENT freed 311K, 7% free 5317K/5668K, paused 2ms+1ms, total 21ms
Indexing F334C202E62F134C5D3E33814E82D69848C390E0 from com.google.android.gms
Screenshot max retries 4 of Token{41cd5208 ActivityRecord{41d5e558 u0 com.manamsoftware.smilepro/.main t43 f}} appWin=Window{4225ba10 u0 Starting com.manamsoftware.smilepro} drawState=4
Screenshot failure taking screenshot for (600x1024) to layer 21010
GC_CONCURRENT freed 474K, 8% free 6474K/6984K, paused 3ms+7ms, total 45ms
User is not opted-in to Usage & Diagnostics or Lockbox.
Indexing done F334C202E62F134C5D3E33814E82D69848C390E0
Indexing C2EE8E0B35C4CE284B59BF463F1D3E5E05CE4C9B from com.google.android.googlequicksearchbox
GC_CONCURRENT freed 466K, 9% free 5602K/6108K, paused 2ms+2ms, total 34ms
Indexing done C2EE8E0B35C4CE284B59BF463F1D3E5E05CE4C9B
Indexing F334C202E62F134C5D3E33814E82D69848C390E0 from com.google.android.gms
Indexing done F334C202E62F134C5D3E33814E82D69848C390E0
Activity pause timeout for ActivityRecord{41d5e558 u0 com.manamsoftware.smilepro/.main t43 f}
Timeline: Activity_windows_visible id: com.android.launcher time:4048603
wanglei --> mBatteryProps.batteryTemperature = 300
mHoldBootFastWakeLock = true
[CTRL_IFACE]wlan0: SIGNAL_POLL
schedule task: 2000
[CTRL_IFACE]wlan0: SIGNAL_POLL
[CTRL_IFACE]wlan0: SIGNAL_POLL
wanglei --> mBatteryProps.batteryTemperature = 300
Starting window AppWindowToken{42105518 token=Token{41cd5208 ActivityRecord{41d5e558 u0 com.manamsoftware.smilepro/.main t43}}} timed out
[CTRL_IFACE]wlan0: SIGNAL_POLL
Activity destroy timeout for ActivityRecord{41d5e558 u0 com.manamsoftware.smilepro/.main t43 f}
wanglei --> mBatteryProps.batteryTemperature = 300
mHoldBootFastWakeLock = true
[CTRL_IFACE]wlan0: SIGNAL_POLL
[193] com.google.android.finsky.c.e.run(1151): Replicating app states via AMAS.
GC_CONCURRENT freed 1340K, 19% free 6054K/7440K, paused 5ms+14ms, total 78ms
[193] com.google.android.finsky.c.c.a(311): Completed 0 account content syncs with 0 successful.
[1] com.google.android.finsky.services.j.a(149): Installation state replication succeeded.
[CTRL_IFACE]wlan0: SIGNAL_POLL
[CTRL_IFACE]wlan0: SIGNAL_POLL





 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Now it's work perfect! where was the problem ?!!!

Google included the usage of Vector Drawables into the support libraries some time ago. Vector drawables are supported directly with Android 5.0 and above. Thats why it worked on your Note4.
For older Android versions the support needs to be compiled into the apk. This "magic" feature was added in v23 of the build tools.

Erel said that B4A should prefer the v23 build tools (v24 is currently not compatible with B4A). But it seems that it uses an older version if it is installed. So deinstalling all older versions forced B4A to use the 23.0.3 version and all should be fine now.

I will change the instructions in the first post that users should deinstall all older versions.
 

koaunglay

Member
Licensed User
Longtime User
B4X:
LogCat connected to: 2008edd522bb
Unable to open log device '/dev/log/main': No such file or directoryUnable to open log device '/dev/log/main': No such file or directoryUnable to open log device '/dev/log/main': No such file or directory

Please what is my error?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…