Im trying to make a Dialog close when button is pressed.But Every time i press the button the application crashes.
public class CanvasPaint extends Activity implements OnClickListener {
final Button widthbtn = (Button) findViewById(R.id.widthbtn);
final Button widthpopBtn = (Button) findViewById(R.id.widthpopBtn);
final Context context = this;
final Dialog widthDialog = new Dialog(context);
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
widthDialog.show();
}
});
}
That's some inception code. Why are you setting a click listener inside a click listener? It should be more like
widthbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.show();
}
});
widthpopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
widthDialog.dismiss();
}
});
Your dialog layout is not properly inflated, and therefore, your widthpopBtn will be null. Try inflating the layout like this:
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
Button widthpopBtn = (Button) dialogView.findViewById(R.id.widthpopBtn);
Related
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();
}
});
I have bottom sheet dialog that displays when button clicked, so I have implemented all the logic for bottom sheet dialog into onClick method of button, how can I make separate class for showing bottom sheet dialog and just call method in onClick
Here is my code in onClick:
BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
View sheetView = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
NumberPicker minutePicker = (NumberPicker) sheetView.findViewById(R.id.np);
tv = (TextView) sheetView.findViewById(R.id.tv);
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();
minutePicker.setOnValueChangedListener(
new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal,
int newVal) {
tv.setText(Integer.toString(newVal));
}
});
}
To implement separation of concern and make your code modular
public class ClsBottomSheet{
public static TextView tv;
LayoutInflater inflater;
BottomSheetDialog mBottomSheetDialog;
public static NumberPicker minutePicker;
public ClsBottomSheet(Context context) {
mBottomSheetDialog = new BottomSheetDialog(context);
inflater = LayoutInflater.from(context);
}
public BottomSheetDialog showDialog(){
View sheetView = inflater.inflate(R.layout.bottom_sheet, null);
minutePicker = (NumberPicker) sheetView.findViewById(R.id.np);
tv = (TextView) sheetView.findViewById(R.id.tv);
Button btnOne = (Button) sheetView.findViewById(R.id.btn_one);
Button btnTwo = (Button) sheetView.findViewById(R.id.btn_two);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetDialog.cancel();
}
});
btnTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetDialog.dismiss();
}
});
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
mBottomSheetDialog.setContentView(sheetView);
minutePicker.setOnValueChangedListener(new ClsCallback());
return mBottomSheetDialog;
}
}
I'd rather not use Alert Dialog, but I will if I can set the positive Button to be the button I already have. If I can't do that, is there a way to set positive and negative Buttons in a custom dialog?
You can use the following custom alert dialog.
public class CustomAlertDialog {
public void showDialog(Context activity, String msg, String buttonText, final CustomDialogListener customDialogListener){ //one button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String positiveButtonText, String negativeButtonText, final CustomDialogListener customDialogListener){//two button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert_two_button);
TextView text = (TextView) dialog.findViewById(R.id.text_alert_two_dialog);
text.setText(msg);
Button positiveDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_YES);
Button negativeDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_NO);
positiveDialogButton.setText(positiveButtonText);
negativeDialogButton.setText(negativeButtonText);
positiveDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
negativeDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onNegativeButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String buttonText){ //simple alert without callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
The custom_alert.xml contains one button and one textView to display the message.
The custom_alert_two_button.xml contains two buttons and one textView to display the message.
Last one contains only one textview to display message.
i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}
Please don't answer already answered
I already asked this question, but I think I wasn't clear enough! My onClick methods for buttons that were obtained by inflating a layout are not working how do I fix this have I forgot to do something in my code?
Updated:
Now with the following code I get a nullpointerexception when I set the onClickListener what is wrong
Here is some of my code:
(note it is not complete you won't see onCreate!)
LayoutInflater inflater = LayoutInflater.from(getBaseContext());
View playv = inflater.inflate(R.layout.play, null);
Button updateLevel = (Button) playv.findViewById(R.id.updateLevel);
updateLevel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLevelOnClick(v);
}
});
View levelv = inflater.inflate(R.layout.level, null);
Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);
gotomenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
Andrew,
Try:
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.root_view, null);
Button updateLevel = (Button) v.findViewById(R.id.updateLevel);
updateLevel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLevelOnClick(v);
}
});
Button goBackMainMenu = v.inflate(R.layout.play, null).findViewById(R.id.tomenu);
goBackMainMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
this is assuming root_view is the View containing the Button objects.