how help screen show for one time only in android - android

in the following code work properly and show help screen when open activity but I want show one time forever,
what can i do?
What should I add in the code?
my code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
}

You can use SharedPrefereces to set a variable that will check if you've shown the dialog yet to the user or not, here's an example:
SharedPreferences prefs = this.getSharedPreferences("com.you.app", Context.MODE_PRIVATE);
Boolean dialogShown = prefs.getBoolean("dialogShown", false);
Then check if the value of dialogShown is false (you don't need to set it first since it will default to false the way we are calling it), then on the following code we execute some code, only if dialogShown is false, meaning we can do all the dialog stuff inside that conditional:
if(!dialogShown){
//Your show dialog code
prefs.edit().putBoolean("dialogShown",true).commit();
}
So the next time we check for the dialogShown value on the shared preferences it will be true therefor not showing the dialog. I believe this is the most common way of doing it.

There is a solution ..
when application first time start then save the shared preference to the app..
Now each and every time You retrieve the shared preference and check if it is there then move to next screen

Use this code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
Boolean showOneTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
if (showOneTime == false) {
return;
}
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
showOneTime = false;
}
}

Related

How to stop Activity attached AlertDialog keep reappearing over Activity after Activity paused and recreated?

I am working on project, which simply validates through username and password.
I made some progress with using DialogFragments and AlertDialog. AlertDialog appears after starting the app over the mainactivity asking for username and password.
I must set the Alertdialog's setCanceledOnTouchOutside(false) and DialogFragment's setCancelable(false) because I don't want the users to dismiss it with pressing android's back button.
The problem is, after dismissing it programatically on successful login, if the activity becomes invisible and visible again , the Alertdialog's OnShowListener called, showing this AlertDialog again.
Can I somehow "detach" this AlertDialog from Activity? This popups also happen after unlocking the screen and getting back to activity which makes it very annoying...
Here is the code of interest:
MainActivity
public class MainActivity extends AppCompatActivity implements NoticeDialogFragment.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(GlobalInformations.getInstance().getUsername()==null){
shownoticeDialog();
}
}
public void shownoticeDialog(){
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
}
#Override
public void onDismiss(DialogFragment dialog) {
//set the username on a TextView instance, etc...
}
NoticeDialogFragment extends DialogFragment
public class NoticeDialogFragment extends DialogFragment {
public interface NoticeDialogListener{
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
public void onDismiss(DialogFragment dialog);
}
NoticeDialogListener mListener;
static Activity activity = null;
//static String username;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activity = (Activity) context;
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e){
throw new ClassCastException(activity.toString() + "must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
final AutoCompleteTextView actv_username = (AutoCompleteTextView) view.findViewById(R.id.username);
final EditText password = (EditText) view.findViewById(R.id.password);
getavailableusernames(actv_username);
final AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(getContext(), R.style.AlertDialogCustom))
.setView(view)
.setTitle("Login")
.setPositiveButton("OK", null)
//.setNegativeButton("Cancel", null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
final Button button =((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String passw = password.getText().toString();
String user = actv_username.getText().toString();
try{
if(user.length()<4 || passw.length()<4){
Toast.makeText(getContext(), "Username/password too short", Toast.LENGTH_SHORT).show();
dialog.show();
}
else {
//login to account, if success dismiss.
login(user, passw,dialog);
}
} catch(Exception e){
}
// dialog.dismiss();
}
});
}
});
dialog.setCanceledOnTouchOutside(false);
// set the DialogFragment to make the dialog unable to dismiss with back button
// (because not working if called on the dialog directly)
this.setCancelable(false);
return dialog;
}
public void login(final String username, String password, final AlertDialog dialog){
boolean login_success = false;
//query the credentials
login_success = dosomesqlquery(username, password);
if(login_success){
dialog.dismiss();
}
}
//passing the handling to activity...
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
}
}
Thank you for your help and patience.
Well this is that kind of situation where I end up heading my desk continously.
The source of the problem was I called dialog.dismiss() which dismisses the dialog, BUT not the dialogfragment itself, so will never, ever dismissed, even if the dialog disappeared from screen. Placing this.dismiss() in NoticeDialogFragment's onDismiss or anywhere else after login succeded will let the application act as it should.
#Override
public void onDismiss(DialogInterface dialog) {
mListener.onDismiss(NoticeDialogFragment.this);
this.dismiss(); //will dismiss the DialogFragment. Yeeey!
}
Thank you for your time and answers as they helped me point out the real problem. I will modify the code based on your suggestions.
An easier way is to use a static variable in your activity using two steps.
Declare a global static boolean
private static boolean session = false;
Check if the boolean has changed and if not, set the boolean to true when the dialog is shown
public void shownoticeDialog(){
if(session)return;
DialogFragment dialogFragment = new NoticeDialogFragment();
dialogFragment.show(getFragmentManager(), "NoticeDialogFragment");
session = true;
}
Set the value when the activity goes background
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("authUser", GlobalInformations.getInstance().getUsername()==null)
}
and read it when it comes back
#Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null && savedInstanceState.containsKey("authUser")) {
boolean authUser = savedInstanceState.getBoolean("authUser", false);
if(authUser) {
//show or don't show dialog
}
}
}

Edit button in application doesn't save when edited

