ラベル Activity Starter の投稿を表示しています。 すべての投稿を表示
ラベル Activity Starter の投稿を表示しています。 すべての投稿を表示

2019年1月2日水曜日

Make a mini calendar app for Android, celebrating the New Year

Celebrate the New Year and make a small app! As you can see from the calendar, there are several months with the same day of the week. So, let's make an application that groups months by the day of the week on the 1st day as a key.

[ For Japanese version, please click here.]

grouping months by the day of the week on the 1st day

Although this mini app is not so practical, I will make it with the following five goals:

[1] Make the application logic in Java for Android.
[2] Use lambda expressions, streams, and the new date classes provided by Java 8.
[3] Create a GUI with App Inventor and call Java from there.
[4] Create as a general-purpose framework applicable to other applications.
[5] Customize the ActivityStarter to create a new block.

First, I will talk about [1] and [2]. With the recent Android, we can use Java 8 features. So, I'm going to write this app as concisely as possible using lambda expressions and streams. The logic part is as follows. The essential part is only one assignment statement to a Map object named "mp". The element of this object is a pair of "day of the week" and "a list of months whose first day matches that day of the week".


Next is [3] and [4]. Java programs like the above can be called using App Inventor 's Activity Starter function (block). I will use it, but in order to make it a general-purpose framework, I prepared a template for both the App Inventor side and the Java side. On the Java side, I prepared an input processing part and an output processing part beforehand so that users can complete it by rewriting only the application logic. Also, the App Inventor side has been designed to be applicable to many other applications as shown below. 

A generic App Inventor application that calls Java programs

With regard to the last item [5], I wanted to slightly change the input data and parameters passing to Activity Starter, as a result of considering the versatility as described above. So, I customized ActivityStarter and created new blocks shown below, using AppyBuilder Code Editor. I / O with the Java side was designed to do via a text file. Regarding the input, I provide two ways, passing the path of the input file and passing the text data directly. The boolean parameter "isFile" in the Setup block blow makes this selection possible.


Newly created App Inventor block (customized version of Activity Starter)

The entire App Inventor program created using this new block is as follows.


App Inventor program with newly created block

[Note] About Android and Java 8
Under Android 5.0 (API level 22), it is Java 7 compliant and Java 8 cannot be used. Android 6.0 (API level 23) can use part of lambda expressions etc., but seems to require Android 8.0 (API level 26) or higher in order to use full functions including streams.

2019年1月1日火曜日

新年にちなんでAndroidでカレンダーアプリを作る

みなさま、明けましておめでとうございます。

今年も、拙文ですが、ここに話題を書いて行こうと思っています。新年にちなんで、何か小さなアプリを作ってみたいと思います。カレンダーを眺めていたら、日にちの曜日が同一となっている月が幾つかあります。そこで、月初めの曜日をキーとして、月をグループ化するアプリを作ってみます。
【英語版はこちらにあります】

月初めの曜日が同じ月をグループ化する

実用性には乏しいミニアプリなのですが、以下の5つの「こだわり」を持って作ります。

【1】Android用に、アプリのロジックは、Javaで作る。
【2】Java8のラムダ式とストリーム、改訂日付クラスを用いる。
【3】GUIはApp Inventorで作り、そこからJavaを起動する。
【4】他のアプリにも適用できる汎用的な枠組みとする。
【5】ActivityStarterをカスタマイズして新規ブロックを作る。

まず、【1】と【2】ですが、最近のAndroidでは、Java8の機能が使えることが分かりました。そこで、ラムダ式とストリームを使って、できるだけ簡潔にこのアプリを書いたつもりです。そのロジック部は、以下の通りです。曜日や月の名前は英語ではなく、日本語にすることもできました。本質的な部分は、Mapオブジェクトmpへの代入文1つだけで済んでいます。このmpの要素は、 [曜日, 初日がその曜日である月のリスト]のペアとなります。


次に、【3】と【4】です。上記のようなJavaプログラムは、App InventorのActivity Starter機能(ブロック)を使って、App Inventorから呼び出すことができます。ここでも、それを使うのですが、できるだけ、汎用的な枠組みにするため、App Inventor側とJava側の両方のひな形を用意しました。Java側には、入力処理部と出力処理部を予め備えておき、上図のようなロジックの記述部だけを書き換えるだけで、ほぼ済むようにしました。また、App Inventor側も、下図のような、他の多くのアプリにも適用できるデザインにしました。

Javaプログラムを呼び出す汎用的なApp Inventorアプリ

最後の【5】ですが、上記の汎用性を考えたことにより、Activity Starterに対して、入力データやパラメータの受け渡しを多少変えたくなりました。そこで、それらに関するカスタマイズを行い、新たなブロックを作りました。以下がそれらのブロックです。Java側との入出力は、テキストファイルを介して行うことにしていますが、入力関しては、入力ファイルのパスを渡す場合と、テキストデータを直接渡す場合の2とおりを選べるようにしました。以下にあるSetupブロックの"isFile"でその選択ができます。

新規作成App Inventorブロック(Activity Starterのカスタマイズ版)
この新規ブロックを使って作ったApp Inventorのプログラムの全体は以下のとおりです。

新規作成ブロックを使ったApp Inventorプログラム

【注】AndroidのJava8対応について
Android 5.0(APIレベル22)以下では、Java7対応であり、Java8は使えません。Android 6.0(APIレベル23)ではラムダ式の一部などは使えますが、ストリームも含めたフル機能を使うには、Android 8.0(APIレベル26)以上が必要のようです。