A
A
Alexander Urich2017-09-18 17:35:27
Android
Alexander Urich, 2017-09-18 17:35:27

how to make a splashscreen in android

There is a site for mobile devices. I'm making an android webview application for it.
When you open the application, a white screen appears for a few seconds, then the site itself opens.
I encountered android building for the first time. It became interesting, I decided to do it myself.
I googled on the Internet and it seems that I even googled something necessary. Opens my splashscreen but, about a second before opening the splash screen and somewhere half a second after the splash screen closes, there is still a white screen.
I already tried to paint over the screen with color, but it did not work.
In res/drawable is splash.jpg splash itself and splash_bg.xml background style file
Here is my code:
splash_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient android:angle="90" android:endColor="#292d20"  android:startColor="#292d20" android:type="linear" />
        </shape>
    </item>
</selector>

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splash_bg"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/splach_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_bg"
        android:visibility="visible">

        <ImageView
            android:id="@+id/splashScreen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:autoSizeTextType="uniform"
            android:background="@drawable/splash" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/main_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/splash_bg"
        android:orientation="vertical"
        android:visibility="invisible">
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/splash_bg"
                android:visibility="visible" />
        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>
</LinearLayout>

MainActivity.java
package com.example.urichalex;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView webView;
    private Activity activity;
    private SwipeRefreshLayout swipe;

    public MainActivity()
    {
        activity = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                new AlertDialog.Builder(activity)
                        .setTitle("Confirm action")
                        .setMessage(message)
                        .setPositiveButton(
                                android.R.string.ok,
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        result.confirm();
                                    }
                                }
                        )
                        .setNegativeButton(
                                android.R.string.cancel,
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        result.cancel();
                                    }
                                }
                        )
                        .create()
                        .show();
                return true;
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                swipe.setRefreshing(false);
                MainActivity.this.setTitle(view.getTitle());
                if (findViewById(R.id.splach_screen).getVisibility() == View.VISIBLE) {
                    // show webview
                    findViewById(R.id.main_view).setVisibility(View.VISIBLE);
                    // hide splash screen
                    findViewById(R.id.splach_screen).setVisibility(View.GONE);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                // будут открываться внутри приложения
                if (url.contains("example.com")) {
                    return false;
                }
                // все остальные ссылки будут спрашивать какой браузер открывать
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                activity.startActivity(intent);
                return true;
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                webView.loadUrl("file:///android_asset/error.html");
            }
        });
        webView.loadUrl("http://example.com/");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
            this.webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mr_serg77, 2017-09-18
@Urichalex

https://habrahabr.ru/post/312516/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question