Last active
August 29, 2015 14:01
-
-
Save coffeearmy/d1eda44a6340107187e4 to your computer and use it in GitHub Desktop.
Dialogs in Android
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.AlertDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
public class DialogHelper { | |
public static void showDialog2Button(Context c, final String title, | |
final String message, | |
DialogInterface.OnClickListener positiveListener, | |
DialogInterface.OnClickListener negativeListener) { | |
new AlertDialog.Builder(c).setTitle(title).setMessage(message) | |
.setPositiveButton(android.R.string.yes, positiveListener) | |
.setNegativeButton(android.R.string.no, negativeListener) | |
//.setIcon(android.R.drawable.ic_dialog_alert) | |
.show(); | |
} | |
} | |
///USAGE | |
private void Test() { | |
DialogHelper.showDialog2Button(getActivity(), "ejemplo", "buh button", | |
new Dialog.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Toast.makeText(getActivity(), "HELLO-OK", | |
Toast.LENGTH_SHORT).show(); | |
} | |
}, new Dialog.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Toast.makeText(getActivity(), "HELLO-CANCEL", | |
Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} |
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.AlertDialog; | |
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.FragmentManager; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ViewSwitcher; | |
public class ItemDialog extends DialogFragment { | |
static final String EDIT_MODE = "edit_mode"; | |
public static final String FRAGMENT_TAG = "_tag"; | |
boolean mIsEdit; | |
FragmentManager mFragmentManager; | |
ViewSwitcher mViewSwitcher; | |
/** Returns an instance of the dialog */ | |
public static ItemDialog newInstance(int mode) { | |
ItemDialog f = new ItemDialog(); | |
Bundle args = new Bundle(); | |
if (mode == 1) {// Edit Account | |
args.putBoolean(EDIT_MODE, true); | |
f.setArguments(args); | |
} | |
return f; | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
LayoutInflater inflater = getActivity().getLayoutInflater(); | |
View view = inflater.inflate(R.layout._dialog, null); | |
// Is the dialog for edit a piggybank account | |
mIsEdit = getArguments() != null ? true : false; | |
// Fragment Manager | |
mFragmentManager = getActivity().getSupportFragmentManager(); | |
mViewSwitcher = (ViewSwitcher) view | |
.findViewById(R.id.AhorroWiz); | |
configureFirstStepWizard(view); | |
AlertDialog.Builder alertBuilder = new AlertDialog.Builder( | |
getActivity()); | |
alertBuilder | |
.setPositiveButton("Siguiente",null) | |
.setNegativeButton(android.R.string.cancel, null); | |
// Set buttons to the dialog | |
if (!mIsEdit) { | |
alertBuilder.setTitle("Añadir Electrodoméstico"); | |
// mTxtvDeleteAccount.setVisibility(View.GONE); | |
} else { | |
alertBuilder.setTitle("Editar Electrodoméstico"); | |
} | |
alertBuilder.setView(view); | |
final Dialog customDialog = alertBuilder.create(); | |
//For custom dialog button behaviour, if not comment follow lines --> | |
customDialog.setOnShowListener(new DialogInterface.OnShowListener() { | |
@Override | |
public void onShow(DialogInterface dialog) { | |
Button b = ((AlertDialog) customDialog).getButton(AlertDialog.BUTTON_POSITIVE); | |
b.setOnClickListener(new OnSaveClickListener()); | |
} | |
}); | |
//<-- | |
return customDialog; | |
} | |
private void configureFirstStepWizard(View view) { | |
} | |
// Listeners | |
/** OnClick listener calls to new Account method */ | |
protected class OnSaveClickListener implements | |
View.OnClickListener { | |
@Override | |
public void onClick(View v) { | |
} | |
} | |
/** OnClick listener calls to edit/modify Account method */ | |
protected class OnEditClickListener implements | |
DialogInterface.OnClickListener { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
// editAccount(); | |
} | |
} | |
} | |
///CALL DIALOG | |
private void showNewAhorroDialog() { | |
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); | |
WizardAhorroDialog newFragment = WizardAhorroDialog.newInstance(0); | |
newFragment.show(ft, WizardAhorroDialog.FRAGMENT_TAG); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment