Alertdialog inside AlerDialog - java

I need to show AlertDialog#1 with custom layout R.layout.add_new_entry with button mybtn and this button should open another AlertDialog#2 with single choice list and after choosing one of them this dialog (AlertDialog#2) should send index of shoosen into first dialog (AlertDialog#1) and set some values into EditText field in AlerDialog#1. Is it possible to make?
My dialog#1 with custom layout:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_new_entry, null))
.setTitle(dialogTitle)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
});
builder.create().show();
I'v spent all night on it and i can't find any good examples or articles how to open dialog inside dialog.
Thanks

Related

How to show user input details back on alert dialog?

Please, i would like to show back details after the user must have input something, back on alert dialog box in Android studio. I used this code below:
editText = (EditText) findViewById(R.id.my_edit_txt);
editText.getText().toString();
But it doesn't show on the confirmation dialog box I created.
It looks like you didn't set the text of your AlertDialog, but this is just an assumption because there is not enough code in your question. Calling editText.getText().toString() does not do anything but return a String. It does not assign it to anything. An example with an AlertDialog would be the following:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(editText.getText().toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
I've took this example from Android Developers and modified it so that it includes the text of your EditText. This code should work because you not only call the toString() method but also assign it's return value to the AlertDialog's message property.
This is my entire code for the alert dialog box:
public void alertdialog(View view){
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder cfmalt = new AlertDialog.Builder(Dashboard.this);
//cfmalt.setMessage("Do you want to quit?").setCancelable(false);
//editText.getText().toString();
cfmalt.setMessage(editText.getText().toString()+"\n"+ vol_edit2.getText().toString());
cfmalt.setMessage(dt.getMonth())
//cfmalt.setMessage("Name:").setMessage(vol_edit2.getText().toString());
cfmalt.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
cfmalt.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});

Change the background of the AlertDialog

i want to know how to change the design of my AlertDialog, the buttons the background.
thank you.
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Do you want to exit")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_menu_zoom)
.show();
}
AlertDialog.Builder alertDialog_ImageSelector=new AlertDialog.Builder(mcontext);
View imageGuideLineInfo=getActivity().getLayoutInflater().inflate(R.layout.your_alyout,null);
alertDialog_ImageSelector.setView(imageGuideLineInfo);
TextView tv_guidelines=(TextView)imageGuideLineInfo.findViewById(R.id.tv_guidelines);
final AlertDialog adinfo=alertDialog_ImageSelector.create();
adinfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//here you can change the background of alertDialog.
adinfo.show();
if you want to change the background of buttons then you need to create the buttons in your_view.xml and bind them here and setBackground.
use this android inbuilt theme
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

AlertDialog not showing in Fragment

I've got a really strange problem where on some KitKat devices, my simple yes/no AlertDialog will appear behind the current fragment and not in the foreground. The reason I say the dialog appears behind the current fragment is because the dialog appears in the foreground only after I rotate the device. The app has a MainActivity that switches between different fragments that take up most of the screen.
MainActivity.java
#Override
public void onBackPressed() {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit_confirm_summary))
.setTitle(getString(R.string.exit_confirm_title))
.setCancelable(true)
.setPositiveButton(getString(R.string.ok),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
After doing some research I found that it is best to use DialogFragment when using Fragments in your app, so I changed my code to this:
MainActivity.java
#Override
public void onBackPressed() {
AlertDialogFragment adf = new AlertDialogFragment();
adf.setRetainInstance(true);
adf.show(getFragmentManager(), "dialog");
}
AlertDialogFragment.java
public class AlertDialogFragment extends DialogFragment {
public AlertDialogFragment() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle("Alert DialogFragment")
.setMessage("AlertDialogFragment Test")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
}).create();
}
}
However, the effect is still the same. The dialog should appear when I press the back button, but is only visible after I press the back button and then rotate the device. It also becomes visible after I go home and come back into the app. I've noticed it only happens on a few devices but I'd like to get rid of this problem for good.
Note: this behavior happens for all dialogs in the app, not just this one.
Anyone have any ideas what is going on?

How to disable positive and negative button if the checkbox is unchecked in java?

I'm trying to do alert message by disable ok and cancel button if the checkbox is unchecked.
reconfirm.java:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
alertDialogBuilder.setView(popupView);
CheckBox check= (CheckBox)findViewById(R.id.checkBox1);
if (check.isChecked() ) {
AlertDialog dialog = null;
((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
}
alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
agree.class);
startActivity(intObj);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
IntentExampleActivity.class);
startActivity(intObj);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
This is answered already here--How to disable / enable dialog negative positive buttons?
after dialog.show use below code
if(your_condition_true)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button
Try Using setEnabled() and setClickable method for buttons.
here is the doc
Try following code snippets. Hope you get some idea from this :)
if (check.isChecked()) {
alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}
where dialog is object of AlertDialog.

How to create this particular dialog view in android? [duplicate]

Can someone tell me how to create the above dialog view similar/exactly to the link [here][1], whereby the focus of the problem is to create the view in the centre of the picture?
I have done some research and it made me wonder should i be using a custom xml to create a custom dialog view or should i be using alertdialog to create the exact view programmability shown above? And even if alertdialog is possible how am i going to accommodate with so many textview messages shown in the middle of the dialog picture given alertdialog limitation? Eg: "builder.setMessage("This is the alert's body");" If you know what i mean!!
Can someone tell me the easiest way to get the exact same view because i'm kinna doing the same app and new to android.. Thanks :)
The best approach will be custom dialog. As it will be helpful creating all those background colours and the effect. I am sure the link you have posted is using custom dialog as well,
cheers
link that might help:
[1] http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
[2] http://androidideasblog.blogspot.com/2010/02/creating-custom-dialog-in-android.html
/// In your code implementations just add this code when you create dialog....after having this just have all TextView arranged in your layout and add that layout id to this below code good luck
//Dialog box creator
private Dialog constructYourDialog()
{
//Preparing views
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.***your_xml_name***, (ViewGroup) findViewById(R.id.***Yout view id***));
//Building dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setPositiveButton("Show Videos", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Video Click");
dialog.dismiss();
});
builder.setNegativeButton("E-Mail", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","E-mail Click");
dialog.dismiss();
}
});
builder.setNeutralButton("Show Map", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("","Show Map Click");
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
return alert;
}
Try following code
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert !");
builder.setMessage("your text here");
builder.setPositiveButton("Show Video", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
connect = false;
}
});
builder.setNegativeButton("Show Map", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.setNeutralButton("Show Both", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
builder.show();
UPDATE: To show custom title create a layout and inflate it using below code
LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
Remove following line from above code
builder.setTitle("Alert !");
and then set using
builder.setCustomTitle(layout)

Categories

Resources