woshidan's blog

あいとゆうきとITと、とっておきの話。

ProgressDialogの色を変えたかったのでカスタムレイアウトをDialogにぶっこむ

ProgressDialogのProgressBarの色を変えようと思ったんだけど色いじるAPI見あたらなかったので、Dailogにカスタムレイアウトを突っ込んでみた。

とりあえずDialogで書いてから、DiaogFragmentでも書いてみた。

Dialogの方

public class MainActivity extends AppCompatActivity {
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog == null) {
                    dialog = new Dialog(MainActivity.this);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.dialog);
                    ((TextView) dialog.findViewById(R.id.progressDialogMessage)).setText("Loading.....");
                    dialog.show();
                } else if (!dialog.isShowing()) {
                    dialog.show();
                } else {
                    dialog.dismiss();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
<!-- dialog.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="16dp"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressDialogCircular"
        android:layout_centerVertical="true"
        android:max="100"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/progress_style"
        android:layout_gravity="center_vertical|left" />
    <TextView
        android:id="@+id/progressDialogMessage"
        android:text="仮に入れてる..."
        android:padding="16dp"
        android:layout_toRightOf="@id/progressDialogCircular"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLa

<!-- progress_style.xml -->
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >
        <size
            android:height="76dip"
            android:width="76dip" />

        <gradient
            android:angle="0"
            android:endColor="#ff99aa"
            android:startColor="#99ffffff"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>

DialogFragmentの方

レイアウト等は変わらず。

package com.example.woshidan.progressdialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isDialogShown()) {
                    dismissDialog();
                } else {
                    showDialog();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private boolean isDialogShown() {
        return (getFragmentManager().findFragmentByTag("dialog") != null);
    }

    private void dismissDialog() {
        android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
        android.app.Fragment prev = getFragmentManager().findFragmentByTag("dialog");

        if (prev == null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        ft.commit();
    }

    private void showDialog() {
        android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
        android.app.Fragment prev = getFragmentManager().findFragmentByTag("dialog");

        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        MyDialogFragment.newInstance("Loading...");

        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance("Loading...");
        newFragment.show(ft, "dialog");
    }

    private static class MyDialogFragment extends DialogFragment {
        private String message;

        static MyDialogFragment newInstance(String message) {
            MyDialogFragment f = new MyDialogFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putString("message", message);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            message = getArguments().getString("message");

            int style = DialogFragment.STYLE_NO_FRAME;
            int theme = android.R.style.Theme_Holo_Light_Dialog_NoActionBar;

            setStyle(style, theme);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.dialog, container, false);
            ((TextView)v.findViewById(R.id.progressDialogMessage)).setText(message);
            return v;
        }
    }
}

f:id:woshidan:20160117122535g:plain