how to close a popup dialog? - java

I have a simple text of code in my main.class file which creates a popup dialog. When I try to close the popupdialog with finish(); method, it shuts down the whole application, and not only the popup dialog box. How can i solve this ? ( this is for android, look at my comments below aswell ).
This is the code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.start_dialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.loss);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.golf_ball);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();

Jesmar,
Hello and welcome.
You probably intended to use the dismiss function on your Dialog.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Cheers!

button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
actually that finish() is belong to your activity, not your dialog. do dialog.dismiss() instead

Use cancel() or dismiss()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
or
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});

Related

On clicking the button dialog is not visible

I have created the button but when I click on the button it is not showing my dialog.
The button is shown that it clicks but nothing appears. I am trying to show a ⚠ dialog
Here my code
XML
<Button
android:layout_width="wrap_content"
android:id="#+id/submitB"
android:layout_height="36dp"
android:layout_weight="1"
android:background="#drawable/option_button"
android:text="#string/submit" />
XML under the LinearLayout
JAVA
private Button submitB;
submitB = (Button) findViewById(R.id.submitB);
submitB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitTest();
}
});
}
private void submitTest()
{
AlertDialog.Builder builder = new AlertDialog.Builder(QuestionsActivity.this);
builder.setCancelable(true);
View view = getLayoutInflater().inflate(R.layout.alert_dialog_layout,null);
Button cancelB= view.findViewById(R.id.cancle);
Button confirmB= view.findViewById(R.id.confirmB);
builder.setView(view);
AlertDialog alertDialog= builder.create();
cancelB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
Call show() on your alertDialog object
private void submitTest()
{
AlertDialog.Builder builder = new AlertDialog.Builder(QuestionsActivity.this);
builder.setCancelable(true);
View view = getLayoutInflater().inflate(R.layout.alert_dialog_layout,null);
Button cancelB= view.findViewById(R.id.cancle);
Button confirmB= view.findViewById(R.id.confirmB);
builder.setView(view);
AlertDialog alertDialog= builder.create();
cancelB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
It seems you are missing the show() method to show your Dialog.
I suggest you this syntax which I use to show an AlertDialog:
new AlertDialog.Builder(CompileModuleActivity.this)
.setTitle(getResources().getString(R.string.success))
.setMessage(myMessage)
.setIcon(R.drawable.ic_check)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
Of course you can create a NegativeButton or NeutralButton to make the dialog dismiss. I just put finish() for my purpose.

how to display dialog box on click of items in recyclerview

I am trying to display a dialog box when a particular item is clicked, but I can only see a little box in place of the layout I have set. here is the onbindview holder.
#Override
public void onBindViewHolder(ListEventAdapter.ListEventViewHolder holder, int position) {
EventResponse event = eventList.get(position);
view to be clicked
holder.home_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(view);
}
});
private void showAlertDialog(View view) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.alert_dialog);
home_t = dialog.findViewById(R.id.home_t);
place_btn = (CircularProgressButton) dialog.findViewById(R.id.place_bet_btn);
close = dialog.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
place_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
on R.layout.alert_dialog just use match_parent in layout_width

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();
}
});

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 .

I wrote this few lines of code to change the xml file of this class but it doesn't work

Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1".
Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...
public void SwitchLayout()
{
Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);
View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
weiter_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
weiter_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
zurück_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
}
Need Help ;) Thank you!
Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.
A better way is define onclick attribute in the layouts xml file.
For instance if you have this:
<Button
android:id="#+id/w2"
android:onClick="getms3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="w2" />
then you should define
public void getms3(View v)
{
setContentView(R.layout.ms3);
}
Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
}
});

Categories

Resources