Halil Sahin
Halil Sahin

Reputation: 603

How to add real Loading Screen

I have an android application of a pdf file inside. When I open my application on lower configuration devices (galaxy 2 etc.), it takes a while to load the PDF file. I tried to adding a splash screen in my application but when opening MainActivity.class after SplashScreen still taking same time. How can I develop a real loading screen? It should work on top of Main Activity layer. Here my codes:

AndroidManifest.xml

        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>
</manifest>

SplashScreen.class

public class SplashScreen extends AppCompatActivity {
    private GifImageView gifImageView;
    private ProgressBar progressBar;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        gifImageView=(GifImageView)findViewById(R.id.gifview);
        progressBar=(ProgressBar)findViewById(R.id.progressbar);
        progressBar.setVisibility(progressBar.VISIBLE);

        try {
            InputStream inputStream = getAssets().open("loading.gif");
            byte[] bytes  = IOUtils.toByteArray(inputStream);
            gifImageView.setBytes(bytes);
            gifImageView.startAnimation();
        }
        catch (IOException ex)
        {
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                    Intent intent = newIntent(SplashScreen.this,MainActivity.class);
                    SplashScreen.this.startActivity(intent);
                    SplashScreen.this.finish();
            }
        },12000);
    }
}

Here my codes how I can solve this problem?

Upvotes: 1

Views: 3731

Answers (5)

c__c
c__c

Reputation: 1612

Nowadays lots of applications are avoiding loading screen due to bad UI/UX. You can use alternative for content loading. This article may help more about. You can look this kind of implementation on lot of popular apps like Facebook, LinkedIn, Upwork etc.

Upvotes: 0

user7571182
user7571182

Reputation:

You can create a LoadingScreen instead of splash screen(looking at your 12 secs. delay). Here is something that might help:

public class LoadingScreen {private ImageView loading;

LoadingScreen(ImageView loading) {
this.loading = loading;
}

public void setLoadScreen(){
final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2, 
R.mipmap.loading_3, R.mipmap.loading_4};
final Handler loadingHandler = new Handler();
Runnable runnable = new Runnable() {
    int loadingImgIndex = 0;
    public void run() {
        loading.setImageResource(loadingImages[loadingImgIndex]);
        loadingImgIndex++;
        if (loadingImgIndex >= loadingImages.length)
            loadingImgIndex = 0;
        loadingHandler.postDelayed(this, 500);//change to accordingly(i.e. 12000)
    }
};
loadingHandler.postDelayed(runnable, 500); // change it accordingly
}}

In your MainActivity, you can pass a to the LoadingScreen class like this :-

private ImageView loadingImage;

Don't forget to add an ImageView in activity_main. After that call the LoadingScreen class like this;

 LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();

It is always helpful in creating a custom loading screen instead of Splash screen.Also don't forget to add some different images to make that feel like a gif and after that add the images in the res folder . I have checked it for lower versions too Therefore, believe that it might serve your purpose...

Upvotes: 0

Fragile
Fragile

Reputation: 51

You should give a shot to this article: https://antonioleiva.com/branded-launch-screen/ It's far better than Splash Screen, 'cause It loads async to your MainActivity

Add this code to styles:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
   <style name="AppTheme.BrandedLaunch" parent="AppTheme">
        <item name="android:windowBackground">@drawable/branded_logo</item>
   </style>

Now create branded_logo.xml into your drawable directory

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/background_material_light"/>
    </item>
    <item>
        <bitmap
            android:src="@drawable/launch_logo_image"
            android:tileMode="disabled"
            android:gravity="center"/>
    </item>
</layer-list>

And assign that style into your MainActivity

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.BrandedLaunch">

And finally add setStyle to your default theme in your onCreate in MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
...

Upvotes: 0

Nikit Prabhu
Nikit Prabhu

Reputation: 156

Have you considered using Async Task for this? You can use the async task in your MainActivity.

For Example you can do something like the following:

class PDFLoader extends AsyncTask<Void, Void, Void>
{
    protected void onPreExecute (){
        super.onPreExecute();
       // Here you can instantiate the Progressdialog to show the progress.

    }
    protected String doInBackground(Void...arg0) {
        // here you can go ahead write the reading logic for the PDF
       // if it is taking time.
    }
    protected void onProgressUpdate(Integer...a){
        super.onProgressUpdate(a);
       // you may or may not use this. This can act as a progress updated based on
       // the integer.
    }
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // Here you can dismiss the progress bar since the read as been done.
    }
}

Call it like this in your MainActivity:

new PDFLoader().execute();

I hope this helps you. This is just the basic structure to get you started. More information here.

Upvotes: 1

Ankit
Ankit

Reputation: 307

Using a Splash screen is not a solution to this.You are adding a screen with 12second gap and after 12 second the MainActivity class is getting execute. So, you can directly add a progress bar in MainActivity class and you can use AsyncTask to solve this problem. AsyncTask has 3 methods- OnPreExecute, doInBackground, OnPostExecute. Here, you can add a progress bar in OnPreExecute method and the code for opening PDF file in doInBackground method, and finally stop the progress bar in OnPostExecute method.

For more help you should provide your MainActivity class code here.

Upvotes: 0

Related Questions