Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity
  2. implements NavigationView.OnNavigationItemSelectedListener {
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. if(null == savedInstanceState) {
  8. // set you initial fragment object
  9. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  10. transaction.replace(R.id.content, InicioPagina.newInstance());
  11. transaction.commit();
  12. }
  13.  
  14. public class InicioPagina extends Fragment {
  15.  
  16. private ProgressBar prg;
  17. SwipeRefreshLayout mySwipeRefreshLayout;
  18. public WebView myWebView;
  19.  
  20. public static InicioPagina newInstance() {
  21. InicioPagina fragment = new InicioPagina();
  22. return fragment;
  23. }
  24.  
  25. @Override
  26. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  27. View rootView = inflater.inflate(R.layout.iniciopagina, container, false);
  28. prg = (ProgressBar)rootView.findViewById(R.id.progressBar2);
  29. mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipelayout);
  30. myWebView = (WebView) rootView.findViewById(R.id.inicio_pagina);
  31. myWebView.loadUrl("http://example.com.br/");
  32.  
  33. //*Ativar JavaScript
  34. WebSettings webSettings = myWebView.getSettings();
  35. webSettings.setJavaScriptEnabled(true);
  36.  
  37. //*Forçar links para abrir no WebView ao invés do navegador
  38. myWebView.setWebViewClient(new WebViewClient());
  39.  
  40.  
  41. myWebView.setVerticalScrollBarEnabled(false);
  42.  
  43. mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  44. @Override
  45. public void onRefresh() {
  46. myWebView.reload();
  47. mySwipeRefreshLayout.setRefreshing(false);
  48. }
  49. });
  50.  
  51. myWebView.setOnKeyListener(new View.OnKeyListener()
  52. {
  53. @Override
  54. public boolean onKey(View v, int keyCode, KeyEvent event)
  55. {
  56. if(event.getAction() == KeyEvent.ACTION_DOWN)
  57. {
  58. WebView webView = (WebView) v;
  59.  
  60. switch(keyCode)
  61. {
  62. case KeyEvent.KEYCODE_BACK:
  63. if(webView.canGoBack())
  64. {
  65. webView.goBack();
  66. return true;
  67. }
  68. break;
  69. }
  70. }
  71.  
  72. return false;
  73. }
  74. });
  75.  
  76. return rootView;
  77. }
  78.  
  79. public class WebViewClient extends android.webkit.WebViewClient{
  80. @Override
  81. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  82. prg.setVisibility(View.VISIBLE);
  83. super.onPageStarted(view, url, favicon);
  84. }
  85. @Override
  86. public void onPageFinished(WebView view, String url) {
  87. prg.setVisibility(View.GONE);
  88.  
  89. super.onPageFinished(view, url);
  90. }
  91. }
  92.  
  93. <android.support.constraint.ConstraintLayout
  94. xmlns:android="http://schemas.android.com/apk/res/android"
  95. xmlns:tools="http://schemas.android.com/tools"
  96. xmlns:app="http://schemas.android.com/apk/res-auto"
  97. android:layout_width="match_parent"
  98. android:layout_height="match_parent"
  99. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  100. tools:showIn="@layout/app_bar_main"
  101. tools:context="br.com.example.example.MainActivity">
  102. <RelativeLayout
  103. android:layout_width="wrap_content"
  104. android:layout_height="wrap_content">
  105. <FrameLayout
  106. android:id="@+id/content"
  107. android:layout_width="match_parent"
  108. android:layout_height="match_parent"></FrameLayout>
  109. </RelativeLayout>
  110.  
  111. <TextView
  112. android:layout_width="match_parent"
  113. android:layout_height="wrap_content"
  114. android:text="Sem acesso a internet"
  115. android:textAlignment="center"
  116. android:textSize="30sp" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement