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:

koaunglay

Member
Licensed User
Longtime User
This is my setting and installed sdk!

what do I need to do?
This is Manifest
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<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/Theme.AppCompat")
SetApplicationAttribute(android:theme, "@style/MyAppTheme")

Please
 

koaunglay

Member
Licensed User
Longtime User
Thanks you for your reply.
I really installed
1. AppCompat - jar, aar, xml
2. Android SDK Build-tools 23.0.3 and android support repository 33
3. paths Configuration = android-23 jar
4. #Extends: android.support.v7.app.AppCompatActivity
5. used themes like Theme.AppCompat
So I don't know other. Which one should I install in sdk?
My connection is not good for install all sdk. At the time I want to install just really need.
Thanks anyway. Sorry for my english.
Read the setup info for b4a 6. You are missing some things in sdk
 

corwin42

Expert
Licensed User
Longtime User

corwin42

Expert
Licensed User
Longtime User

Logging seems to be disabled on your device. Without a proper log it would be hard to find the exact problem. Try to find a way how to enable logging on your device. There are some threads here in the forum. Otherwise try to use an emulator.

Do you use the example project or is it your own project? The manifest file you posted here is not from the example.

Do you use B4A 6.0 final version (not beta)?

Which Android version has your device?

Most important would be to get a proper log.
 

Reids

Member
Licensed User
Longtime User
I got this error, app is run okay, but when I close and open back this warning showup
Android version 4.4.4 I run app compat with msmaterial drawer which share same xml

Error Log
B4X:
java.lang.NullPointerException: expected receiver of type android.app.ActionBar, but got null
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at org.myapp.app.main.onCreateOptionsMenu(main.java:128)
	at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
	at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:358)
	at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
	at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:270)
	at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
	at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
	at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
	at android.view.Choreographer.doCallbacks(Choreographer.java:574)
	at android.view.Choreographer.doFrame(Choreographer.java:543)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
	at android.os.Handler.handleCallback(Handler.java:733)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5146)

My Manifest File
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14"/>
<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")

CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#0098FF</item>
        <item name="colorPrimaryDark">#007CF5</item>
        <item name="colorAccent">#AAAA00</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
</resources>
)
SetApplicationAttribute(android:theme, "@style/MaterialDrawerTheme.Light")
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.WRITE_CONTACTS")

Msmaterial drawer has 2 more additional xml file which
B4X:
#AdditionalRes: path\compact\res, android.support.v7.appcompat
#AdditionalRes: path\md-lib
But I didn't using first additional res cause it conflicted with appcompat res, so I comment it, app run fine but sometime that error show up, I din't know which is cause it

Refer on error method at org.myapp.app.main.onCreateOptionsMenu(main.java:128)
my java code is same as example
B4X:
#If Java
	public boolean _onCreateOptionsMenu(android.view.Menu menu) {
		if (processBA.subExists("activity_createmenu")) {
			processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
			return true;
		}
		else
			return false;
	}
#End If

My project oncreate Activity code is
I comment it because if I enable this error show up more frequent
B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
	'Menu.Clear
End Sub

Is this lib compatible with msmaterial drawer?
 
Last edited:

dragonguy

Active Member
Licensed User
Longtime User
how can raise the menu item menu click event?
here is my code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ActionBar As ACToolBarDark
End Sub

'Inline Java code to initialize the Menu
#If Java
    public boolean _onCreateOptionsMenu(android.view.Menu menu) {
        if (processBA.subExists("activity_createmenu")) {
            processBA.raiseEvent2("", true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
            return true;
        }
        else
            return false;
    }
#End If

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    ActionBar.SetAsActionBar
End Sub

'This is the Sub called by the inline Java code to initialize the Menu
Sub Activity_CreateMenu(Menu As ACMenu)
    'Menu.clear
    Menu.Add2(1, 1, "About", Null)
End Sub

Sub ActionBar_MenuItemClick (Item As ACMenuItem)
    'when i  click the item
       'Never come here
End Sub
 

corwin42

Expert
Licensed User
Longtime User

This may be not related to your error but setting the theme two times makes no sense.
If you create your own theme you should set "@style/MaterialDrawerTheme.Light" as the parent of your own theme.


If you don't use a Toolbar and don't use a customizable menu you should remove the Java code and the menu initialization.

Is this lib compatible with msmaterial drawer?

Yes.
 

corwin42

Expert
Licensed User
Longtime User

dragonguy

Active Member
Licensed User
Longtime User
one one more question about the ACEditText, how can i change the color of the line when it get focus?
 

corwin42

Expert
Licensed User
Longtime User
one one more question about the ACEditText, how can i change the color of the line when it get focus?
This is "colorAccent" of your theme.
 

Reids

Member
Licensed User
Longtime User

Hi corwin I use a toolbar and I can confirm the problem is related to
B4X:
Activity_ActionBarHomeClick
it appear not a serious error just a warning message that say java.lang.NullPointerException: null receiver
Everytime I put that event, the warning message show up, I using actionbar with code not with designer, designer doesn't show any warning message, but adding actionbar by code the error will showup

I do some research either put appcompat actionbar with designer or with code is really big difference
I tried this when put appcompat actionbar in code the event won't triggered, but with designer the event is trigered
B4X:
ABHelper.Initialize
ABHelper.ShowUpIndicator = True
ActionBar.InitMenuListener

B4X:
Sub ActionBar_NavigationItemClick
   Activity.Finish
End Sub
 

corwin42

Expert
Licensed User
Longtime User

Do you have a small example project which shows this error?
 

corwin42

Expert
Licensed User
Longtime User
Thanks, this helped a lot.

1. The exception occurs only if the Activity_ActionBarHomeClick event exists in the Activity code. This is caused by B4A generated code that tries to enable the home button. The problem is that the B4A code tries to get the standard ActionBar but because we use AppCompat with Toolbar this does not work. The good thing is that the exception will not do anything bad and is just ignored.

2. The problem that ActionBar_NavigationItemClick is not triggered when the Toolbar is added by code is a bug in the AppCompat library. For a workaround do not use uppercase letters for the EventName of the ToolBar. If you initialize the Toolbar with
B4X:
ActionBar.Initialize("actionbar")
everything should work as expected. (The event sub name can still be Upper/Mixed case only the EventName in Initialize() should be lower case)

The bug will be fixed for the next version but it will take some time for the release. I'm currently on vacation and have only a Netbook with me. Running Eclipse on it is not much fun.
 
Last edited:

Reids

Member
Licensed User
Longtime User
The bug will be fixed for the next version but it will take some time for the release. I'm currently on vacation and have only a Netbook with me. Running Eclipse on it is not much fun.
Finally the actiobar_NavigationItemClick got working,this event is enough to solve this problem
Thank You Very Much!, have a good day there~
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…