It's the following, I'm making an application that is a kind of webBrowser. In this application, I have a url written inside a .txt, and I need to load that URL into my WebView. Here's what I've done:
NOTE: I can already load a URL into my WebView by specifying it in the apk code itself as you can see in the code below.
activity_main.xml:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="50px"
android:layout_height="50px"
android:id="@+id/button"
android:background="@drawable/engrenagem"
android:layout_alignWithParentIfMissing="false"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="20px" />
MainActivity.java:
package com.abacoti.awser;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; ;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
String webURL = "http://www.google.com.br/";
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview);
if (savedInstanceState == null)
{
web.loadUrl(webURL);
}
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
}
@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
@Override
public void onBackPressed() {
}
}