2022年5月27日金曜日

Android StudioでMPAndroidChartを使えるようにする

 Android Studio Chipmunk(シマリス)が最近リリースされたので、

グラフを描画するライブラリ(MPAndroidChart)の取り込み方を調べた。

公式(https://github.com/PhilJay/MPAndroidChart)を見て最新バージョンを確認!

本当は公式のSetUpを見れば誰でも取り込めるようになってたら良かったんだけど

分かりにくかったので、まとめます。


2022年2月14日月曜日

String#splitで区切り文字を要素に入れる

java言語のString#splitを使い文字列を配列に分解する時、

引数で指定した文字列は配列から消えるが、要素内に入れたい場合がある。

例えば

String str = "{あいうえお}{かきくけこ}{さしすせそ}";
// これを{}を残したまま以下のように分解したい
String[] result = {
  "{あいうえお}",
  "{かきくけこ}",
  "{さしすせそ}"
};

正規表現の「先読み」「後読み」を使うことで対応できる。

2022年2月2日水曜日

ViewBindingで配置できるViewの上限は253個まで

 ViewBindingを使ってAndroid開発をしているとき、

大量にViewを作るとBindingでエラーなることがあった。


2021年12月31日金曜日

Menuの文字に色を付ける

 AndroidでToolbarを使いMenu表示させると、

以下のように文字が見えず空欄のMenuが表示されてしまう。



これはMenuの文字色が白、背景色も白であることが原因だった。

なのでMenuの文字色を調整する方法を記載する。

2021年11月14日日曜日

OutSystemsでTwitterのトレンドを取得する

OutSystemsでTwitterのトレンドを取得する方法をまとめておく。
トレンド取得APIの公式はここ

各ユーザ毎で行う方法と、アプリケーションとして行う方法の二種類あるが
今回はただトレンド情報を取得したいだけなので、ユーザ情報は使わず、
アプリケーションで行う方法をとる。

2021年10月26日火曜日

Firebaseのメールリンクでログインしてみる

 AndroidアプリでFirebaseを使ったメールリンク認証を作る際、
躓いたのでまとめておきます。


メールリンク認証とは

メールアドレスのみで認証する、パスワード設定が不要な認証方法です。

手順は
  1. アプリのユーザ登録画面からメールアドレスを入力してもらう
  2. Firebaseからそのメールアドレスに招待メールが送られる
  3. メールの招待RULをクリックすると、アプリが起動し認証完了(ユーザ登録完了)

メールを受け取れるということは本人である証明。というセキュリティの考え方。

2021年9月12日日曜日

ラズパイにPostgreSQLを入れてみる

 「OSS DB」という試験を受けようと思ったので、
公式で推していた「PostgreSQL」の環境構築をする。
環境はラズパイ3に作っていく。

2021年8月29日日曜日

「Redundant SAM - constructor」になった時の対処法

 Android開発をKotlin言語でしていると、ボタン押下などのリスナー設定をしているときに
「Redundant SAM - constructor」が出ることがあるので、意味と対処法を残しておく。
まぁ、
カーソルを合わせたときに出る「Remove redundant SAM - constructor」を押すか、Alt+Shift+Enterキーの同時押しをすれば、意味を理解していなくても自動でコードを修正してくれますけどね..


2021年8月22日日曜日

Activityの遷移時、すでに生成しているActivityへ遷移する方法

startActivityでActivityを切り替える場合、
デフォルトのままだと、毎回新規でActivityのインスタンスを作ってしまう。
なので、すでに同名Activityが存在していても、新しいActivityがスタック上にできあがる。


Intentで指定したActivityがすでに生成されている場合は、
生成済みActivityに遷移させるにはどうするか。について書いていく。

2021年8月11日水曜日

Ktolinで「Parameter 'xxx' is never used」というワーニングが出たときの対応

KtolinでAndroid開発をしているとき、 
ListViewなど押下イベント設定(setOnItemClickListener)をラムダで書くと以下のようになる。
// 押下したリストのアイテム情報をトースト表示する
listView.setOnItemClickListener { parent, view, position, id ->
    Toast.makeText(this, array[position], Toast.LENGTH_SHORT).show()
}
こうすると、
パラメータ4つあるけど positionしか使っておらず、残りの引数に波線が表示される。


ワーニングメッセージを見るとそれぞれ
Parameter 'parent' is never used, could be renamed to _
Parameter 'view' is never used, could be renamed to _
Parameter 'id' is never used, could be renamed to _

となっている。

2021年8月8日日曜日

AndroidStudioでビルドしようとするとエラーでできない

Android Studio Arctic Foxにアップデートして、
新しいプロジェクト立ち上げてビルドすると以下のエラーになった。

Build file 'E:\Document\MyApplication11\app\build.gradle' line: 2

An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.


訳: 
Android Gradleプラグインを実行するには、Java11が必要です。
現在、Java1.8を使用しています。

つまり、
JDKのバージョンを11にすればよいということだ。

2021年6月15日火曜日

jcenter()が非推奨の為、AndroidStudioで新規作成するとエラー

AndroidStudioで新規プロジェクトを作成すると、以下のエラーが出てビルドできなくなったので備忘録
Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other Maven repositories.
This repository is deprecated and it will be shut down in the future.
See http://developer.android.com/r/tools/jcenter-end-of-service for more information.
Currently detected usages in: root project 'My Application', project ':app'
Affected Modules: app

どうやら「jcenter()が非推奨になって、じきに完全消滅するから、ビルドスクリプトから消してくれ」とのこと

AndroidStudio4.2でKotlinで新規プロジェクト作成するとエラーになる

以前にも紹介したが、また同類のエラーが発生したので備忘録
新規プロジェクトをKotlinで作成すると、以下のエラーが出てビルドできなくなった。
A problem occurred configuring root project 'My Application'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10-release-894.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.10-release-894/kotlin-gradle-plugin-1.5.10-release-894.pom
       - https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.10-release-894/kotlin-gradle-plugin-1.5.10-release-894.pom
     Required by:
         project :

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

どうやらkotlinのプラグイン「1.5.10-release-894」が見つからないらしい。。

2021年6月9日水曜日

KotlinでListの参照は[]を使うべきかgetを使うべきか

KotlinでListの参照を行うには以下二種類の方法が存在する
  • []のインデックス演算子で指定する方法
  • get()のメソッドで指定する方法

どちらを使うのが適切なのか調べてみると
言語仕様で[]を使う参照は、getに置き換えてくれてるようだ。

わざわざ[]で参照をできるように言語仕様側で調整していることになる。
ということは
言語開発者の意図として、[]を使ってほしいということ。

以上から、

Kotlin言語では[]で参照することが正しい

2021年5月11日火曜日

AndroidStudio4.2でKotlinが使えなくなった件

 AndroidStudio4.2がリリースされたのでアップデートしてみたら

新規プロジェクトをKotlinで作成すると、以下のエラーが出てビルドできなくなった。

A problem occurred configuring root project 'My Application'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0-release-764.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.0-release-764/kotlin-gradle-plugin-1.5.0-release-764.pom
       - https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.0-release-764/kotlin-gradle-plugin-1.5.0-release-764.pom
     Required by:
         project :

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

どうやらkotlinのプラグイン「1.5.0-release-764」が見つからないらしい。。

2021年4月11日日曜日

DialogFragmentは別ファイルに分けないとクラッシュする

前回、Fragmentはpublicクラスで定義しないと落ちる話をした。
当然、DialogFragmentもFragmentを継承しているので、この制約を受ける。。
よって、ファイルを分けてpublicクラスでDialogFragmentも定義しなくてはクラッシュすることになる。

しかし、ダイアログなんかは軽く表示させたいし、
わざわざクラスファイルを分けて記述していくのが面倒くさい。。
なので、ファイルを分けなくてすむ方法について記載する。

FragmentはPublic以外でクラス定義すると落ちる

Fragmentをpublic以外のアクセス修飾子でクラス定義していると、FragmentManagerに追加する際、以下のIllegalStateExceptionが投げられクラッシュする。

Caused by: java.lang.IllegalStateException: Fragment com.sample.test.myapplication.MainActivity.MyDialogFragment must be a public static class to be  properly recreated from instance state.
   at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:165)
   at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:179)
   at androidx.fragment.app.FragmentTransaction.add(FragmentTransaction.java:125)
   at androidx.fragment.app.DialogFragment.show(DialogFragment.java:154)
   at com.sample.test.myapplication.MainActivity.onClick(MainActivity.java:32)
このエラーが出たときの対処法は、
Fragmentを別のファイルに定義し、publicクラスにすればよい。
本記事では、何故このような制約があるのかについて書いていく。

2021年4月10日土曜日

Buttonの上にViewを載せる

AndroidアプリでButtonの上にViewやFragmentを乗せても、

Buttonが表示物に隠れず、ボタンが上に重なって表示される。

解決方法として、ボタンの属性「stateListAnimator」に「"@null"」を設定すれば良い。

例えば、ボタンの上に画像(ImageView)を配置したいときは以下のようにする。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:stateListAnimator="@null" ★ コレ! ★
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

2021年4月1日木曜日

GitHubで公開されているプロジェクトの取り込み

 Androidは公式がGitHubにサンプルプロジェクトを公開している。

これらのサンプルコードを取り込んでビルドしたいときの方法を残しておく。

※GitHubからソースコードを落としたいだけなら、手順2までで良い