カテゴリー: Android

『Android Kotlin Fundamentals』をやった(4)

Android Kotlin Fundamentals: Lifecycles and logging

3. Explore the lifecycle methods and add basic logging

Step 1: Examine the onCreate() method and add logging

項番6が改行されていない。


4. Use Timber for logging

Step 2: Create an Application class and initialize Timber

Warning: It might be tempting to add your own code to the Application class, because the class is created before all of your activities and can hold global state. But just as it's error-prone to make readable and writable static variables that are globally available, it's easy to abuse the Application class. Avoid putting any activity code in the Application class unless the code is really needed.

ふーむ、Applicationクラスには余計なコードを書かないのがお作法なのね(´・ω・`)


5. Explore lifecycle use cases

Use case 3: Partially hide the activity

Whatever code runs in onPause() blocks other things from displaying, so keep the code in onPause() lightweight.

onPause()内は軽めに…φ(・ω・` )メモメモ…


6. Explore the fragment lifecycle

Lifecycles and loggingの後は次のComplex Lifecycle Situationsへのリンクがない?


Android Kotlin Fundamentals: Complex Lifecycle Situations

4. Task: Use the Android lifecycle library

Step 1: Turn DessertTimer into a LifecycleObserver

LifecycleOwnerをimplementしてるのはFragmentActivityのsuperclassのComponentActivityではなかろうか…


5. Task: Simulate app shutdown and use onSaveInstanceState()

Step 1: Use adb to simulate a process shutdown

The Android Debug Bridge (adb) is a command-line tool that lets you send instructions to emulators and devices attached to your computer.

Android Debug Bridge(adb)はコンピュータに接続されたデバイスまたはエミュレータへ指示を送るコマンドラインツールと…φ(・ω・` )メモメモ…

4.Type adb and press Return.
今も向こうではエンターじゃなくてリターンキー呼びが主流なんだろうか。

んー、
adb shell am kill com.example.android.dessertclicker
が上手く動いてないのか、Recents Screenから復帰(?)させても、前の状態が残ってるな…
何か間違ってるな(ノ∀`)

adb server version (41) doesn't match this client (40); killing...
could not read ok from ADB Server
* failed to start daemon
error: cannot connect to daemon

って出てるけど、これは問題あるのかないのかがまずわからん(・∀・)

コマンドプロンプトで上のコマンドを放ったら、何のエラーメッセージも出さずに終了出来た模様。Recents Screenから復帰させたら、無事に前の状態がリセットされていた。

なんでASだと上手く行かないんだろうと悩んだが、一回コマンドプロント側で上手く行ったら、ASでも上手く行くようになった。原因はよくわからないが、取り敢えず解決したからいいか(ノ∀`)
adb.exeが複数起動してたんかな?

Step 2: Use onSaveInstanceState() to save bundle data

A bundle is a collection of key-value pairs, where the keys are always strings.

前にも説明があったが、キーは必ず文字列なんだねぇ。

Generally you should store far less than 100k, otherwise you risk crashing your app with the TransactionTooLargeException error.

サイズは100KB以下に抑えないと駄目と…φ(・ω・` )メモメモ…