The EditButton of my app in Android Studio can be edited but once you have edited the texts it will not save when you exit the window. What to do?
public class BellPepperActivity extends AppCompatActivity {
TextView bpTextView;
AlertDialog dialog;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bell_pepper);
bpTextView = (TextView) findViewById(R.id.bpTextView);
dialog = new AlertDialog.Builder(this).create();
editText = new EditText(this);
dialog.setTitle("BELL PEPPER");
dialog.setView(editText);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "SAVE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
bpTextView.setText(editText.getText());
}
});
bpTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(bpTextView.getText());
dialog.show();
}
});
}
}
By window, I assume you Activity (the AppCompatActivity that you have created). To maintain state in Activities you have to learn about the activity lifecycle. Basically when you leave you have to save the instance state:
// invoked when the activity may be temporarily destroyed, save the instance state here
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(TEXT_VIEW_KEY, editText.getText());
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
and when you restore the state you do the same:
// This callback is called only when there is a saved instance previously saved using
// onSaveInstanceState(). We restore some state in onCreate() while we can optionally restore
// other state here, possibly usable after onStart() has completed.
// The savedInstanceState Bundle is same as the one used in onCreate().
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
editText.setText(savedInstanceState.getString(TEXT_VIEW_KEY));
}
Obviously you have to create a TEXT_VIEW_KEY as private final string at the top of your class:
private static final String TEXT_VIEW_KEY = "TEXT_VIEW_KEY";
Untested, but that should work for you now. For more advanced lifecycle handling learn about the Android Architecture Components, but that should wait until you understand the basic activity lifecycle in android App.

how to pop up a dialog box for the first time when i launched my app for the first time?

i want to pop up a dialog box when my app is open for the first time in a device.i want to show in popup box that how to use the app.
if the app is opening for the first time it will show the dialog box ,otherwise it will simply avoid to show the dialog box.and the activities also will change according to the first use or normal use.for first time use it will show a activity1 or else it will show activity2.please help me.
this is my activity which show a image when app is opened
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
// here i want to go to another activity acording to the first time use or normal time
}
}, 3000);
}
public class class_name extends AppCompatActivity {
public static final String MyPREFERENCES2 = "MyPrefs" ;
SharedPreferences sharedpreferences2;
public boolean isFirstRun;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_name);
new Timer().schedule(new TimerTask() {
public void run() {
checkFirstRun();
}
}, 3000);
}
public void checkFirstRun() {
System.out.println("its in check first run");
isFirstRun = getSharedPreferences("PREFERENCE2", MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
startActivity(new Intent(class_name.this, new_activity1.class));
getSharedPreferences("PREFERENCE2", MODE_PRIVATE)
.edit()
.putBoolean("isFirstRun", false)
.commit();
}
else{
startActivity(new Intent(class_name.this, new_activity2.class));
}
}
}
final String FIRST_TIME_KEY = "com.example.app.MainActivity.firstTimeKey";
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstTime = sp.getBoolean(FIRST_TIME_KEY, false);
if(isFirstTime) {
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean(FIRST_TIME_KEY, true);
edit.apply();
//show the dialog
}

Android OnClickListener not firing for button on separate Layout

I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);

starting an activity where user left it

I have the following code:
public class SplashScreenActivity extends Activity {
private boolean animated ;
private Handler handler1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isPreviouslyLoggedIn()) {
setContentView(R.layout.splash);
final TextView revolution=(TextView) findViewById(R.id.textView1);
final Button login=(Button) findViewById(R.id.loginButton);
final Button signUp=(Button) findViewById(R.id.signUpButton);
login.setOnClickListener(loginListener);
signUp.setOnClickListener(signUpListener);
if (!animated) {
animated = true;
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
ImageView image = (ImageView) findViewById(R.id.image);
TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);
slide.setDuration(1000);
image.startAnimation(slide);
handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
revolution.setVisibility(View.VISIBLE);
login.setVisibility(View.VISIBLE);
signUp.setVisibility(View.VISIBLE);
}
},1200);
}
}
else {
setContentView(R.layout.home);
Intent intent = new Intent(getApplicationContext(), PickUpActivity.class);
startActivity(intent);
}
}
When the user clicks on one of the buttons, it leads him to a different activity in the same app. However, when the user clicks back from the next activity, the animation is started again. How can I prevent the animation from showing again as i want it to occur only once when user opens the app?
You can use Application class.Declare a boolean var in it and set it "true" before starting second Activity.In your onCreate() of first Activity check this boolean and do animation only it is false(it means that user has not started second activity yet).For example create class with name App in your package:
public class App extends Application{
private static boolean animated;
#Override
public void onCreate() {
super.onCreate();
animated = false;
}
public static boolean getAnimated(){
return animated;
}
public static void setAnimated(boolean animated1){
animated = animated1;
}
}
Register App in manifest:
<application
android:icon= ...
android:label= ...
android:name="yourpackage.name.App" >
(I suppose that your package name is :"yourpackage.name")
Now change your code like this:
if (!App.getAnimated()) {
App.setAnimated(true);
revolution.setVisibility(View.INVISIBLE);
login.setVisibility(View.INVISIBLE);
signUp.setVisibility(View.INVISIBLE);
...
Or you can use sharedpreferences and retrieve a boolean from it when you want to start animation.You have to set it's default value "false" and when user start second Activity,you have to set it "true".
You can set a flag for this, first time keep the flag true and when the user clicks on any of the button, set the flag value to false.
Now start the animation if the flag value is true.

Categories

Resources