Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if(null == savedInstanceState) {
- // set you initial fragment object
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.content, InicioPagina.newInstance());
- transaction.commit();
- }
- public class InicioPagina extends Fragment {
- private ProgressBar prg;
- SwipeRefreshLayout mySwipeRefreshLayout;
- public WebView myWebView;
- public static InicioPagina newInstance() {
- InicioPagina fragment = new InicioPagina();
- return fragment;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.iniciopagina, container, false);
- prg = (ProgressBar)rootView.findViewById(R.id.progressBar2);
- mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipelayout);
- myWebView = (WebView) rootView.findViewById(R.id.inicio_pagina);
- myWebView.loadUrl("http://example.com.br/");
- //*Ativar JavaScript
- WebSettings webSettings = myWebView.getSettings();
- webSettings.setJavaScriptEnabled(true);
- //*Forçar links para abrir no WebView ao invés do navegador
- myWebView.setWebViewClient(new WebViewClient());
- myWebView.setVerticalScrollBarEnabled(false);
- mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- myWebView.reload();
- mySwipeRefreshLayout.setRefreshing(false);
- }
- });
- myWebView.setOnKeyListener(new View.OnKeyListener()
- {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event)
- {
- if(event.getAction() == KeyEvent.ACTION_DOWN)
- {
- WebView webView = (WebView) v;
- switch(keyCode)
- {
- case KeyEvent.KEYCODE_BACK:
- if(webView.canGoBack())
- {
- webView.goBack();
- return true;
- }
- break;
- }
- }
- return false;
- }
- });
- return rootView;
- }
- public class WebViewClient extends android.webkit.WebViewClient{
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- prg.setVisibility(View.VISIBLE);
- super.onPageStarted(view, url, favicon);
- }
- @Override
- public void onPageFinished(WebView view, String url) {
- prg.setVisibility(View.GONE);
- super.onPageFinished(view, url);
- }
- }
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:showIn="@layout/app_bar_main"
- tools:context="br.com.example.example.MainActivity">
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <FrameLayout
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"></FrameLayout>
- </RelativeLayout>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Sem acesso a internet"
- android:textAlignment="center"
- android:textSize="30sp" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement