Skip to content

Android Declares

Using and writing declares for Android is very similar to other platforms. The syntax is mostly the same, but we’ve made some additions to make Declares that change Controls easier. Let’s walk through a couple of easy-to-understand Declares to show you how they’re made.

First, let’s just get the Android version number, the kind a user might see if they were looking for what version of Android they were running on. As with any Declare, we first look up the call in the platform documentation. For Android, that is typically here:

After searching the platform docs, we find out how to get the version here:

The “Lib” section of the Declare will contain the “path” up to the function/property that we want to get the value. We get this from that page as:


The actual property we want is called “RELEASE” (“The user-visible version string.”) and since we want our Declare function to be called something different, we’ll set that as an alias.

In summary, the full Xojo Declare to get the Android version would be:

Declare Function AndroidVersion Lib "android.os.Build.VERSION" Alias "RELEASE" As CString

We use “CString” for any String coming from the Android platform for both ease of use and Declare compatibility – even though the String type from the platform may vary internally, you do not need to worry about that.

You can find this Declare in the Examples section of Xojo in the Platform>.Android>Declare>Declare project.

Object Declares

With Android, there is a special new Declare format that allows us to make calls directly against certain objects and controls. This comes in handy if we want to easily make a Declare that changes the property of a control. As an example, let’s set the background color of a MobileButton.

The typical way to use this new type of Object Declare is by adding an Extends method to a Module. Since we want to extend MobileButton with the ability to change its color, we’ll put a method on a Module called “SetBackColor” with the following parameters: “Extends ctrl As MobileButton, c As Color”

We look through the platform docs again, and learn that in Android, a Button is considered a View and thus has the following method:

We don’t need an Alias this time since the extension method is what we’ll call in Xojo code. What we’ll do instead is make the Declare like this:

Declare Sub setBackgroundColor Lib "Object:ctrl:MobileButton" (myColor As Integer)

Breaking this down, we’re calling the setBackgroundColor function. The Lib denotes this new type of Object declare: an Object, separated by a colon, the Xojo name of the object (in this case, our “ctrl” parameter), followed by another colon, and the Xojo type of the object.

The next and final line of our extension method will actually call the declared function:


You can see this Declare and its helper method in action from the Examples section of Xojo in the Platforms>Android>Declare>ButtonBackgroundColor project.

We’ve already seen users using Declares to extend their Android projects during our pre-release phase, and some are planning to make Declare projects available to everyone in the future to make using them even easier!

Travis got started with computers as a kid with a TI-99/4A. He moved on to the original Macintosh when it was released and has been programming ever since. He’s been using Xojo in various ways since v3 back in 2001. When not programming he’s usually found playing around with VR, music, games, or watching a good show.