Hide Custom Dialog on button click - java

Currently I am using the following code and created a custom dialog. And dialog opens when button is clicked,, but I want to hide the dialog by tapping on OK button..but its not working and not showing error...
I used 'dialog.dissmis'
Code is here:
final Dialog dialog =new Dialog(ActionBarActivity. this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_dictionary);
TextView word = (TextView)dialog.findViewById(R.id.txtWord);
final TextView wordMeaning = (TextView)dialog.findViewById(R.id.txtMeaning);
dialog.getWindow().getAttributes().windowAnimations = R.style.AnimLeftRight;
//Get Words and it's meanings.
word.setText(Dictionary.getWord());
wordMeaning.setText(Dictionary.getMeaning());
Button btntts = (Button)dialog.findViewById(R.id.btntts);
final Button ok = (Button)dialog.findViewById(R.id.btnPositive);
// Show the dialog first.
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
btntts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String convertTextToSpeech
wordMeaning.getText().toString();
convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
convertToSpeech.setLanguage(Locale.US);
convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
Please anyone help me to hide the dialog on button click..thanks

This is the proper way to code a custom dialog box.
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.custom_alert_view, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
//Get Button from the layout.
Button dismiss = (Button) builderView.findViewById(R.id.dismiss);
//Click the dismiss button from within the alert. will dismiss the dialog box
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});

Related

Alertdialog closing automatically after seconds?

I have implemented a Alertdialog, but this dialog closes automatically after 1 second. He doesn't even pay attention to the buttons, but simply closes automatically. I can't find the error in the code, do you perhaps know where my error is?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText name = new EditText(MainActivity.this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Ok", null);
builder.setNegativeButton("Cancel", null);
builder.setView(name);
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button btnOK = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnCancel = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = name.getText().toString();
if (name.isEmpty() || name.matches("")) {
name.setError("ERROR");
} else {
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("NAME", name);
editor.apply();
//Switch to next activity
}
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
});
dialog.show();
What is wrong?
There is nothing wrong in the code. But I have a suspicion on the code you have given.
How to correct?
Instead of adding clickListeners on btnOk and btnCancel inside the onShownListener, you can add then outside the onShow method. It will not make any difference but I feel that that was closing the dialog.That is why I feel you shall try what I have mentioned.

Android: How to use a card view instead of a button

I have a dialog which currently opens on a button click and works fine but it means I have an ugly button that does not look good, I would prefer it to be opened from a CardView.
This is the card view:
CardView manager=findViewById(R.id.manager_card);
manager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {}
});
And this is the dialog which currently opens from a button click:
Button btnLoginDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Init Widget Button and set click listener
btnLoginDialog = (Button) findViewById(R.id.btnLoginDialog);
btnLoginDialog.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == btnLoginDialog) {
// Create Object of Dialog class
final Dialog login = new Dialog(this);
// Set GUI of login screen
login.setContentView(R.layout.login_gui);
login.setTitle("Login to Pulse 7");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)login.findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)login.findViewById(R.id.txtPassword);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtUsername.getText().toString().trim().equals("admin") && txtPassword.getText().toString().trim().equals("admin"))
{
// Validate Your login credential here than display message
Toast.makeText(SignInActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
Intent intent = new Intent(getApplicationContext(), ManagerMenu.class);
startActivity(intent);
}
else
{
Toast.makeText(SignInActivity.this,
"Please enter valid Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
}
}
I can't figure this out. I hope there is enough information there for someone to help me out.
Use this type,this will help you:-
CardView manager=findViewById(R.id.manager_card);
manager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog login = new Dialog(this);
// Set GUI of login screen
login.setContentView(R.layout.login_gui);
login.setTitle("Login to Pulse 7");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)login.findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)login.findViewById(R.id.txtPassword);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtUsername.getText().toString().trim().equals("admin") && txtPassword.getText().toString().trim().equals("admin"))
{
// Validate Your login credential here than display message
Toast.makeText(SignInActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
Intent intent = new Intent(getApplicationContext(), ManagerMenu.class);
startActivity(intent);
}
else
{
Toast.makeText(SignInActivity.this,
"Please enter valid Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
}
});

AlertDialog & SnackBar alerts

