Closing Custom Dialog Box - java

I am Using the following code for my custom dialog box.
Code is here
I am using a new layout by setCustomView Method.That layout contains a 'Ok' button and a 'Cancel' button.
I need to close the dialog box when click on cancel.
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
//up to this comes , here what I can wright
}
});

dialogObject.dismiss();
You can use this method

Why not you create custom dialog from here:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Very clearly explained and easy to implement too.

try this :
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
qustomDialogBuilder.dismiss();//this line will close the dialog
}
});

Replace your TestDialogActivity like below,
public class TestDialogActivity extends Activity {
private static final String HALLOWEEN_ORANGE = "#FF7F27";
private AlertDialog alertDialog;
private OnClickListener mShowDialogClickListener = new OnClickListener() {
public void onClick(View v) {
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(
v.getContext())
.setTitle("Set IP Address")
.setTitleColor(HALLOWEEN_ORANGE)
.setDividerColor(HALLOWEEN_ORANGE)
.setMessage("You are now entering the 10th dimension.")
.setCustomView(R.layout.example_ip_address_layout,
v.getContext())
.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
alertDialog=qustomDialogBuilder.create();
qustomDialogBuilder.setAlertDialog(alertDialog);
alertDialog.show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(mShowDialogClickListener);
}
and replace setCustomView of QustomDialogBuilder like below
public QustomDialogBuilder setCustomView(int resId, final Context context) {
View customView = View.inflate(context, resId, null);
((TextView)customView.findViewById(R.id.ip_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
return this;
}
finally add below line into your QustomDialogBuilder
private AlertDialog alertDialog;
public void setAlertDialog(AlertDialog alertDialog)
{
this.alertDialog=alertDialog;
}
To close your dialog click on IP Address text.

Using the QustomDialog Source here in your activity class (TestDialogActivity), you can set the "Ok" and "Cancel" button by setting the Negative and Positive button of the dialog like this:
private OnClickListener mShowDialogClickListener =new OnClickListener(){
public void onClick(View v){
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.").
setCustomView(R.layout.example_ip_address_layout, v.getContext()).
setIcon(getResources().getDrawable(R.drawable.ic_launcher));
qustomDialogBuilder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
qustomDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
/**
* Do something here...
*/
}
});
qustomDialogBuilder.show();
}
};
And it will look like this:
Hope you'll find this helpful. Thanks!

Related

void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object referenc [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have added one button "btn". I have set onclicklistener, inside the button "btn" I have added another button "btnYes" to show display custom dialog when I add these "btnYes" apps get crash.
When I remove the "btnyes" button app is working.
Can we add onclicklistener for two button inside one button for different work?
Java Code
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btnYes = findViewById(R.id.yes);
btnNo = findViewById(R.id.no);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.customdialog.MainActivity$1.onClick(MainActivity.java:33)
If btnYes and btnNo are on the dialog, then you should initialize these buttons using the AlertDialog's View object.
You have to modify your code like following.
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
// these button should be initialize here.
btnYes = myView.findViewById(R.id.yes);
btnNo = myView.findViewById(R.id.no);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Hope it helps you.
if you want to display Dialog with choices yes or no, use this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for yes
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for no
}
});
AlertDialog alert = builder.create();
alert.show();
But, if you want to use a custom dialog layout, you should do it like this.
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.YOUR_LAYOUT_HERE);
Button btnyes= dialog.findViewById(R.id.btnyes);
btnyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});

Why is my Android snackbar dismissed after clicking its action button?

I have a snackbar that I build with a duration set to Snackbar.LENGTH_INDEFINITE
The snackbar is displayed properly when I call mySnackbar.show();
But as soon as I hit the action button, the snackbar is dismissed.
The dismiss method seems called by the system.
Does anyone know a workaround ?
Here is my code for building my snackbar:
Snackbar mySnackbar = Snackbar.make(mParent, R.string.the_question, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.yes, new View.OnClickListener() {
#Override
public void onClick(View v) {
//My code...
}
})
.addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar snackbar, int event) {
}
#Override
public void onShown(Snackbar snackbar) {
}
});
Below code is showing the Alert dialog "after" the snackbar is displayed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
"This is Snackbar", Snackbar.LENGTH_INDEFINITE).
setAction(R.string.yes, new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}).addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar transientBottomBar, int event) {
super.onDismissed(transientBottomBar, event);
}
#Override
public void onShown(Snackbar sb) {
super.onShown(sb);
}
});
snackbar.show();
showAlertDialog(this, "Alert!!", "Alert Dialog", "Yes", "No");
}
The showAlertDialog is simple static method to show the dialog
public static void showAlertDialog(Context context, String title, String message, String posBtnMsg, String negBtnMsg) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(posBtnMsg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton(negBtnMsg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
The screen shot of the output for above code is below,
The answer to this question lies in the way Snackbar.setAction(CharSequence text, final View.OnClickListener listener) is implemented
If you pass this method a non empty text or non null listener, the TextView displaying the action's text is set an OnClickListener which calls BaseTransientBottomBar.dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION) when the action is performed. This causes the Snackbar to be dismissed.
To prevent that, one needs to retrieve the TextView of the Snackbar's action view, and override its OnClickListener with a listener that does not call dispatchDismiss()
Here is the Snackbar.setAction() code for reference
public Snackbar setAction(CharSequence text, final View.OnClickListener listener) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) mView.getChildAt(0);
final TextView tv = contentLayout.getActionView();
if (TextUtils.isEmpty(text) || listener == null) {
tv.setVisibility(View.GONE);
tv.setOnClickListener(null);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onClick(view);
// Now dismiss the Snackbar
dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION);
}
});
}
return this;
}

