Androidでのカスタムダイアログタイトルの実装

Android標準のダイアログタイトルはデザインが限定的であるため、独自にカスタマイズされたタイトルを設定することができます。これには、AlertDialog.BuilderクラスのsetCustomTitle()メソッドを使用します。

まず、カスタムタイトル用のレイアウトファイルcustom_title.xmlを作成します。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/custom_top"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/blue"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="都市を選択"
            android:textColor="@color/white"
            android:textSize="22sp" />
    </LinearLayout>
</LinearLayout>

次に、アクティビティのレイアウトファイルactivity_main.xmlを定義します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp">

    <Button
        android:id="@+id/select_city_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="都市選択1" />

    <Button
        android:id="@+id/select_city_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="都市選択2" />
</LinearLayout>

最後に、メインアクティビティのコードMainActivity.javaを記述します。

package com.example.customdialog;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private String[] cities = {"東京", "大阪", "名古屋", "札幌", "福岡", "京都"};
    private Button button1, button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.select_city_1);
        button2 = findViewById(R.id.select_city_2);

        button1.setOnClickListener(v -> showDefaultDialog());
        button2.setOnClickListener(v -> showCustomTitleDialog());
    }

    private void showDefaultDialog() {
        new AlertDialog.Builder(this)
                .setTitle("都市を選択")
                .setItems(cities, (dialog, which) -> {})
                .create()
                .show();
    }

    private void showCustomTitleDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View customView = inflater.inflate(R.layout.custom_title, null);

        TextView titleTextView = customView.findViewById(R.id.title_text);
        titleTextView.setText("カスタムタイトル");

        new AlertDialog.Builder(this)
                .setCustomTitle(customView)
                .setItems(cities, (dialog, which) -> {})
                .create()
                .show();
    }
}

上記のコードを実行すると、左側は標準のダイアログ(setTitle()でタイトルを設定)、右側はカスタムタイトルを持つダイアログ(setCustomTitle()を使用)が表示されます。

タグ: Android AlertDialog CUSTOM_TITLE

7月28日 16:13 投稿