Would it be possible to close an AlertDialog without having to click on/set a PositiveButton/NegativeButton?
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.DeleteRecord(_id);
db.close();
Snackbar.make(textEntryView, "Removed", Snackbar.LENGTH_LONG).setDuration(700).show();
}
});
Using a custom button added to my layout I want to click on it and have it close the AlertDialog aswell.
I have a total of 3 buttons currently
Update Record
Delete Record
Done (This one is using alert.setPositiveButton)
The other two I added in my layout so I could use Snackbar alert and I know I can do this by using setNeutralButton but it won't show a SnackBar alert.
I this code will work-
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final FrameLayout customView= (FrameLayout) View.inflate(this, R.layout.custome_view, null);
final Button button = (Button) customView.findViewById(R.id.my_button);
button.setText(mTvEmail.getText().toString());
builder.setView(customView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
To make it easier this is the whole thing
private void CreatePopup(Long id) {
final LayoutInflater Manual = LayoutInflater.from(this);
final View textEntryView = Manual.inflate(update, null);
final EditText infoData = (EditText) textEntryView.findViewById(R.id.InfoData);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final TextView TextSet = (TextView) textEntryView.findViewById(R.id.product);
final Button Accept = (Button) textEntryView.findViewById(R.id.button);
final Button Delete = (Button) textEntryView.findViewById(R.id.delete);
final SQLite db = new SQLite(this);
final Long _id = id;
final SQLiteDatabase X = db.getReadableDatabase();
final Cursor c;
c = X.rawQuery("SELECT Product FROM Inventory WHERE _id =" + _id, null);
c.moveToFirst();
final String Data = c.getString(c.getColumnIndexOrThrow("Product"));
TextSet.setText(Data);
c.close();
Accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (infoData.length() != 0) {
final Editable Data = infoData.getText();
db.UpdateRecord(Data, _id);
db.close();
Snackbar.make(textEntryView, "Updated", Snackbar.LENGTH_LONG).setDuration(700).show();
}
else {
Toast toast = Toast.makeText(getApplicationContext(),
"Input Quantity!", Toast.LENGTH_SHORT);
toast.show();
}
}});
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.DeleteRecord(_id);
db.close();
Snackbar.make(textEntryView, "Removed", Snackbar.LENGTH_LONG).setDuration(700).show();
}
});
alert.setTitle("Update Quantity").setView(textEntryView);
alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
CreateListView();
}
});
alert.show();
}

alertDialog not showing when creating cancel button

I think I know where exactly is the problem but I don't know if I'm right or not and I don't know how to fix it
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view1 = getActivity().getLayoutInflater().inflate(R.layout.dialog_reserv_table, null);
builder.setView(view1);
final AlertDialog dialog = builder.create();
Button annulerButton = (Button) view1.findViewById(R.id.annulerReserv);
annulerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
I think creating dialog it should be after all declarations and before show
in this way
AlertDialog dialog=builder.create();
dialog.show();
but in this way I can't call dialog.cancel();
You can create a custom Dialog without AlertDialog.Builder:
Edit:
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_reserv_table);
Button annulerButton=(Button)dialog.findViewById(R.id.annulerReserv);
annulerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
Source: http://www.mkyong.com/android/android-custom-dialog-example/

Can't add OnClickListener to a Button in a Dialog

I have written a function to handle showing of Dialog but I am not able to use OnClickListener in it. What is wrong with my code can any one tell me?
Here is my function
private void showInputDialog() {
final Dialog dialog=new Dialog(MainDashboard.this);
dialog.setContentView(R.layout.frg_dialog_change_pass);
btn_save_password=(Button) findViewById(R.id.btn_save_password);
btn_cancel_pass=(Button) findViewById(R.id.btn_cancel_pass);
edtOldpass=(EditText) findViewById(R.id.edtOldpass);
edtNewpass=(EditText) findViewById(R.id.edtNewpass);
edtConfirmpass=(EditText)findViewById(R.id.edtConfirmpass);
dialog.show();///Show the dialog.
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}
Calling Activity.findViewById() will look for the View in the layout of your Activity (the one you've set by calling setContentView() in onCreate()).
I guess those Views are in your Dialog layout, so you need to call findViewById() on your Dialog instance:
btn_save_password = (Button) dialog.findViewById(R.id.btn_save_password);
btn_cancel_pass = (Button) dialog.findViewById(R.id.btn_cancel_pass);
edtOldpass = (EditText) dialog.findViewById(R.id.edtOldpass);
edtNewpass = (EditText) dialog.findViewById(R.id.edtNewpass);
edtConfirmpass = (EditText) dialog.findViewById(R.id.edtConfirmpass);
// Declare this globally above oncreate
private android.app.AlertDialog dialog;
android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(MainDashboard.this);
LayoutInflater layoutInflater = getLayoutInflater();
View alertView = layoutInflater.inflate(R.layout.frg_dialog_change_pass, null);
alertDialog.setView(alertView);
alertDialog.setCancelable(false);
Button btn_save_password= (Button) alertView.findViewById(R.id.btn_save_password);
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do your stuff here
}
});
if(dialog !=null)
dialog.dismiss();
dialog = alertDialog.create();
dialog.show();
Change your function to:
private void showInputDialog() {
final Dialog dialog=new Dialog(MainDashboard.this);
View view = LayoutInflater.from(MainDashboard.this).inflate(R.layout.frg_dialog_change_pass);
dialog.setContentView(view);
btn_save_password=(Button) view.findViewById(R.id.btn_save_password);
btn_cancel_pass=(Button) view.findViewById(R.id.btn_cancel_pass);
edtOldpass=(EditText) view.findViewById(R.id.edtOldpass);
edtNewpass=(EditText) view.findViewById(R.id.edtNewpass);
edtConfirmpass=(EditText)view.findViewById(R.id.edtConfirmpass);
dialog.show();///Show the dialog.
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}
Basically you have to use findViewById with the view which is used for dialog .

Categories

Resources