Skip to content

Instantly share code, notes, and snippets.

@Antarix
Last active January 3, 2016 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Antarix/8535320 to your computer and use it in GitHub Desktop.
Save Antarix/8535320 to your computer and use it in GitHub Desktop.
Snippnets for opening differnt Dialog's in android
public class DialogExamples{
private ArrayList<Integer> mSelectedDays;
public DialogExamples(){
super();
}
//Alert dialog example
public void alert(String message) {
AlertDialog.Builder bld = new AlertDialog.Builder(HomeActivity.this);
bld.setMessage(message);
bld.setNeutralButton("OK", null);
Logcat.d(TAG, "Showing alert dialog: " + message);
bld.create().show();
}
//Alert dialog with center message
public void alertCenterMessage(String message) {
AlertDialog.Builder bld = new AlertDialog.Builder(LoginActivity.this);
bld.setMessage(message);
//bld.create();
bld.setNeutralButton("OK", null);
AlertDialog dialog = bld.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
}
//Choose item from list
private void openActionDialog() {
List<String> list = Arrays.asList("Add","Delete");
final CharSequence[] items = list.toArray(new CharSequence[list.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Actions");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Add")) {
//Add action will be done here
}
if (items[item].equals("Delete")) {
//Delete action will be done here
}
}
}).create().show();
}
//Custom layout dialog
public void customDiaog() {
final Dialog dialog = new Dialog(SettingsActivity.this,style.Base_Theme_AppCompat_Light_Dialog);
dialog.setTitle("ADD PASSCODE");
dialog.setContentView(R.layout.dialog_add_passcode);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final EditText editPasscode = (EditText) dialog
.findViewById(R.id.edit_passcode);
final EditText editName = (EditText) dialog
.findViewById(R.id.edit_name);
final Button buttonSave = (Button) dialog
.findViewById(R.id.button_add);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Utility.notBlank(editName, false) && Utility.notBlank(editPasscode, false)) {
if (DatabaseFunctions.addPasscode(editName.getText().toString(), editPasscode.getText().toString())) {
fillListView();
}
dialog.dismiss();
}
}
});
dialog.show();
}
//Multiple selection dialog
public void multipleSelectionDialog(View v){
final String[] items = new String[] { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
boolean[] checkedItems = new boolean[7];
for (int i = 0; i < 7; i++) {
if(mSelectedDays.contains(i)){
checkedItems[i] = true;
}else{
checkedItems[i] = false;
}
}
dialog.setTitle("Select days")
.setMultiChoiceItems(items,checkedItems, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(mSelectedDays.contains(which)){
mSelectedDays.remove(Integer.valueOf(which));
}else{
mSelectedDays.add(which);
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
dialog.dismiss();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog.create().show();
}
//Help screen dialog
public void helpScreen() {
// Here pass the activity's object.
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Make the layout transparent.
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
// Set up the content of the dialog to the help_screen.xml file.
dialog.setContentView(R.layout.help_screen);
dialog.setCanceledOnTouchOutside(true);
//Dismiss the HelpScreen from touching anywhere on screen.
View view = dialog.findViewById(R.id.help_screen_layout);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
}
@sendtomitesh
Copy link

I need a dialog with simple text entry, its not present in this...

@sendtomitesh
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment