2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android Kotlin】AlertDialogの文字色をカスタマイズする

Last updated at Posted at 2022-09-25

環境メモ
⭐️macOS Monterey
⭐️Android Studio Chipmunk 2021.2.1 Patch 2

↓↓完成内容
スクリーンショット 2022-09-25 19.59.23.png

themesで色を設定する

buttonBarPositiveButtonStyleはPositiveButtonの文字色を設定する。
buttonBarNegativeButtonStyleはNegativeButtonの文字色を設定する。
android:textColorはタイトルの文字色を設定する。
android:textColorPrimarはメッセージの文字色を設定する。
android:backgroundはダイアログの背景色を設定する。

themes.xml

    <style name="CustomAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
        <item name="buttonBarPositiveButtonStyle">@style/alertDialogPositiveStyle</item>
        <item name="buttonBarNegativeButtonStyle">@style/alertDialogNegativeStyle</item>
        <item name="android:textColor">@color/green</item>
        <item name="android:textColorPrimary">@color/purple</item>
        <item name="android:background">@color/lightcyan</item>
   </style>

   <style name="alertDialogPositiveStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
       <item name="android:textColor">@color/pink</item>
   </style>

   <style name="alertDialogNegativeStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
       <item name="android:textColor">@color/orange</item>
   </style>

colors.xml
    <color name="pink">#ff1493</color>
    <color name="orange">#ff8c00</color>
    <color name="red">#ff0000</color>
    <color name="green">#006400</color>
    <color name="purple">#800080</color>
    <color name="lightcyan">#e0ffff</color>
MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.button1)
        button.setOnClickListener{
            AlertDialog.Builder(this, R.style.CustomAlertDialog)
                .setTitle("タイトルなのだ")
                .setMessage("メッセージですですXXXXXXXXXXX")
                .setPositiveButton("OK"){ _, _ -> }
                .setNegativeButton("キャンセル"){ _, _ -> }
                .show()
        }
    }
}

activity_main.xml
<?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/button1"
        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_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.19" />
</androidx.constraintlayout.widget.ConstraintLayout>

完成

2
3
0

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?