Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SplashActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //hide title bar of activity
- getWindow().requestFeature(Window.FEATURE_NO_TITLE);
- //Making activity full screen
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.activity_splash);
- int t =3000; //time for spash screen 3000 means 3sec
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- startActivity(new Intent(SplashActivity.this, MainActivity.class));
- }
- },t);
- }
- <ImageView
- android:layout_width="250dp"
- android:layout_height="250dp"
- android:src="@drawable/icon"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- public class MainActivity extends AppCompatActivity
- {
- String webAdress ="https://www.google.com/";
- WebView webView;
- FrameLayout frameLayout;
- ProgressBar progressBar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- webView = (WebView) findViewById(R.id.webView);
- frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- webView.setWebViewClient(new HelpClient());
- webView.setWebChromeClient(new WebChromeClient() {
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- frameLayout.setVisibility(View.VISIBLE);
- progressBar.setProgress(newProgress);
- setTitle("Loading..."); //when url is loading set this on actionbar
- if (newProgress == 100) {
- frameLayout.setVisibility(View.GONE); // Hide progress bar when page is loaded
- setTitle(view.getTitle()); //get amd set title of opend page
- }
- super.onProgressChanged(view, newProgress);
- }
- });
- webView.getSettings().setJavaScriptEnabled(true); //enable javaScript
- //check internet connection
- if (haveNetworkConnection()) {
- webView.loadUrl(webAdress);
- } else {
- Toast.makeText(this, "No internet Connection", Toast.LENGTH_SHORT).show();
- }
- progressBar.setProgress(0);
- }
- private class HelpClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- frameLayout.setVisibility(View.VISIBLE);
- return true;
- }
- }
- private boolean haveNetworkConnection(){
- boolean haveConnectedWifi = false;
- boolean haveConnectedMobile = false;
- ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
- for(NetworkInfo ni : networkInfos){
- if(ni.getTypeName().equalsIgnoreCase("WIFI"))
- if (ni.isConnected())
- haveConnectedWifi = true;
- if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
- if (ni.isConnected())
- haveConnectedMobile = true;
- }
- return haveConnectedWifi || haveConnectedMobile;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- //check if the event was the back button and if there's history
- if ((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
- webView.goBack();
- return true;
- }
- //if it was the back or there is no web page history, bubble up to the default system behaviour
- return super.onKeyDown(keyCode, event);
- }
- <!--Progressbar-->
- <FrameLayout
- android:id="@+id/frameLayout"
- android:layout_width="match_parent"
- android:layout_height="3dip"
- android:background="@android:color/transparent">
- <ProgressBar
- android:id="@+id/progressBar"
- android:layout_width="match_parent"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_height="7dp"
- android:layout_gravity="top"
- android:layout_marginTop="-3dp"
- android:progressDrawable="@drawable/custom_progress"
- android:background="@android:color/transparent"
- android:progress="20"/>
- </FrameLayout>
- <!--WebView-->
- <WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1">
- </WebView>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <!--internet permision-->
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".SplashActivity"
- android:theme="@style/Theme.AppCompat.Light.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>
- Intent intent = new Intent(this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- finish();
Add Comment
Please, Sign In to add comment