Last active
June 26, 2016 06:34
-
-
Save talenguyen/4baebc1652dc6f0ce7c81fe88be60302 to your computer and use it in GitHub Desktop.
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
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatDialogFragment; | |
import android.util.Log; | |
/** | |
* Created by Giang Nguyen on 6/25/16. | |
*/ | |
public class SingleChoiceDialogFragment extends AppCompatDialogFragment { | |
public static final String EXTRA_TITLE = "title"; | |
public static final String EXTRA_ITEMS = "items"; | |
public static final String EXTRA_SELECTION = "selection"; | |
private static final String TAG = "SingleChoiceDialog"; | |
public static SingleChoiceDialogFragment newInstance(String title, String[] items, int selection) { | |
if (title == null || items == null || items.length == 0) { | |
throw new NullPointerException("title and items must not be null or empty"); | |
} | |
if (selection < 0 || selection > items.length - 1) { | |
throw new IllegalArgumentException("selection must be in [0, item.length)"); | |
} | |
Bundle args = new Bundle(); | |
args.putString(EXTRA_TITLE, title); | |
args.putStringArray(EXTRA_ITEMS, items); | |
args.putInt(EXTRA_SELECTION, selection); | |
SingleChoiceDialogFragment fragment = new SingleChoiceDialogFragment(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { | |
final Bundle args = getArguments(); | |
final String title; | |
final String[] items; | |
final int selection; | |
if (args != null) { | |
title = args.getString(EXTRA_TITLE); | |
items = args.getStringArray(EXTRA_ITEMS); | |
selection = args.getInt(EXTRA_SELECTION); | |
} else { | |
title = ""; | |
items = new String[0]; | |
selection = 0; | |
} | |
return new AlertDialog.Builder(getContext()) | |
.setTitle(title) | |
.setSingleChoiceItems(items, selection, new DialogInterface.OnClickListener() { | |
@Override public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
onItemSelected(which); | |
} | |
}) | |
.create(); | |
} | |
private void onItemSelected(int which) { | |
if (getParentFragment() instanceof OnItemSelectedListener) { | |
((OnItemSelectedListener) getParentFragment()).onItemSelected(which); | |
} else if (getActivity() instanceof OnItemSelectedListener) { | |
((OnItemSelectedListener) getActivity()).onItemSelected(which); | |
} else { | |
Log.i(TAG, "onItemSelected() called with: " + "which = [" + which + "]"); | |
} | |
} | |
public interface OnItemSelectedListener { | |
void onItemSelected(int position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment