Dialog start only the first time on startup? - java

I tried to make a dialog on startup of my application like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hi Stackoverflow!").create().show();
}
But I want to make it so that if I click a checkbox in the dialog "Don't show again" then the second time I start the application the dialog doesn't appear. How can I do it?

If you just want a Dialog with a Button then you don't really need an AlertDialog. You can simply create a Dialog, create a layout for the Dialog in xml with the CheckBox and Button, then use setContentView() on the Dialog.
To not show the Dialog again simply create a boolean variable and use onCheckedChanged() to set that variable to true if the checkbox is checked. Save that in SharedPreferences and check that value on start up.
Good example of getting started with SharedPreferences
SharedPreferences complete docs

You can use SharedPreferences for this. Add a flag that tells you if the dialog has been shown before (or if the app has been launched before) and determine from there whether to show the dialog or not.

Related

Dismiss Popup Window on Touch Outside

I'm trying to open up a pop-up window with 4 buttons on it that will dismiss when a button is pressed or when the user clicks outside of the pop-up window. I would just make an alert dialogue, but that will only support 3 buttons.
There have been a lot of questions about this same thing, and I can't find any consistent answer or any answer that works for me (including the deprecated Bitmap Drawable). I've put all of the suggestions I've seen into my code, but to no avail.
Here's everything I've used so far:
//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);
PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
I've tried to find everything I can, like keeping setFocusable before showAtLocation, but when I run the app, nothing happens when I click. Figured it might be something individual to my code, since I'm new and don't really know what I'm doing.
don't know what you really want to do...
if you want show dialog when click button
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do like show dialog
}
});

Android Studio - DatePicker in AlertDialog, null object reference

I have an activity in my app, where I want the user to pick the date from a DatePicker, which is contained in an AlertDialog. In the AlertDialog, I have set a view, to an xml layout file (, which contains only a LinearLayout, with a single DatePicker.
The code is really simple, and looks like this, just below onCreate().
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.layout.activity_alertdialog_date);
DatePicker datePicker = (DatePicker) findViewById(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show()
The layout shows up in the AlertDialog, and that part works great.
However, when I try to add this line, I get a null object reference error.
datePicker.setMinDate(System.currentTimeMillis() - 1000);
Here is the error message.
Attempt to invoke virtual method 'void
android.widget.DatePicker.setMinDate(long)' on a null object reference
How can I fix this, or improve my code in another way?
I really appreciate all the help I can get. Thanks!
Your problem is that your findViewById is looking in the wrong place for the DatePicker view. Calling findViewById in an activity will call it on the Activity's layout hierarchy, not the dialog's layout. You need to inflate the layout for the alert dialog first, and then get a reference to the view. This can be acheived in a couple ways.
Probably the easiest is to inflate the view and get the reference before showing the dialog:
View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_alertdialog_date, false);
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.Activity_AlertDialog_SetStartDate);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(dialogView);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show();
You could also get the view from the alert dialog after it's been created:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
AlertDialog dialog = alert.create();
dialog.show();
DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.Activity_AlertDialog_SetStartDate);

Prevent onClick while Activity is displaying Progress Dialog (Android)

An Activity I have makes an API call, while it is making this call the activity displays ProgressDialog.
My problem is that while the ProgressDialog is being displayed the page is still clickable, and if the user clicks on a clickable object during this, it crashes the app.
How can I disable the onClick functions of the activity while the ProgressDialog is running?
EDIT: I would like to do this without removing the ability to cancel the loading.
try using this
progressDialog.setcancelable(false);
try this for Your comment
You are trying to set just the Property of Activity. Instead Bring up
a dialog using ProgressDialog, so that Ui will not be accessible
ProgressDialog dialog = ProgressDialog.show(this, "", "Please wait...", true);
Edit: If you do not want to use ProgressDialog, get the Root Layout and call
layout.setEnabled(false)
Hope It Helps U
dialogue.setCanceledOnTouchOutside(false)

How to set imageButtons to VISIBLE at onResume() method?

I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()

Refreshing a button in Android

I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?

Categories

Resources