Guest User

Untitled

a guest
Oct 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. public class SplashActivity extends AppCompatActivity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. //hide title bar of activity
  7. getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  8. //Making activity full screen
  9. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  10. setContentView(R.layout.activity_splash);
  11.  
  12. int t =3000; //time for spash screen 3000 means 3sec
  13. new Handler().postDelayed(new Runnable() {
  14. @Override
  15. public void run() {
  16.  
  17. startActivity(new Intent(SplashActivity.this, MainActivity.class));
  18. }
  19. },t);
  20. }
  21.  
  22. <ImageView
  23. android:layout_width="250dp"
  24. android:layout_height="250dp"
  25. android:src="@drawable/icon"
  26. android:text="Hello World!"
  27. app:layout_constraintBottom_toBottomOf="parent"
  28. app:layout_constraintLeft_toLeftOf="parent"
  29. app:layout_constraintRight_toRightOf="parent"
  30. app:layout_constraintTop_toTopOf="parent" />
  31.  
  32. public class MainActivity extends AppCompatActivity
  33. {
  34.  
  35. String webAdress ="https://www.google.com/";
  36. WebView webView;
  37. FrameLayout frameLayout;
  38. ProgressBar progressBar;
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42.  
  43.  
  44. super.onCreate(savedInstanceState);
  45. webView = (WebView) findViewById(R.id.webView);
  46. frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
  47. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  48.  
  49. webView.setWebViewClient(new HelpClient());
  50. webView.setWebChromeClient(new WebChromeClient() {
  51. @Override
  52. public void onProgressChanged(WebView view, int newProgress) {
  53. frameLayout.setVisibility(View.VISIBLE);
  54. progressBar.setProgress(newProgress);
  55. setTitle("Loading..."); //when url is loading set this on actionbar
  56. if (newProgress == 100) {
  57. frameLayout.setVisibility(View.GONE); // Hide progress bar when page is loaded
  58. setTitle(view.getTitle()); //get amd set title of opend page
  59. }
  60. super.onProgressChanged(view, newProgress);
  61. }
  62. });
  63.  
  64. webView.getSettings().setJavaScriptEnabled(true); //enable javaScript
  65.  
  66. //check internet connection
  67. if (haveNetworkConnection()) {
  68. webView.loadUrl(webAdress);
  69. } else {
  70.  
  71. Toast.makeText(this, "No internet Connection", Toast.LENGTH_SHORT).show();
  72. }
  73. progressBar.setProgress(0);
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. private class HelpClient extends WebViewClient {
  81.  
  82. @Override
  83. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  84. view.loadUrl(url);
  85. frameLayout.setVisibility(View.VISIBLE);
  86. return true;
  87. }
  88. }
  89.  
  90. private boolean haveNetworkConnection(){
  91. boolean haveConnectedWifi = false;
  92. boolean haveConnectedMobile = false;
  93.  
  94. ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  95. NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
  96.  
  97. for(NetworkInfo ni : networkInfos){
  98. if(ni.getTypeName().equalsIgnoreCase("WIFI"))
  99. if (ni.isConnected())
  100. haveConnectedWifi = true;
  101.  
  102. if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
  103. if (ni.isConnected())
  104. haveConnectedMobile = true;
  105.  
  106.  
  107. }
  108. return haveConnectedWifi || haveConnectedMobile;
  109. }
  110.  
  111. @Override
  112. public boolean onKeyDown(int keyCode, KeyEvent event) {
  113. //check if the event was the back button and if there's history
  114. if ((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
  115. webView.goBack();
  116. return true;
  117. }
  118.  
  119. //if it was the back or there is no web page history, bubble up to the default system behaviour
  120. return super.onKeyDown(keyCode, event);
  121. }
  122.  
  123. <!--Progressbar-->
  124. <FrameLayout
  125. android:id="@+id/frameLayout"
  126. android:layout_width="match_parent"
  127. android:layout_height="3dip"
  128. android:background="@android:color/transparent">
  129. <ProgressBar
  130. android:id="@+id/progressBar"
  131. android:layout_width="match_parent"
  132. style="?android:attr/progressBarStyleHorizontal"
  133. android:layout_height="7dp"
  134. android:layout_gravity="top"
  135. android:layout_marginTop="-3dp"
  136. android:progressDrawable="@drawable/custom_progress"
  137. android:background="@android:color/transparent"
  138. android:progress="20"/>
  139. </FrameLayout>
  140.  
  141. <!--WebView-->
  142. <WebView
  143. android:id="@+id/webView"
  144. android:layout_width="match_parent"
  145. android:layout_height="match_parent"
  146. android:layout_weight="1">
  147. </WebView>
  148.  
  149. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  150. <!--internet permision-->
  151. <uses-permission android:name="android.permission.INTERNET"/>
  152.  
  153. <application
  154. android:allowBackup="true"
  155. android:icon="@mipmap/ic_launcher"
  156. android:label="@string/app_name"
  157. android:roundIcon="@mipmap/ic_launcher_round"
  158. android:supportsRtl="true"
  159. android:theme="@style/AppTheme">
  160. <activity android:name=".SplashActivity"
  161. android:theme="@style/Theme.AppCompat.Light.NoActionBar">
  162. <intent-filter>
  163. <action android:name="android.intent.action.MAIN" />
  164.  
  165. <category android:name="android.intent.category.LAUNCHER" />
  166. </intent-filter>
  167. </activity>
  168. <activity
  169. android:name=".MainActivity">
  170.  
  171. </activity>
  172.  
  173. </application>
  174.  
  175. Intent intent = new Intent(this, MainActivity.class);
  176. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  177. startActivity(intent);
  178. finish();
Add Comment
Please, Sign In to add comment