LoginSignup
19
21

More than 5 years have passed since last update.

AlertDialogのテーマを変更したい

Posted at

例えば
ここのActivityでは全体的にHolo.Lightを使ってるのだけれど、アラートダイアログだけ「Holoの黒っぽい見た目」にしたいという場面があったとします。
私はありました。

スタイルを作る

まず、res/values/styles.xmlに下記のようなスタイルを追記します。

styles.xml
<resources>
  <style name="AwesomeDialogTheme" parent="android:Theme.Holo.Dialog">
  </style>
</resources>

スタイルを使う

次に、設定したスタイルをダイアログにあててみます

アラートダイアログを生成するときに下記のようなコードを書くはずです。
これだと、アプリやActivityで採用してるテーマをそのまま流用することになります。

YokuaruDialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

先ほど設定したスタイルを使うために下記のように書きます。

AwesomeDialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AwesomeDialogTheme));

これでダイアログだけHolo黒になった(=^・^=)

19
21
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
19
21