Closing an AlertDialog box after a button has been clicked - java

I have the following code which displays a dialog box to the user if no network connection is detected.
private void createNoNetworkDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = inflater.inflate(R.layout.offline_mode_dialog,null);
builder.setView(view);
builder.show();
}
There are two buttons in this dialog which have methods defined for their onClick actions. I would like to close the dialog pop-up after either of these button is pressed. Any ideas??

Yes,call dismiss() from the Listener's onClick since the DialogInterface reference is passed, which allows for dismissal.
Eg
builder.setPositiveButton ("Yes", new DialogInterface.OnClickListener()
{
public void onClick (DialogInterface dialog, int which)
{
//do stuff beforehand
dialog.dismiss();
}
});
Or if your buttons are inside the layout, show the dialog and keep a reference to it (final AlertDialog dialog = builder.show()). Then use dialog.findViewById() to find the respective buttons. Assign a normal View.OnClickListener and in it call dismiss() using the dialog reference you're holding.

Try this, Am using custom layout its working for me. inilitized Button custon_dialog.findViewById() and then write OncliclListner(). It will work
final Dialog custon_dialog = new Dialog(Login.this);
custon_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custon_dialog.setContentView(R.layout.forget_custom_dialog);
custon_dialog.setCancelable(true);
Button submit_Btn = (Button) custon_dialog
.findViewById(R.id.submit);
Button cancel_Btn = (Button) custon_dialog
.findViewById(R.id.cancel);
submit_Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your stuf
}
});
cancel_Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
custon_dialog.dismiss();
}
});
custon_dialog.show();
}

Related

Is it impossible to have ListvVew in Custom Dialog?

If the showChatList() function is not being called in the code, and the dialog is displayed normally.
When the listView is called via showChatList() function, it does not work.
To the original custom dialog
Is it impossible to bring up the listView?
public void callFunction() {
final Dialog dlg = new Dialog(context);
dlg.setContentView(R.layout.room_list);
dlg.show();
final Button okButton = (Button) dlg.findViewById(R.id.okButton);
final Button backbtn = (Button) dlg.findViewById(R.id.backbtn);
**final ListView chat_list = (ListView) dlg.findViewById(R.di.chat_list);**
- or
**chat_list = dlg.findViewById(R.id.chat_list);**
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
showChatList();
}
Your adapter depends on firebase.
It will wait until it gets data,
then I believe you need to call
adapter.notifyDataSetChanged();
after
adapter.add();
When dealing with views like dialogs, it is important to load all the views needed in the dialog before calling dialog.show().
Alternatively, I'll suggest the use of DialogFragment with this example: https://blog.mindorks.com/implementing-dialog-fragment-in-android.
DialogFragments allow you to manage your dialogs just like any other fragment.
Let me know which one you're able to use.

How to align message in dialog function and reuse the function?

I want to create a function for dialog method and reuse the function later on.
Code to create a dialog within a function:
private void alertView( String message ) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle( "Hello" )
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i){
}
}).show();
}
Code to call this function:
alertView("My message");
This works fine but I want to center my message. I have looked for solutions and used various methods such as:
AlertDialog alert = dialog.show();
TextView messageText =(TextView)alert.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.RED)
Nothing works. Could someone please help me with this?
Found a solution to my question from this website: http://examples.javacodegeeks.com/android/core/ui/dialog/android-custom-dialog-example/
Created an xml layout as described on the website and made a little change to the code in my java class:
private void alertView( String message ) {
//create a dialog component
final Dialog dialog = new Dialog(this);
//tell the dialog to use the dialog.xml as its layout description
dialog.setContentView(R.layout.dialog);
dialog.setTitle("your title");
TextView txt = (TextView) dialog.findViewById(R.id.txt);
txt.setText(message);
Button dialogButton = (Button)dialog.findViewById(R.id.dialogButton);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mUartCom.write("D"); //change this
}
});
dialog.show();
}
and then I've reused this function numerous times by changing the message:
alertView("Please select one of the red icons to begin");

Custom dialog disappears when clicking next to it

I want to show a custom dialog and force the user to click on whether button one or two.
The problem is that users can use the back button AND if they click on the view that is shown in the background my dialog also disappears.
Why? And how could I prevent this?
final Main t = this;
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.prompt_input_access);
dialog.setTitle("Title");
Button cmdLoginAccount = (Button) dialog.findViewById(R.id.cmdLoginAccount);
Button cmdLoginBank = (Button) dialog.findViewById(R.id.cmdLoginBank);
cmdLoginAccount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = true;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
cmdLoginBank.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = false;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
dialog.show();
You just need to use the setCanceledOnTouchOutside method :
dialog.setCanceledOnTouchOutside(false);

Which context do i need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

In Android, how do I create a popup and submit data to the main view?

I've created a Main.xml with buttons. They all perform a certain action and this is all fine, but there should also be password protected buttons. So I also created a second xml (popup.xml). This should pop up if the user presses the button. In popup.xml there is just a textfield for user input and a button to submit.
At the moment I can press on the button and the popup appears, but I don't know how to submit the user input data to the main view or just go back to the main view by pressing the button.
public class BastiLauncherActivity extends Activity implements OnClickListener {
private Button b1;
// ...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// this b1 is a button in the main view where this pop up should appear
b1 = (Button) findViewById(R.id.b1Button);
b1.setOnClickListener(this);
// ...
}
#Override
public void onClick(View v) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup, null,
false), 200, 300, true);
pw.setOutsideTouchable(true);
if (v == b1) {
// opening the popup
pw.showAtLocation(findViewById(R.id.dateiButton), Gravity.CENTER, 0, 0);
} else if (...) {
}
}
}
I see you're using a PopupWindow - to remove it you invoke dismiss().
If you just want a pop up to capture some user input then return back to the Activity that spawned the pop up then I would suggest using a Custom Dialog. You can create whatever you like in the dialog, and add whatever buttons you need with handlers for each button. An example;
new AlertDialog.Builder(Main.this)
.setTitle("Enter password")
.setMessage("Password required for this function")
.setView(/* You view layout */)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();

Categories

Resources