Vision Coderz
Vision Coderz

Reputation: 8078

Android Flash screen loading white screen

I have following code in my Main Activity

public class MainActivity extends AppCompatActivity {



    ImageView my_flash_screen;
    DB db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        my_flash_screen= (ImageView) findViewById(R.id.my_flash_screen);



        MainTask task = new MainTask();
        task.execute();

       AnimationSet animation = new AnimationSet(true);
        animation.addAnimation(new AlphaAnimation(0.0F, 1.0F));
        animation.addAnimation(new ScaleAnimation(0.0f, 1, 0.0f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)); // Change args as desired
        animation.setDuration(1500);

        my_flash_screen.startAnimation(animation);

        new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {


                Intent i = new Intent(MainActivity.this, NavigationActivity.class);
                startActivity(i);
                finish();
            }
        }.start();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private class MainTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            if(FontUtil.storeSharedPreference(getApplicationContext()).getString("font", "").isEmpty())
            {
                FontUtil.savePrefs("font", "SKN.TTF",getApplicationContext());
                FontUtil.savesize("fb",22, getApplicationContext());

                Log.d("Test","insidefont");
            }

            try {
                if(db==null){
                    db=new DB(getApplicationContext());
                    db.createdatabase();
                    Log.d("Test","intialise first");
                }
                Log.d("Test","intialise second");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mynew"
    android:gravity="center"
    android:layout_weight="1"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/mynew"

        android:id="@+id/my_flash_screen"
        android:contentDescription="loading image" />
</LinearLayout>

Some times when i open my app only white screen will showing.i have used AsyncTask to copy database from asset folder to device and shared preference will set font type and size.Can any one help me to fix the issue.Thank You.

Before asking question i have read following questions

White background when Android app start up

white background for few seconds before splash screen android

Splash screen activity background color

Upvotes: 0

Views: 465

Answers (1)

Saveen
Saveen

Reputation: 4220

yes happened due to it doesn't recognise your image immediately.It took some time. Till that time it show white screen.

I'll give you idea how you can use and avoid this problem.

Put your image to style and use it as theme.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorText</item>
</style>

<!--SplashActivity theme.-->

<style name="Theme.Splash" parent="AppTheme">
    <item name="android:windowBackground">@drawable/ic_splash</item>
</style>

and use your xml simply blank

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

Then just use that theme in your splash activity.

<activity
        android:name=".module.splash"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

Second Option :

My first way is perfect but you want to use some animation or something else then you this style and use same layout which you set image in your layout.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">@color/kitean_primary_color</item>
    <item name="colorPrimaryDark">@color/kitean_primary_dark</item>
    <item name="colorAccent">@color/kitean_color_accent</item>
</style>

This is the best way to avoid this problem. Thanks

Upvotes: 1

Related Questions