2012年10月3日

【Android】AlertDialogのコーナーを丸めてみた


通常のダイアログでは丸まっていないダイアログの角をiPhone風に丸めてみました。



コードは以下の通りです。
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
>
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp" />
    <stroke
        android:width="1dp"
        android:color="#ff7f7f7f" />
    <solid android:color="#000000" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
>
    <stroke
        android:width="1dp"
        android:color="#ff7f7f7f" />
    <solid android:color="#ff000000" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="-1dp"
>
    <shape android:shape="rectangle">
        <corners
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp" />
        <stroke
            android:width="1dp"
            android:color="#ff7f7f7f" />
        <solid android:color="#000000" />
    </shape>
</inset>

<resources>

    <style name="AppTheme" parent="android:Theme.Light" />

    <style name="RoundedDialog">
        <item name="android:fullDark">@drawable/rounded_dialog_body</item>
        <item name="android:topDark">@drawable/rounded_dialog_header</item>
        <item name="android:centerDark">@drawable/rounded_dialog_body</item>
        <item name="android:bottomDark">@drawable/rounded_dialog_footer</item>
        <item name="android:fullBright">@drawable/rounded_dialog_body</item>
        <item name="android:centerBright">@drawable/rounded_dialog_body</item>
        <item name="android:topBright">@drawable/rounded_dialog_header</item>
        <item name="android:bottomBright">@drawable/rounded_dialog_footer</item>
        <item name="android:bottomMedium">@drawable/rounded_dialog_footer</item>
        <item name="android:centerMedium">@drawable/rounded_dialog_body</item>
    </style>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="@android:style/Theme">
        <item name="android:alertDialogStyle">@style/RoundedDialog</item>
    </style>
</resources>

public class MainActivity extends Activity {
    private static final int DIALOG_ALERT = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.text_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DIALOG_ALERT);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if(id == DIALOG_ALERT) {
            return new AlertDialog
                    .Builder(new ContextThemeWrapper(this, R.style.MyTheme))
                    .setTitle("AlertDialog")
                    .setMessage("Hello World!")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setNegativeButton("NG", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
        }
        return null;
    }
}