0

It was visible at first, but I don't know if it's related to the emulator, but my toast message is no longer visible. Can you help me?

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {
    private static final String PREFS_NAME = "MyPrefsFile";
    private static final String KEY_FIRST_TIME = "first_time";

    private boolean isFirstTime() {
        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        boolean firstTime = preferences.getBoolean(KEY_FIRST_TIME, true);
        if (firstTime) {
            preferences.edit().putBoolean(KEY_FIRST_TIME, false).apply();
        }
        return firstTime;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (isFirstTime()) {
            Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
        }

        new Handler().postDelayed(() -> {
            Intent i = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }, 5000);



    }


}

my code is like this And as I said, the toast message appeared on the screen at first, but I don't know if I did something, but it no longer appears. Could it have something to do with xml files?

  • Does this answer your question? [Can an Android Toast be longer than Toast.LENGTH\_LONG?](https://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long) – Halil Ozel Mar 30 '23 at 21:56
  • you can use Log.d("ur tag","ur message"); to check if the toast had been called. – Alonso Mar 31 '23 at 01:46
  • The problem is the emulator. It was resolved after deleting and reinstalling. Thank you for your interest. – Kuzey Tekinoğlu Mar 31 '23 at 17:57

2 Answers2

1

By default Toast messages are visible for few seconds.

As you are using Toast.LENGTH_SHORT it will show for short time. If you want to have for a little longer you can use Toast.LENGTH_LONG.

And for custom duration use setDuration() method of toast.

Kamal Nayan
  • 1,635
  • 1
  • 5
  • 19
0

It seems like an emulator issue. Your toast should be displayed in any case based on code you provided.

Try to:

  1. Cold boot emulator
  2. Test on real device
  3. Delay toast showing

Note: never use splash screen in this way. It's designed to be something like loading screen, not with fixed timer.