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,715
  • AppCompatLib3_51.zip
    87.2 KB · Views: 2,481
  • AppCompatLib3_52.zip
    87.3 KB · Views: 4,407
  • AppCompatLib4_00.zip
    181.8 KB · Views: 4,575
Last edited by a moderator:

dragonguy

Active Member
Licensed User
Longtime User
if I have two activity - Main, Second, can I set different color for two activity?

colorPrimaryDark value only can set in Main activity but how can i set Second activity colorPrimaryDark value?
 

corwin42

Expert
Licensed User
Longtime User
if I have two activity - Main, Second, can I set different color for two activity?

colorPrimaryDark value only can set in Main activity but how can i set Second activity colorPrimaryDark value?

Use two themes and set each theme to one activity:

B4X:
'For all other activities
SetApplicationAttribute(android:theme, "@style/Theme.AppCompat")

'Different themes for special activities
SetActivityAttribute(Main, android:theme, "@style/MyAppTheme1")
SetActivityAttribute(Second, android:theme, "@style/MyAppTheme2")

CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme1" parent="@style/Theme.AppCompat">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#F57C00</item>
        <item name="colorAccent">#FFA726</item>
    </style>
    <style name="MyAppTheme2" parent="@style/Theme.AppCompat">
        <item name="colorPrimary">#009800</item>
        <item name="colorPrimaryDark">#007C00</item>
        <item name="colorAccent">#00A726</item>
    </style>
</resources>
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Hi @corwin42 .
Great library and thank you very much for this.
Anyway i see that using version 3.20 works without any problem on Android 5 or >. But crashes if you use Android 4 (which does not happen on the version 2 library that works on both Android version 4/5/6).
Any suggestion ?
 

chuath

Member
Licensed User
Longtime User

I had this problem before and solved it with @corwin42's help on post #199
Follow the steps and it should be resolved.
particular this step
Check if the only existing folder in <SDK>\build-tools is the 23.0.3 folder.
 

Anser

Well-Known Member
Licensed User
Longtime User
Hi Corwin,

V3.30
  • New: Support for ActionViews in MenuItems (Needed for CustomViews in NavigationView of DesignSupport library)


With the new version of AppCompat V3.30, will it be possible to show a badge on the ACMenuItem displayed on the ToolBar as shown on the below given post ?

https://www.b4x.com/android/forum/t...cmenuitem-shown-on-toolbar.68900/#post-446042

I have seen many apps with this feature, unfortunately, till now, I could not find any other way to get this done on my app. Or am I in the wrong track to get this done ?

Regards

Anser
 
Last edited:

corwin42

Expert
Licensed User
Longtime User

Yes. It should be possible to put nearly every View as an ActionView to the ActionBar/ToolBar now.

Hello! Is there a way to change the corner radius and elevation for ACButton?

You can't change the corner radius because it is in the default background drawable of the button.
You can change the Elevation of every view with the AppCompat.SetElevation() method (on some view types this won't have an effect but ACButton should work).
 

corwin42

Expert
Licensed User
Longtime User
Any sample available demonstrating the usage of the above said.

Currently I have none. I needed the feature for the upcoming version of DesignSupport library. But because it works there it should work in the Toolbar, too.
I will create an example in near future. Maybe I will write some small tutorials for this and other new features.
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I am using ACCheckbox and I want it to be in a custom white color. I am using the below method to set the checked color given in the link https://www.b4x.com/android/forum/threads/checkbox-checked-color.61779/#content
B4X:
'Pass the Checkbox object and Colors int values to set for each state.
Sub SetColorTintList(CB As CheckBox,Checked As Int,Enabled As Int,Disabled As Int)

    Dim States(3,1) As Int

    States(0,0) = 16842912   'Checked
    States(1,0) = 16842910    'Enabled
    States(2,0) = -16842910 'Disabled


    Dim Color(3) As Int = Array As Int(Checked,Enabled,Disabled)

    Dim CSL As JavaObject
    CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,Color))
    Dim CB1 As JavaObject = CB
    CB1.RunMethod("setButtonTintList",Array As Object(CSL))

End Sub

It works great on Android 5 and 6 but I am getting the below error in honor4x Android 4.4.4

B4X:
Exception java.lang.RuntimeException: Method: setButtonTintList not found in: android.support.v7.widget.AppCompatCheckBox

I checked the google devloper website and the method does not exists but an inherited xml from compactbutton class is present. Any idea what can be done.

https://developer.android.com/reference/android/support/v7/widget/AppCompatCheckBox.html

Best,
SK
 

corwin42

Expert
Licensed User
Longtime User

Reids

Member
Licensed User
Longtime User
Hi corwin, it seem the appacompat lib has a bug in .ButtonColor properties on version 36 google support library, which button displayed as gray button but
on previous version of 23 google support library button working nicely *this problem occured in android version 4, which not occured on version 6.
Oh if you suggest me to use .color properties the button will have bigger padding which is not elegant
your appcompat library ACbutton has .ButtonColor properties which is very cool!
I forced to use version 36 because design support require support lib of version 36

Thanks
 

roberto64

Active Member
Licensed User
Longtime User
Are you using a light or dark Theme? It should be ok if you use a light theme.

hello Corwin42, in ACEditText InputType Phone ownership and Numeric, the keypad appears with letters instead of the keyboard with the numbers?
thank youh
 

corwin42

Expert
Licensed User
Longtime User
help! please!

Remove the #AdditionalRes lines except the ..\resource one.
As DonManfred said, read the first post for detailed project setup instructions.
 

Lildinti

Member
Licensed User
Longtime User
Hi Corwin,
Love the library but have a question: How do you define other items in the theme? For example primary text color, divider color, etc?
If I add them to the manifest, compilation gives error:

Generating R file. Error
res\values\theme.xml:7: error: Error: No resource found that matches the given name: attr 'colorTextPrimary'.


Any thoughts?
Thanks heaps

lildinti
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…