Punch a web page in Android Application
In Android studio project we punch a web page in the android application. Here we use WebView for display a web page. WebView is a view that display web pages inside your application.
The WebView is helpful is when you want to provide information in your application that you might need to update. Such as an end-user agreement or a user guide. Within your Android application, you can create an Activity that contains a WebView, then use that to display your document that’s hosted online.
Another scenario in which WebView can help is if your application provides data to the user that always requires an Internet connection to retrieve data, such as email.
In this case, you might find that it’s easier to build a WebView in your Android application that shows a web page with all the user data, rather than performing a network request. Now parsing the data and rendering it in an Android layout.
Instead, you can design a web page that’s tailored for Android devices and then implement a WebView in your Android application that loads the web page.
Add a WebView in Android, XML code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mess_status.swapan.mymess.MainActivity"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </LinearLayout>
Add Java code:
public class MainActivity extends AppCompatActivity { private WebView myWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myWebView =(WebView)findViewById(R.id.webView); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.loadUrl("http://www.datainflow.com/"); myWebView.setWebViewClient(new WebViewClient()); } @Override public void onBackPressed() { if (myWebView.canGoBack()){ myWebView.goBack(); } else { super.onBackPressed(); } } }