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:

corwin42

Expert
Licensed User
Longtime User

mshafiee110

Active Member
Licensed User
Longtime User

joneden

Active Member
Licensed User
Longtime User
Sorry my bad, although it had worked I forgot that I needed to update the libs in the custom libs folder with the newer SDK files. I'm a dumbass

3. Copy v4 and v7 support libraries from <...>\android-sdk\extras\android\support\v7\appcompat\libs to your CustomLibs folder
 

migrec

Member
Licensed User
Longtime User
Hey @corwin42, with support library 23.1 setelevation method seem to not work.
This is my code: AppCompat1.SetElevation(actionbar, 4dip) where actionbar is an ACToolbarDark
if i Log appcompat1.getelevation(actionbar) i get 0
is this something you could look at?
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I am using Theme.AppCompat.Light.NoActionBar as my parent theme. When the search view is activated, the back arrow is coming in black. I need to change the color to white. Any suggestions how to do that?

Edit : I got it :)

B4X:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="homeAsUpIndicator">@drawable/name  of the icon in resouce folder (the arrow image)</item>
</style>

Best,
SK
 
Last edited:

Reids

Member
Licensed User
Longtime User
I got error this while running the sample, no error while compiling, I've read every page on this thread, follow the instruction
> update the latest sdk & android extra
> makesure the latest b4a_appcompat file by deleting old file
> copy android-support-v4.jar & android-support-v7-appcompat.jar to library folder
> use android-sdk\platforms\android-23\android.jar while compiling
but error still there, I don't know what I'm missing at

B4X:
threadid=1: thread exiting with uncaught exception (group=0x419ecce0)
FATAL EXCEPTION: main
Process: de.amberhome.appcompat.example2, PID: 13517
java.lang.NoSuchFieldError: android.support.v7.appcompat.R$styleable.ActionBarWindow
    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
    at de.amberhome.appcompat.example2.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:2169)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5146)
    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:732)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at dalvik.system.NativeStart.main(Native Method)
  Force finishing activity de.amberhome.appcompat.example2/.main
Screenshot max retries 4 of Token{41e84e58 ActivityRecord{41ffb368 u0 de.amberhome.appcompat.example2/.main t47 f}} appWin=Window{41e148a8 u0 Starting de.amberhome.appcompat.example2} drawState=4
Screenshot failure taking screenshot for (720x1280) to layer 21015
Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=de.amberhome.appcompat.example2

I google up "java.lang.NoSuchFieldError: android.support.v7.appcompat.R$styleable.ActionBarWindow" maybe problem with android-support-v7-appcompat.jar?
but I already using the latest android-support-v7-appcompat.jar
 

corwin42

Expert
Licensed User
Longtime User

Attachments

  • AppCompatRipple.zip
    7.8 KB · Views: 299

androh

Member
Licensed User
Longtime User
I want to ripple effect on switch view. But including text, whole row.
How can I do that?

Edit: That's ok. I did that very easy with your example :)
Example attached.
 

Attachments

  • SwitchRipple2.zip
    20.5 KB · Views: 319
Last edited:

migrec

Member
Licensed User
Longtime User
I recently wrote that setelevation is not working with support lib 23.1 installed.
And this doesn't work either:
B4X:
actionbar.PopupTheme = actionbar.THEME_LIGHT
Does anyone have knowledge regarding this issue?
 

corwin42

Expert
Licensed User
Longtime User
I recently wrote that setelevation is not working with support lib 23.1 installed.
And this doesn't work either:
B4X:
actionbar.PopupTheme = actionbar.THEME_LIGHT
Does anyone have knowledge regarding this issue?

Be aware that SetElevation() does not work with every View. If it does not work with the View you want then you can put the view on a Panel and set the elevation for the Panel.

PopupTheme should work. Please provide an example project to show that it does not work. I don't have time to create a testcase myself for all bug reports.
 

migrec

Member
Licensed User
Longtime User
Be aware that SetElevation() does not work with every View. If it does not work with the View you want then you can put the view on a Panel and set the elevation for the Panel.

PopupTheme should work. Please provide an example project to show that it does not work. I don't have time to create a testcase myself for all bug reports.
Solved the popup theme just by changing the theme from .noactionbar to theme.appcompat.light, but about the setelevation, it doesnt even work with the toolbar, i remeber it did before.
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I want to use a drawable for the thumb of the seekbar, how should one write the xml attributes to change it? I was referring to the google doc in the below link but I am getting an xml error.

https://chromium.googlesource.com/a...pport/v7/appcompat/res/values/styles_base.xml

I am using the sample theme given in the example.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=
"MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name=
"colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#F57C00</item>
<item name="colorAccent">#FFA726</item>
<item name="windowNoTitle">true</item>
<item name=
"windowActionBar">false</item>
</style>
</resources>

Any suggestions appreciated. Need to change the seekbar thumb drawable using xml attribute
B4X:
android:thumb

Best,
SK
 

Alisson

Active Member
Licensed User
corwin2 your exemple result the error in the xml:

upload_2016-1-29_10-52-29.png


This my manifest editor:

upload_2016-1-29_10-52-59.png


This is my code xml:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2015 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<resources>

    <style name="Base.Theme.AppCompat" parent="Base.V22.Theme.AppCompat" />
    <style name="Base.Theme.AppCompat.Light" parent="Base.V22.Theme.AppCompat.Light" />

    <style name="Base.V22.Theme.AppCompat" parent="Base.V21.Theme.AppCompat">
        <item name="actionModeShareDrawable">?android:attr/actionModeShareDrawable</item>
    </style>

    <style name="Base.V22.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light">
        <item name="actionModeShareDrawable">?android:attr/actionModeShareDrawable</item>
    </style>

</resources>

You know solve the error?

I wait your feedback.

Thanks very much!
 

corwin42

Expert
Licensed User
Longtime User
Sorry but it doesn't, my phone is running 6.0.1 it works with a few other views but not the toolbar

A workaround would be to place the Toolbar on a panel and elevate the panel.

You know solve the error?
No. Everything I can say is that there is an error in your setup. Please check that really everything is up to date and that you use v23 android.jar. See the troubleshooting guide in the first post of the Appcompat library thread.
 
Top