The putInt() method (and similar methods from the Bundle class like putFloat() and putString() takes two arguments:
putString()の後に閉じ括弧が抜けてる?

Step 3: Use onCreate() to restore bundle data

So if the bundle is not null, you know you're "re-creating" the activity from a previously known point.

それで判定が出来るわけか。

which in turn
which in turnって何?非制限用法(付加用法)って?
「which in turn,」の訳し方について 下記の文章で「which in turn,」はどう訳せば良いでし…


ようやっとここまででLesson4が終わった…(ヽ'ω`)
勉強になるけど長いなw

『Android Kotlin Fundamentals』をやった(3)

Android Kotlin Fundamentals 02.4:Data binding basics

an object that contains a reference to each view. This object, called a Binding object,

Bindingオブジェクトとは各Viewへの参照を包含するオブジェクト…φ(・ω・` )メモメモ…

To use data binding, you need to enable data binding in your Gradle file, as it's not enabled by default.
To work with data binding, you need to wrap your XML layout with a <layout> tag.
The namespace declarations for a layout must be in the outermost tag.

build.gradle(:app)でdatabindingを有効にして、
<layout>タグで全体を囲って、xmlnsを最外である<layout>タグ内に移動。

あれ…
なんかアプリが繰り返し停止するとか言ってエラーが(ノ∀`)
kaptを追加していないのがいけないかと思い、pluginのところに

id 'kotlin-kapt'

を追加したが、関係なかった模様。

    buildFeatures {
        dataBinding true
    }

ではなく、

    dataBinding {
        enabled true
    }

にしてみたけど、
別に関係なく…まあそれはそうかw

あー、中身は空でもいいけど<data></data>を追加しないと駄目ってことなのかな?
<layout>タグで囲んだ後は<data>タグも一緒に追加しないと起動でこけるみたいだな、少なからず、うちの今の環境だと。

おお、エラーになっていたMainActivity.ktの
import com.example.aboutme.databinding.ActivityMainBinding
が自動追加された。サンプルと違って、うちのコードのパッケージにandroidがないな…
まあそこはどうでもいいかw

import androidx.databinding.DataBindingUtil
を追加するとエラーになって
import android.databinding.DataBindingUtilが追加されるな…
jetpackから移動したんだろうか(´・ω・`)?

Step 4: Use the binding object to replace all calls to findViewById()

When the binding object is created, the compiler generates the names of the views in the binding object from the IDs of the views in the layout, converting them to camel case. So, for example, done_button is doneButton in the binding object, nickname_edit becomes becomes nicknameEdit, and nickname_text becomes nicknameText.

ほーん、なるほど…( ゜σ・゚)ホジホジ
ってbecomesが二回出てきてるなw

findViewById(id)で指定してたら、自動的にbindingオブジェクトを追加するか、同じメソッド内にあるbindingオブジェクトを選択して、~.キャメルケース化したidとかに変更する機能はないんかな。それは一括でリファクタリング出来る機能として存在した方がいいのかな…

完成形の方にupdateNicknameがないな…
いずれ削除するのかな?

thorough 徹底的に 充分な, 十分な, 念入りな
一瞬、throughかと思ったら、こんな英単語があるんだな…


4. Task: Use data binding to display data

Step 2: Add data to the layout

この段階でタグを挿入するのか…
他の環境だと上記のエラーは起きないのであろうか?
何はともあれThis is where you will connect the view with the data.ということらしい。


Android Kotlin Fundamentals:Create a fragment

2. App overview

The top of the app displays a view called the *app bar (*or action bar) which shows the app's name.

この*はなんだろうか…
もしかして強調表示とかの制御文字の一部?


4. Task: Add a fragment

Step 1: Add a Fragment class

Create a binding object

これ項番5でplaceholder_layout.xmlを削除するけど、これってなんなんだろう?

Step 2: Add the new fragment to the main layout file

今まで、ずっとtext tabと言ってたのに、ここでようやくCode tabという表記になった。


Android Kotlin Fundamentals:03.2 Define navigation paths

5. Task: Add fragments to the navigation graph

Step 1: Add two fragments to the navigation graph and connect them with an action

placeholder_layout.xmlを削除し忘れていた為かなんか知らないけど、titleFragmentが出てこなくて悩んだ(ノ∀`)

Step 2: Add a click handler to the Play button

最後のopens.と画像の間に改行が欲しいところか。


7. Task: Change the Back button's destination

Step 1: Set the pop behavior for the navigation actions

あれ? popUpTo属性がよくわからないと思ってぐぐったら、
コードラボを翻訳しているサイトを見つけた(ノ∀`)

まあしばらくは原文サイトで読み続けようかな。

項番4の
Select the action for navigating from the gameFragment to the gameWonFragment. Again, set popUpTo to gameFragment in the Attributes pane and select the popUpToInclusive checkbox.
ってあるけど、画像だとgameWonFragmentになっているような気がするけど、これはなんなんだろうか…

Step 2: Add more navigation actions and add onClick handlers

val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this,navController)

はbindingの後に書かないとまたエラーが発生して起動しなくなった。

Step 2: Add the options-menu resource

追加したmenu itemにはidが入っていないのがデフォ?
取り敢えずaboutFragmentにしたけれど。


10. Task: Add the navigation drawer

Step 3: Create the drawer menu and the drawer layout

Note: If you use the same ID for the menu item as for the destination Fragment, you don't need to write any code at all to implement the onClick listener!

へぇ(・∀・)
随分と簡略化されてるんだなぁ。


Android Kotlin Fundamentals: 03.3 Start an external Activity

3. Task: Set up and use the Safe Args plugin

An Android Bundle is a key-value store.

Bundleはキーバリューペアを格納するオブジェクトと…φ(・ω・` )メモメモ…

Android's Navigation Architecture Component includes a feature called Safe Args. Safe Args is a Gradle plugin that generates code and classes that help detect errors at compile-time that might not otherwise be surfaced until the app runs.

Safe Argsはアプリ実行時まで表面化しないかもしれないエラーをコンパイル時に発見する手助けをするコードとクラスを生成するGradleプラグイン?

Step 3: Add a NavDirection class to the game Fragment

GameFragmentDirectionsというか~Directions.javaファイルってASから見られるのかな?
なんか見えない気がする。エクスプローラで開くと表示されるんだけども…
\android-kotlin-fundamentals-starter-apps-master\AndroidTrivia-Starter\app\build\generated\source\navigation-args\debug\com\example\android\navigation

import androidx.annotation.NonNull;
import androidx.navigation.ActionOnlyNavDirections;
import androidx.navigation.NavDirections;

public class GameFragmentDirections {
  private GameFragmentDirections() {
  }

  @NonNull
  public static NavDirections actionGameFragmentToGameOverFragment() {
    return new ActionOnlyNavDirections(R.id.action_gameFragment_to_gameOverFragment);
  }

  @NonNull
  public static NavDirections actionGameFragmentToGameWonFragment() {
    return new ActionOnlyNavDirections(R.id.action_gameFragment_to_gameWonFragment);
  }
}

NavDirectionsはインターフェースなのか。

ActionOnlyNavDirectionsはその継承クラスでnavの矢印のidを保持して、getArguments()は常に新しいBundleを返す…?

Android の各種リソースの ID は int 型で管理されています。 この値を変数に格納したり、メソッドのパラメータとして受け取るように実装した場合、不正な int 値が渡されて実行時に落ちてしまうリスクがあります。

@IdRes R.id.xxx で参照するリソースID

へぇ(・∀・)

4. Task: Add and pass arguments

Step 2: Pass the arguments

ここでnumQuestionsとquestionIndexをパラメータとして追加したら何故か変な補完がされてエラーに。余計なvarやらany以降を削除したら、エラーは消えた。

『Android Kotlin Fundamentals』をやった(2)

Android Kotlin Fundamentals: Add user interactivity

3.Task: Add an EditText for text input

This the same code you finished in a previous codelab.
be動詞ってなくてもええんかな?

Step 2: Add an EditText

In the Palette pane, notice how the icon for TextView shows the letters Ab with no underscoring. The EditText icons, however, show Ab underscored. The underscoring indicates that the view is editable.

下線の有無はそういう違いだったのか(・∀・)シランカッタ


5. Task: Add a button and style it

Step 1: Add a DONE button

またcolorAccentがcolors.xmlにないな。
これは今はない設定なのか?

と思ったが、themes.xmlに
@color/teal_200
という指定があったので"teal_200"を"colorAccent"に変えたら変更された模様。
関係ないがstyles.xmlで指定してもthemes.xmlで指定されていたら、こっちが優先されるんだろうか?


6. Task: Add a TextView to display the nickname

Step 2: Change the visibility of the TextView

何故か、指定してもテキスト(コード)の方で以下の2つが出て来ないな…なんだろうか。

style="@style/NameStyle"
android:text=""

7. Task: Add a click listener to the DONE button

Step 1: Add a click listener

5.Hide the nickname EditText view by setting the visibility property of editText to View.GONE.

なんか強調表示の範囲がおかしいというか、EditTextが強調表示されているが故に文が読みにくくなっているような。


Android Kotlin Fundamentals:02.3 ConstraintLayout using the Layout Editor

4. Task: Use Layout Editor to build a ConstraintLayout

Step 1: Set up your Android Studio work area

日本語化してるからAutoconnect toggle buttonがどれかわからない…
Enable Autoconnection to Parentでいいのかな?

と思ったら下でアイコンが表示されてた。
名前は違うけど、これでいいのか。

default marginが何故かGUIで設定できない。
数値を押しに行くとドロワー(?)パネルが消えちゃう…
うちの環境がおかしいのかな?

bias slidersが見当たらないΣ(・ω・`≡´・ω・)
と思ったら、Constraint Widgetのとこにあったわ(ノ∀`) ← view inspectorが正式名称
ダーク設定にしていたからか、ボケてきているからか、見落としていたわ。

Step 2: Add margins for the Hello World text view

なぜかTextViewに左右のmarginをセットしてもキャンセルされてしまう…
上下には設定出来るのに…なぜだ(´・ω・`)?

あれ? 既存の制約を消して(黒丸クリック)からもう一回 +したら設定できるようになった。

Step 3: Adjust constraints and margins for the text view

In the view inspector, click the Delete Bottom Constraint dot 8b8e6d915b2b4a9c.pngon the square to delete the bottom constraint.

ここで間に入ってる画像、上下逆じゃない(´・ω・`)?
これだとBottomじゃなくてTopの方じゃない?


5. Task: Style the TextView

Step 1: Add a font

ここも"Add font to project"の手順が後だな…

Step 2: Add a style

styles.xmlはなくてthemes.xmlしかないな
Android Studio4.1辺りから変更になったのか…

このような色の実際の定義は、テーマ内で提供されています。デバイスが夜間モードになると、アプリは「ライト」テーマから「ダークテーマ」テーマに切り替えることができます。またリソース名の値も変更できます。スタイルは特定の色の定義ではなくセマンティック名を使用しているため、スタイルを変更する必要はありません。
themes

ってあるけど、実際の色の設定でcolors.xmlとかでやってんじゃね(´・ω・`)?

「ライト」テーマと「ダーク」テーマの切り替え用のスタイルセットのように思えるけど、そういうことではないのか…

どうでもいいが"「ダークテーマ」テーマ"になってるな…
原文だとyour app can switch from its "light" theme to its "dark" themeだけども、翻訳AIがおかしいんかな( ゜σ・゚)ホジホジ

テーマの方に書いても上手く適用されなかったので、styles.xmlを新規に作ってそこに値をペーストしたら適用された。

あ(ノ∀`)
styleに指定しようとして蹴られてるだけだったw
android:themeの方に指定したら上手く行ったわ。


6. Task: Add a second TextView and add constraints

Step 1: Add a new text view

constraint handleは緑にならずに青丸が表示されるだけかな。

Step 3: Set attributes for the new text view

属性ペインだとlayout_widthの方が上に来てるな。
まあいいんだけどもw


10. Task: Add a baseline constraint

Baseline constraint

Edit Baselineアイコンが出て来ない。
ああ、ベースラインアイコンとかサンプルデータのセットは右クリックすればコンテキストメニューで出てくるんだな。


11. Task: Add a chain of buttons

Step 2: Create a horizontal chain and constrain it

16dpを@dimen/margin_wideに差し替えたり、visibilityを設定したりしてないな…
なぜかgreen_buttonのandroid:layout_marginEndだけ16dpのままだなw


12. Task: Add click handlers to the buttons

項番4のview's background Add
ピリオド抜け?

whenのとこでR.id.box_~_textと指定してるけど、これでアクセス出来るのか?
取り敢えずfindViewById(R.id.box_~_text)に差し替えたけども。

『Android Kotlin Fundamentals』をやった(1)

Android Kotlin の基礎のための Codelab

と言いつつ、まだ途中(ノ∀`)
まだLesson4までしか終わってない。

『Welcome to Android Basics in Kotlin』と異なり、英文で内容もちょっと長いかな? あと若干前の内容っぽい。英文なので読むのに時間がかかるが、知りたいことというか途中で浮かんでくる疑問を即座に説明してくれてたりするので、読む価値はあると思う。

途中でこの内容(というかcodelab全般?)翻訳・公開しているサイトを発見したが、英文で読む方に慣れてしまったがためにそのまま英文で読み進めることにした。

最近、英文を全く読まなくなっているので少しは読まないといけない気がするし…

いつものように、やっている最中の適当なメモ書きや感想であり、勘違い、おま環由来の問題等々で大騒ぎしている記録である(ノ∀`)


thus far これまでに 今までに
terminology 用語
unambiguous 明確な
complicate 複雑にする, わかりにくくする
sheer 全く, 単なる 形容詞 純然たる, 全くの
the sheer number of の膨大な数
inevitably 必然的に
radically 根本的に
underneath 下に

なんでView groupでchild viewのhorizontal positionを一括指定出来ないんだろうか?

All the view IDs in your XML file must have this prefix..
ピリオドが一つ余計。

Use the findViewById() method to get a View reference for the view that you defined in the XML class.
この場合のclassって何を指してるんだろうか?
fileじゃいけないのか?

the duration to show the message.
先頭が大文字になっていない。

The show() method at the end displays the toast.
は<li>というか<ul>外に書くべき?


Android Kotlin Fundamentals 01.3: Image resources and compatibility

Step 1: Add the images

項番3と4が2から分かれていない。
previewタブはない?

performantなんて単語あるんだな…
と思ったが、よくよく考えてみると、importancetとimportantの語形変化と一緒だな(ノ∀`) performanceの形容詞形か。


性能[行為・パフォーマンス]の
〈俗〉《コ》高性能の、効率の良い

store the View object in a field.
Kotlinにはフィールドはないと言ってたけど、それは意味が違うのかな(´・ω・`)?


5. Task: Use a default image

項番4が3から分かれていない。

The tools namespace is used when you want to define placeholder content that is only used in the preview or the design editor in Android Studio. Attributes using the tools namespace are removed when you compile the app.

ふーむ、ふーむ(・∀・)
Tools名前空間をつけた値は開発時のプレースホルダーでコンパイル時には除去されるんだな。

Namespaces are used to help resolve ambiguity when referring to attributes that have the same name.

名前空間自体は名前衝突というかコンパイラが名前で迷わないためのプレフィックスってことでええんかな?


6. Task: Understand API levels and compatibility

Step 1: Explore API levels

項番1と2が分かれていない。

Step 3: Add compatibility for vector drawables

項番1と2が分かれていない。

The app namespace is for attributes that come from either your custom code or from libraries and not the core Android framework.

なるほど、app属性は自分のカスタムコードや標準(?)のAndroidフレームワーク以外のライブラリコード由来の属性の為のものなのか。


9.Summary

Summaryのところでもっと綺麗にまとめてあった(ノ∀`)

The tools namespace for design-time attributes:
Use the tools namespace in the Android layout file to create placeholder content or hints for layout in Android Studio. Data declared by tools attributes is not used in the final app.

The app namespace:
The app namespace in your XML layout file is for attributes that come from either your custom code or from libraries, not from the core Android framework.


Android Kotlin Fundamentals 01.4: Learn to help yourself

3. Task: Use project templates

最初間違えてempty Activityでプロジェクトを作ってしまい、一瞬戸惑う(ノ∀`)
次にBasicで作ったつもりだったが、なんか出来上がった画面が違うような…

フラグメントとボタンが使われていて、TextViewの文言もなんか違う。
バージョンによる違いかな?まあ大した問題でもないからいいか。

color.xmlにcolorAccentがない?


4. Task: Learn from sample code

dagger.hilt.android.AndroidEntryPoint
なんだこのアノテーション…

Hilt は Android 用の依存関係インジェクション ライブラリです。これを使うことで、プロジェクトで依存関係の注入(DI)を手動で行うためのボイラープレートが減ります。手動で依存関係の注入を行うには、すべてのクラスとその依存関係を手作業で作成し、コンテナを使用して依存関係の再利用と管理を行う必要があります。

Hilt は、プロジェクト内のすべての Android クラスにコンテナを提供し、そのライフサイクルを自動で管理することで、アプリケーションで DI を行うための標準的な方法を提供します。Hilt は、よく知られた DI ライブラリである Dagger の上に構築されているため、コンパイル時の正確性、実行時のパフォーマンス、スケーラビリティ、Android Studio のサポートといった Dagger の恩恵を受けられます。詳細については、Hilt と Dagger をご覧ください。Hilt

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android.

ふーん(´・ω・`)
この場合の依存関係の注入って@AndroidEntryPointでGardenActivityに各Fragmentを結びつけてるのか…?
と思いきや、

@AndroidEntryPoint は、プロジェクト内の Android クラスごとに個別の Hilt コンポーネントを生成します。これらのコンポーネントは、コンポーネント階層で説明されているように、それぞれの親クラスから依存関係を受け取ることができます。

依存関係を注入したいコンポーネント全てに引っ付けるのかな?
まあ取り敢えずはこのくらいの理解でいいかw

Changing the launcher icon
項番4,5が改行されていない

日本語化してるから、その差分かもしれないがLegacyタブはなくてオプションタブだった。そもそもAPIバージョンをほぼ最新にしてるからその影響かもしれないがよくわからないw


Android Kotlin Fundamentals: LinearLayout using the Layout Editor

3. Task: Create the AboutMe Project

新規プロジェクト作成ダイアログに"This project will support instant apps"という項目がないな。


5. Task: Add a TextView using Layout Editor

Layout Editorの表示属性切り替えのアイコンってどれだ…
一応下までスクロールすると全属性が表示されるからまあいいか。

項番3,6,7が改行されていない。

Attributeでは大文字の"ID"だけどLayoutEditorだと"id"だな。
textColorの変更がよくわからなかったので@color/blackをGUIから選択した。
→後でstyles.xmlを直接編集したけど、GUIから指定する方法がわからなかったな(ノ∀`)


6. Task: Style your TextView

「Right/left versus start/end」の

Add "start" and end" in addition to "left" and "right".
For example, use both android:paddingLeft and android:paddingStart.

で一瞬悩む。これは"left"や"right"に"start"や"end"を追加するっていうんじゃなくて、"left"や"right"に加えて"start"や"end"も使えるよって話なのかな?

項番7の改行がない。

Step 3: Add a font

robotoフォントの追加手順は3,4を変えるべきでは?
画面配置が逆なので、非常に微妙ながらも逆流手順に思える。

A style is a collection of attributes that specify the appearance and format for a view.

スタイルはビューの外観やフォーマットを指定する属性のコレクションであると…φ(・ω・` )メモメモ…


7. Task: Add an ImageView

6.To rename the id of the ImageView, right-click on "@+id/imageView" and select Refactor > Rename.

の通りにするとThe resource name must start with a letterというエラーが出てリファクタリング出来ない。"@+id"は自動補完されるのか、除去して残りの"star_image"だけ入力すればいいのかな。

項番10でOKは二回クリックする必要があるか?

星マーク(アスタリスク?)2つは何を意味してるんだっけか…