close Alert Dialog without using negative Buttons

I created an AlertDialog, which will either refresh the activity (when btnConfirm1 is pressed), or does nothing and simply closes down (when btnDisconfirm1 is pressed). Everything is working apart from btnDisconfirm1. How can I close the dialog?
So apparently AlertDialog does not have a dismiss or cancel method, but is there another way without using negative buttons? The thing is, I created a layout file for this dialog and I don't know how to put a negative button in my xlm-file.
Or should I use a completely different approach apart from AlertDialog? Thanks!
btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder2 = new AlertDialog.Builder(ScoreScreen.this);
View mView2 = getLayoutInflater().inflate(R.layout.dialog_confirm_delete, null);
Button btnConfirm1=(Button) mView2.findViewById(R.id.btnConfirm1);
Button btnDisconfirm1=(Button) mView2.findViewById(R.id.btnDisconfirm1);
btnConfirm1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setPlayerScore(0);
player2.setPlayerScore(0);
player3.setPlayerScore(0);
player4.setPlayerScore(0);
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
btnDisconfirm1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT DO I PUT HERE???
}
});
mBuilder2.setView(mView2);
AlertDialog dialog = mBuilder2.create();
dialog.show();
}
});
First you should create the AlertDialog with
AlertDialog mDialog = mBuilder2.create();
And secondly you can dismiss the dialog inside the OnClickListener with
mDialog.dismiss();
You can put this method in your Util class. and use callbacks
public interface OnDialogDismiss {
void onPositiveClick();
void onNegativeClick();
}
Modify it as your requirement.
public void showDialogForMultipleCallback(Context context, String title, String message, boolean cancellable, String neutralBbtn, String negativeBtn, String positiveBtn, final OnDialogDismiss onDialogDismiss) {
final AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle(title);
builder1.setMessage(message);
builder1.setCancelable(cancellable);
if (neutralBbtn != null) {
builder1.setNeutralButton(neutralBbtn, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (alert11 != null)
alert11.dismiss();
}
});
}
if (negativeBtn != null) {
builder1.setNegativeButton(negativeBtn,
new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (alert11 != null)
alert11.dismiss();
}
});
}
if (positiveBtn != null) {
builder1.setPositiveButton(positiveBtn, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
onDialogDismiss.onPositiveClick();
}
});
}
alert11 = builder1.create();
alert11.show();
}

Getting java.lang.NullPointerException on customDialogBox

I am getting java.lang.NullPointerException for this method. I have check the adapter and lists are just fine and the list is ofcourse under fragment_phonelist xml file.
private void showCustomDialog() {
final Dialog dialog = new Dialog(SetupProfile.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_phonelist);
Button done = (Button)dialog.findViewById(R.id.btn_done);
Button canCel = (Button)dialog.findViewById(R.id.btn_cancel);
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
MyCustomAdapter tst = new MyCustomAdapter(this,ContactName,ContactNumb);
PhoneListView.setAdapter(tst);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
canCel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
Please any help..
This line
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
Shouldn't that be like this:
ListView PhoneListView = (ListView)dialog.findViewById(R.id.list_phone);

How to close popup window?

I have an activity with custom popup window (quickaction style). There are some buttons leading to other activities. I want to close popup after pressing button (about or email button) in this popup (now when I go back popup appears again).
public class FirstActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code
Button quickButton = (Button) findViewById(R.id.button_quickaction);
quickButton.setOnClickListener(this);
final ActionItem about = new ActionItem();
final ActionItem email = new ActionItem();
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
QuickAction qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
}
}
Please, help.
Added:
I need something like this:
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qa.dismiss();
}
});
But qa cannot be resolved. Even if I add final to QuickAction qa = new QuickAction(v);.
try finish() on button's onClick method.
updated:
QuickAction qa;
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
you can also put private QuickAction qa; at your activity.

Categories