LoginSignup
41

More than 5 years have passed since last update.

Android開発で繰り返しググちゃうこと(kotlin編)

Last updated at Posted at 2017-12-10

Android開発をしていて、よく忘れて調べることを追加していきます。

Actionbarのタイトルを変更する

supportActionBar?.title = ""

Actionbarに戻るボタンを表示する

onCreateなどに下記を追加

supportActionBar?.setDisplayHomeAsUpEnabled(true)

下記リスナーをオーバーライド

override fun onSupportNavigateUp(): Boolean {
    finish()
    return super.onSupportNavigateUp()
}

トップの階層では表示させたくない場合は下記で判定できる

// バックスタックにFragmentがある場合のみ戻るボタンを表示する
val canback = supportFragmentManager.backStackEntryCount > 0
supportActionBar?.setDisplayHomeAsUpEnabled(canback)

画面を縦固定にする

Activityの定義にandroid:screenOrientationを指定する

AndroidManifest.xml
<activity
    android:name=".Presentation.Activity.HogeActivity"
    android:screenOrientation="portrait" />

数字をカンマ区切で表示したい

// 2,000と表示したい
"%,d".format(2000)

TextViewに打ち消し線を入れる

val textView = findViewById(R.id.text_view)
val paint = textView.paint
paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG
// 線がギザギザにならないようにアンチエイリアスを有効にする
paint?.isAntiAlias = true

1行でif/elseを書く

val nextPage = if (page == 0) 1 else page + 1

ImageAssetsを使用したアイコン生成方法

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41