futabooo blog

色々手をつけすぎてすぐに忘れるので備忘録

Androidでカスタムdialog

背景画像がデフォのアイコン画像なのでださいけども。
こんなかんじのが最終的にできます。
f:id:futabooo:20131108014918p:plain


MainActivity

public class MainActivity extends Activity implements View.OnClickListener {

    private Context context;
    private Button button;
    private Dialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        if (dialog != null) {
            dialog.dismiss();
            button.setEnabled(true);
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        v.setEnabled(false);
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                dialog = new Dialog(context, R.style.AppBaseTheme_CustomDialog);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setCancelable(false);
                dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                dialog.getWindow().getAttributes().width = ViewGroup.LayoutParams.FILL_PARENT;
                dialog.getWindow().getAttributes().height = ViewGroup.LayoutParams.FILL_PARENT;
                dialog.show();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                if (dialog != null) {
                    dialog.dismiss();
                    button.setEnabled(true);
                }
            }

        };
        task.execute((Void[]) null);
    }


main.xml

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

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show dialog" />
</LinearLayout>


theme_custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme.CustomDialog" parent="android:Theme.DeviceDefault.Dialog">

        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

</resources>


custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>