React Native for Android: yet another Context

If you have to inject / create a React Native module into your Android Activity (that does not extend the default React Native Activity), please beware that the Activity lifecycle is not tied to the React Context lifecycle used to create the RN module.

So you have to be careful to retrieve it at the right moment, bearing in mind that a developer can hot reload (pressing double R on the simulator), causing the creation of a new React Context.

Also, the framework gives you a way to listen to the lifecycle of the topmost React Native Acitivity, so that you don’t need to hold a reference to it.

To show how to solve these things, I wrote a short example that you can find here.

Please bear in mind that the Javascript part is very dumb. The main point is the Native / Java (yeah, no Kotlin yet for me) part.

Here is what the running app look like:

React Native for Android example

Continue reading

Change locale in an Espresso test

You can change the locale programmatically in an Espresso test too.

Here you can find a quick example I wrote.

The key part is to restart the Activity after you have set the new Locale.

Of course you can also set the flag of ActivityTestRule to manually re-launch the Activity so that you launch it after you set the new Locale.

Just ensure you have a new Activity after you change Locale.

Bear in mind that to change the locale taking into account all the deprecations and sdk versions is more complex than the code I wrote in my example. Read more here.

Android Searchview and the magnifying icon

Just a quick note about the android.support.v7.widget.SearchView.

Normally the widget has the magnifying on the left side if expanded. So if you do something like this:

SearchView searchView =
   (SearchView) findViewById(R.id.search_view);
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);

We will end up with something like this:

Android Searchview standard

If we need to manipulate the magnifying icon, we need a reference to that View:

ImageView searchViewIcon =
   (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

After that just retrieve the parent ViewGroup. It is, at least today, a LinearLayout, so just remove the View and add it again to have it at the end / right side of the SearchView.

ViewGroup linearLayoutSearchView =
   (ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
linearLayoutSearchView.addView(searchViewIcon);

So we have something like this:

Android Searchview right

Of course, it is a standard View, so if we need to remove it altogether, just set its properties accordingly (from here):

searchViewIcon.setAdjustViewBounds(true);
searchViewIcon.setMaxWidth(0);
searchViewIcon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
searchViewIcon.setImageDrawable(null);

Andersen Festival for Muzei – Source Code

As I wrote in the previous post, I made a plugin for the Muzei Live Wallpaper of Roman Nurik, containing the portraits of Hans Christian Andersen, made by illustrators who will held an exhibition at the Andersen Festival.

You can find the plugin in the Play Store; it is an open source project (licensed under Apache v2) and you can find the source code in my github repository.

Please note that I don’t own the portraits, so you won’t find the assets (the images) with those illustrations in the repository: it won’t compile at first but I wrote a readme in the assets folder with a list of the missing files.

Hans Christian Andersen by Matilde Martinelli

In this post I will explain some aspects about writing a plugin for Muzei.

Continue reading

Andersen Festival for Muzei

For the Andersen Festival, 14 illustrators will held an exhibit where they will show their drawings depicting 14 fables of Hans Christian Andersen.

In these days preceding the festival, they have drawn portraits of Andersen himself, to delight us during the waiting.

I liked them so much that, with the artists’ blessing, I have gathered all these portraits in a plugin for Muzei, you can find it here: https://play.google.com/store/apps/details?id=com.luongbui.andersenfestival.muzei

Please remember that you have to install Muzei first: https://play.google.com/store/apps/details?id=net.nurik.roman.muzei

This is an initial release and not all the artists are present, yet.

I will release the source code under an open source license: if interested, check this blog for updates.

Update: read the next post for the source code!

Pure Data for libGDX

In the last months I dwelled a bit in the game development world, playing around with libGDX and Spine 2D.

I think that they are really good for 2D games (especially if you want to support Android and iOS) and that they perform very well in renderings and animations.

However I think that libGDX support for audio can be improved a bit.

Don’t get me wrong: libGDX has a good support for audio but I miss the days when I played around with Pure Data.

So I decided to write a small libGDX backend for libpd, the famous library that turns Pure Data into an embeddable audio synthesis library.

The repository is called pd-for-libgdx and you can find it in my github: https://github.com/manhluong/pd-for-libgdx.

It’s open source, Apache v 2.0.

Technically I just wrote a thin layer that let you call pd-for-andoid and pd-for-ios, through bindings of RoboVM‘s Bro, from your platform-specific libGDX projects.

The backend is still a work in progress (actually it supports only Android and iOS) but it already let you play sounds, like in the pianotest project:

This backend is designed to be used from source, cloning the repository and adding the projects and dependencies to your Eclipse workspace.

Also, keep in mind that I am still using the old way how libGDX handle the projects. That means no Gradle, for now.

Please read futher if you want to know how to use the backend and for some technical details about libGDX.

Continue reading

Peng! With flashlight support

One way to easily find your phone in the dark is to turn on the flashlight, if your device has one. So now Peng let you turn it on and off with a dedicated menu item.
Peng
Not all devices has the flashlight and the Pebble watchapp most take account of that. So the relative menu item displays three statuses: turned on, turned off and not available.
When not available, the user can still press SELECT to manually send a check to the phone, in case the flashlight is not detected for some reason.
Peng Flashlight
Continue reading

Peng! 0.2 + 0.0.2

First of all: thank you for using my little test watchapp!

As a quick and dirty hack I made in a few hours, I did not expect lots of adoption, but it seems from the Android Developer Console stats that there are few active installations out there!

🙂

If you have any feature request or comments, please write them below!

I have just made a quick update, both to the Pebble watchapp and to the Android app.
Continue reading