suer TIL
eye catch
【Android,Kotlin】enable/disableの状態やpressした状態のときにUIの色を変える
状態で色を変える AndroidのViewはenable/disableの状態や、pressした状態で色や形などを変更できる。 例としてテキストに対して以下のように作成できる。
ANDROID
2022-08-31 2023-03-31
eye catch
複数回のコマンドの標準出力をまとめて一度にリダイレクトする
複数回のコマンドを同じファイルにリダイレクト 以下のようなシェルスクリプトを書いた場合、同じファイルをコマンドの回数だけオープンすることになり 無駄が生じる。 #!/bin/sh echo foo >> a.txt echo bar >> a.txt echo baz >> a.txt shellcheckにかけると以下の違反が検出される。
LINUX
2022-08-28 2022-08-28
eye catch
【Android,Kotlin】端末に保存されたファイルかカメラ撮影から画像選択できるようにする
ファイル選択orカメラ撮影で画像を選択できるようにしたい こういうやつ。 カメラで撮った写真を使う場合と端末のファイルを利用する場合でそれぞれ単体の実装は以下の記事を参照。 カメラ: 端末で写真を撮って表示する ファイル選択: GetContentで端末に保存された画像ファイルを選択して表示する OpenDocumentで端末内の画像ファイルを選択して表示する 実装 自作ActivityResultContract ボトムシートとしてファイル選択、カメラ撮影から選択できるようにするためのActivityResultContractを作成する。
ANDROID
2022-08-27 2025-05-20
eye catch
【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。 関連: 【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.OpenDocument()のlauncherを作る。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】端末で写真を撮って表示する
アプリのイメージ カメラアプリで写真を撮って表示したい。 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> cacheフォルダへの書き込みを許可する 外部のカメラアプリがアプリのcacheフォルダへ書き込み、アプリはそれを画面に表示する。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。 関連: 【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.GetContent()のlauncherを作る。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】Kotlinらしいダウンキャスト
ダウンキャストが必要となる例 open class Super {} class A : Super() { fun f() {} } val x: Super = A() x.f() // x は Super なので f メソッドがわからずエラー: if+is演算子で分岐する Kotlinでは実行箇所の文脈によって自動的に推論されてダウンキャストされる。 例えば上記例では if (x is A) でチェックすると、そのなかではxはAのインスタンスであると判断されるので Aクラスのインスタンスメソッドf()が呼べるようになる。
ANDROID
2022-08-22 2023-03-31
eye catch
【Android,Kotlin】KtlintでKotlinコードのチェック
Ktlint KtlintはKotlin用のlinter。 本記事ではビルド設定(Gradle)に組み込んで使用する。 app/build.gradleに設定を追加する configurations { ktlint } dependencies { ... // バージョンは本家を確認すること https://github.com/pinterest/ktlint ktlint "com.pinterest:ktlint:0.48.2" } // lintタスクを追加 task ktlint(type: JavaExec, group: "verification") { description = "Check Kotlin code style." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "src/**/*.kt" } check.dependsOn ktlint // フォーマットタスクを追加 task ktlintFormat(type: JavaExec, group: "formatting") { description = "Fix Kotlin code style deviations." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "-F", "src/**/*.kt" } argsに指定しているパスはプロジェクトの環境に合わせる。
ANDROID
2022-08-21 2023-03-31
eye catch
【HUGO】シンタックスハイライトのスタイルを変更する
デフォルトのハイライトが視認性が悪い hugoで作られた当サイトのソースコードハイライトでコントラスト比の悪いところがあり、 見づらい箇所がところどころあるのでスタイルを変更したい。
HUGO
2022-08-21 2022-08-21
eye catch
【Java】Files.list()の戻り値Stream<Path>はclose()が必要
java.nio.Files.list(Path)でリストアップしたらクローズしないといけない java.nio.Files.list(Path)で指定したディレクトリ直下のファイルのパス一覧をStream<Path>型で取得できる Stream<Path> paths = Files.list(Paths.get("/path/to/directory")); このメソッドの戻り値をcloseしないとToo many open filesでエラーが発生するようになる。 java.nio.Files.list(Path)の戻り値をcloseする Stream<T>インタフェースの親インタフェースBaseStream<T>は、さらに親がAutoClosableインタフェースなので try-with-resoucesで確実に閉じることができる。
JAVA
2022-08-21 2022-08-21
  • ««
  • «
  • 23
  • 24
  • 25
  • 26
  • 27
  • »
  • »»
AUTHOR
author image
suer
ふつうのプログラマ
LATESTS
eye catch
【tig】カスタムキーバインドを追加してGit操作を効率化する
LINUX
2025-06-18 2025-06-18
eye catch
【Shell Script】グループコマンドで複数コマンド出力をまとめてリダイレクト
LINUX
2025-06-17 2025-06-17
eye catch
rsync -a コマンドでも owner と group を実行ユーザーにする
LINUX
2025-06-15 2025-06-15
eye catch
【Shell Script】エラーで途中で止まっても実行される後処理を設定する
LINUX
2025-06-10 2025-06-15
eye catch
sedコマンドで指定した行に文字列を挿入・削除する
LINUX
2025-06-06 2025-06-06
  • ホーム
  • お問い合わせ
  • プライバシーポリシー

© 2022 suer