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

2022年5月27日金曜日

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

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

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

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

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

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


2022年2月2日水曜日

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

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

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


2021年12月31日金曜日

Menuの文字に色を付ける

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

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



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

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

2021年10月26日火曜日

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

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


メールリンク認証とは

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

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

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

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年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までで良い

2021年3月25日木曜日

ListViewに1件ずつ追加する方法

Androidでのリスト表示はListViewを使用することが多い。
何かを契機に1件ずつレコードを追加していく際によく以下のことが起こる
  • Listには追加できているのに見栄えに反映されない
  • 更新すると表示位置が先頭行に戻ってしまう
なので、
ボタンを押下を契機に一件ずつ要素を追加していき、
表示位置も維持するコードのサンプルを載せておく

2021年3月13日土曜日

SharedPreferences#StringSetの挙動について

 SharedPreferencesでStringSetを用いて追加保存する際、以下が起こることがある
  • 1件しか保存されない
  • 保存してないのに勝手にデータが保存されている

結論を先に書くと、
SharedPreferencesからgetStringSetする際、シャローコピーすれば解決するが
 // java版
 Set<String> set = new HashSet<>(
   getSharedPreferences("set",MODE_PRIVATE).getStringSet("data", new HashSet<>()));

 // Kotlin版
 val set = getSharedPreferences("set", MODE_PRIVATE)
   .getStringSet("data", mutableSetOf())?.toMutableSet()
どういう理屈なのかをもう少し詳しくまとめる。



2021年3月7日日曜日

AndroidでkHttpを使ってみる

 Androidのネットワーク通信で有名な外部ライブラリといえば「OkHttp」「Retrofit」だろう。
なのであえて、「kHttp」というライブラリを使った通信を行ってみた。


kHttpとは

公式はここ
Pythonでスクレイピングをするときに使う「requestsモジュール」をKotlinで使うようにしたもの。Android以外でも使えるHTTPリクエストできるシンプルなライブラリだ。

2021年3月4日木曜日

findViewByIdとrequireViewByIdの違い

Androidでレイアウトファイルに定義したViewのインスタンスを探すのに
findViewByIdを使ったことある人は多いと思う。
ViewのAPI仕様を見てたら、requireViewByIdという似たようなメソッドを発見したので、違いを書き残しておく。

2021年2月28日日曜日

Xml declaration should precede all document contentのエラーになった時の対応

 Androidのレイアウトのcodeを書くとき、
「Xml declaration should precede all document content」というエラーになった
当然このまま実行してもAAPTでビルド失敗
MyApplication\app\src\main\res\layout\activity_main.xml:1: AAPT: error: XML or text declaration not at start of entity.
なので対応方法を書く。