Created
January 16, 2017 07:09
-
-
Save yasszu/7d536ddea0fc3f38fa9993117262d12e to your computer and use it in GitHub Desktop.
NumberPickerDialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sample.view; | |
| import android.app.AlertDialog; | |
| import android.app.Dialog; | |
| import android.content.DialogInterface; | |
| import android.os.Bundle; | |
| import android.support.annotation.NonNull; | |
| import android.support.v4.app.DialogFragment; | |
| import android.util.DisplayMetrics; | |
| import android.util.Log; | |
| import android.view.Gravity; | |
| import android.view.ViewGroup; | |
| import android.widget.LinearLayout; | |
| import android.widget.NumberPicker; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * Created by Yasuhiro Suzuki on 2017/01/16. | |
| */ | |
| public class NumberPickerDialog extends DialogFragment { | |
| private static final List<String> mValues = Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); | |
| @NonNull | |
| @Override | |
| public Dialog onCreateDialog(Bundle savedInstanceState) { | |
| // Instantiate the NumberPicker view. | |
| final NumberPicker numberPicker = new NumberPicker(getContext()); | |
| numberPicker.setLayoutParams(new LinearLayout.LayoutParams( | |
| convertToPx(140), | |
| ViewGroup.LayoutParams.WRAP_CONTENT | |
| )); | |
| numberPicker.setMaxValue(mValues.size() - 1); | |
| numberPicker.setMinValue(0); | |
| numberPicker.setDisplayedValues(mValues.toArray(new String[mValues.size()])); | |
| // Instantiate the parent layout. | |
| LinearLayout parent = new LinearLayout(getContext()); | |
| parent.setLayoutParams(new LinearLayout.LayoutParams( | |
| ViewGroup.LayoutParams.MATCH_PARENT, | |
| ViewGroup.LayoutParams.WRAP_CONTENT | |
| )); | |
| parent.setOrientation(LinearLayout.VERTICAL); | |
| parent.setGravity(Gravity.CENTER_HORIZONTAL); | |
| parent.setPadding(convertToPx(56), 0, convertToPx(56), 0); | |
| parent.addView(numberPicker); | |
| // Build the AlertDilaog and set a NumberPicker. | |
| AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
| builder.setTitle("Set day of week."); | |
| builder.setPositiveButton("Set", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| Log.d("week", mValues.get(numberPicker.getValue())); | |
| } | |
| }); | |
| builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| } | |
| }); | |
| builder.setView(parent); | |
| return builder.create(); | |
| } | |
| private int convertToPx(int dp){ | |
| DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); | |
| return (int) (dp * metrics.density); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment