Action Syntax in DecleX, an elegant code, easier to read and write

Android applications are written in Java; sadly, this is a very verbose language which makes the coding process really time consuming and the resultant code is very long. While programming for it, programmers make libraries to simplify it, but these cannot be automatically integrated in the Life Cycle of Activities and Fragments in Android without calling specific methods inside them.

DecleX is fully integrated with a powerful language analyzer which makes possible to write methods faster and easier. Action Syntax is one of the simplest way to make anything required in one step, using a single word named Action. All the dependencies are injected from within the context, and you only need to pass the parameters that really matter. The result: up to 90% less code while using DecleX for whole the development, which is faster and easier to write, much more readable and as an important extra, the maintenance time is reduced to a breeze.

I will introduce in this article this feature through several scenarios that are common in Android development.

For an introductory article about DecleX, I recommend you to read:

Navigating through the app

Creating all the navigation in an Android application using Action Syntax with DecleX is very simple, the actions methods have been simplified to the shortest ever version.

Let’s say we have an Activity (SettingsActivity) with three buttons, two of them will take us to fragments (ProfileFragment, NotificationsFragment) and the last button will logout the user after prompting for confirmation and it will return to another activity (MainActivity).

The code of our activity will look like this:

The first thing to note, is that our existing Activities and Fragment are now referenced placing the symbol “$” in front of the class name. No “context” parameter is needed to be passed to create the intents and call them, or any other complex structure (fragment managers or transactions). DecleX actions will inject all the dependencies from within the context.

To logout the user, it is used an action to ask for a confirmation “$AlertDialog”. It is easier to read and you have to provide only what really matters to show a dialog (the information it needs to display). And in this method (“logout”) we have one of the main feature of the Action Syntax, it is fully sequential. DecleX will parse this method to generate a runnable version. The Alert Dialog will be shown, and only if the user presses the “positiveButton”, it will continue the sequence returning to the MainActivity.

Note when calling a fragment, it is assumed this will be inserted in a GroupView with id “”, this behavior can be changed passing the container as parameter…


there’s many other options available, and there’s access to the transaction object in order to create animations or specify other parameters:

Using actions in fields

There’s a shorter version of the code above which can be used with DecleX, where the actions are called directly without the need of having a method. Here how it looks

So it is enough to specify the event which triggers the action (@Click), what to do when it is triggered (show $ProfileFragment or $NotificationsFragment) and apply this over a view with a given “id” (for instance, in the layout there’s a Button with id=”@+id/showProfile”) .

Activities and Fragments parameters. Activities Results.

DecleX it is completely based in AndroidAnnotations, so Activites or Fragments can pass between them easily parameters, and through actions it is even simpler. Let’s say the Settings Activity above takes a String parameter (named “userName”), which should be passed also to its fragments:

This SettingsActivity can be called from a different activity, for instance the MainActivity, which also checks the result of calling SettingsActivity:

in this example above, SettingsActivity will send back a result if any of the settings was changed, the code to execute when the activity returns is placed into an “if” block called an “Action Selector” which are explained in the next section.

Action Selectors

Java listeners are replaced in the Action Syntax by a simpler notation with an “if” and an action selector. Let’s say you need to display an Alert Dialog where the user can select from multiple actions, but it has also 2 buttons: “More Actions” and “Cancel”:

in this example we can see different events being handled with the Action Selectors.

It is shown also one of the fancy features of the Action Syntax, which permits to create strings using coded expressions, simply enclosing the code with curly brackets, so in this example above the text passed to the Toast:

“Action selected was {actions[position]}”

it is directly converted by the framework to the Java equivalent:

"Action selected was " + actions[position]

Accessing callback variables
When an action selector is used, it is frequently necessary to access to the callback variables. Let’s say, “position” in the example above. If Formatted Syntax is used, they can be accessed directly (like “{actions[position]}” in the previous example), but if you need them in the code, then you should declare the variable with a default value starting with the symbol “$”:

Events and Actions

DecleX is fully integrated with EventBus and for every single event created in the project, an action can be used to post this event (and get results from it).

An event can be created in DecleX (with or without parameters) with the receiving method, for instance:

Then this event can be triggered from anywhere in the application:


See that with this event mechanism you can interact easily with different part of your app (Activities, Fragments, Services) without having to create Intermediate objects (DecleX will handle this for you, in the provided example, a class named “AuthenticationDone” will be created to handle the event with EventBus).

Easy Multi-threading Control

Action Syntax makes really simple switching between threads. For doing so, there’s an action for switching to the UI Thread ($UIThread) and to create a new Background Thread ($Background).

Let’s say there’s some big list which needs to be sorted in background to avoid problems in the UI, but later you need to send the sorted result to a RecyclerView (with id “itemsList”), so this should be done in the UIThread (since you can modify Views only in the Main Thread). The code would be something like:

Displaying and gathering information

DecleX gives support to a set of components which are commonly used in the development and come with Android SDK.

A Progress Dialog is used in Android frequently to indicate an ongoing action. DecleX comes with a builtin Action for it, where you can easily show it and hide it whenever it’s required using the action “$ProgressDialog”:

Alert Dialogs are supported, though an action named “$AlertDialog” which puts all the features of AlertDialog.Builder into a simpler DecleX Action:

DateDialog and TimeDialog
Frequently it is needed to request from the user a Date or Time. DecleX has builtin the actions to interact with the corresponding dialogs in Android named “$DateDialog” and “$TimeDialog”:

Toasts are commonly used in Android to display useful information to the user. There’s an action “$Toast” to be used with them which accepts a string using Formatted Syntax.

System notifications are another frequently used method to display information to the user. The action for it supports all the options of the notification builder for Android, and simplifies showing images or assigning pending intents.

Handling Models, Populating/Recollecting the UI

DecleX comes with an amazing mechanism to handle Models injections and populate/recollect the injected fields into/from the UI.

To inject models it is used the action $LoadModel which starts the injection mechanism of DecleX (loading directly the model from a specific mean, like SQLite, Network or Preferences). With $PutModel it is stored back the specific object. These two actions run also a $Populate and $Recollect actions when they are executed, populating the user interface with the injected model and reading the user input back (executing validations if they are defined).

This is completely addressed in the article:

Programming Custom Actions

Actions can be easily programmed. Actions are @EBean objects which are injected with the action call itself.

The framework comes with a set of builtin actions, and other are created for its enhanced components, but the programmer can use this mechanism to construct powerful libraries which could use all the injections capabilities of the Actions.

See more about it in DecleX documentation: Programming Actions

What’s next

DecleX is in constant development and improvement, and the Action Syntax together with all its mechanism has many important enhancements scheduled for the next releases:

Action Extensions
With actions we can have Extensions capabilities in Java (similar to those of C# or Swift languages) which makes programming even simpler to read and write. This feature is in current development and detailed in Github. This will permit an even simpler code for Actions:

Action support for Functions
Methods which have a result should return it in the same call, but the Actions mechanism could address future results automatically, for instance:

Note that the selected type index is returned only after the user selects a value, so this would be in some moment in the “future”. Actions could handle this automatically so the method “getInformationType” would be treated as an action itself (it will return it’s information later, and then the $Toast in the method “sendInformation” will be executed.

Actions with 3rd party libraries
DecleX intends to integrate 3rd parties libraries to make coding with them easier. Let’s say, FacebookSDK could be integrated with a Plugin, and calling the SDK features could be handled by Action Events or specific Actions. This would make possible to rapidly integrate all these libraries within your programs using DecleX (as simply as using one line of code to invoke any action, or one annotation to automatically configure all the 3rd party library).

Where to start?

If you haven’t started using DecleX then you can find much more information and Examples on its page in GitHub:

the main goal of the project is to speed up Android Development simplifying every single task that should be done to make an application, and then permitting to give faster maintenance to these apps.

On its way, the ultimate goal is to have the simplest-ever framework for Android Development, which could be used to create applications faster, focusing in what really matters while programming, removing all that boilerplate code which most of the time programmers are forced to repeat.

I will keep posting the features of the framework, and also features to come.

Let me know your thoughts about Action Syntax or any other feature in DecleX in the comments, don’t hesitate to suggest enhancements or contribute to its development, and feel free to contact me for any question regarding the framework ;-).

Self-Starting in the IT world, my aim is to be an active part in the creation of the future technologies.