This tutorial is to show how WebView works and how do we display html contents (eg. text, images...) in our own WebView.
If the user clicks on 'GO' button without entering any url then an empty string is passed through the intent (we have seen before how data is passed between activities) and otherwise if the user has typed some URL then the same url will be caught in the BrowserActivity.java and will be run.
User does not need to type http:// all the time as it is already concatenated to the dynamic URL.
----------------------------webview_activity.xml-------------------------
-------------------------browser_activity.xml-----------------------
----------------- WebViewActivity.java--------------------------
-------------------BrowserActivity.java--------------------
User does not need to type http:// all the time as it is already concatenated to the dynamic URL.
----------------------------webview_activity.xml-------------------------
<?xml version="1.0" encoding="utf-8"?>----------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4DB6AC"
android:gravity="center">
<TextView
android:layout_gravity="center"
android:textSize="30sp"
android:id="@+id/textView1"
android:background="@color/colorPrimary"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="#ffffff"
android:text="Enter a url:" />
<EditText
android:id="@+id/urlET"
android:background="#64FFDA"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="#000000"
android:textSize="25sp"
android:gravity="center"
android:hint="Enter Url here"
android:ems="10">
<requestFocus />
</EditText>
<Button
android:id="@+id/goBTN"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="30sp"
android:text="GO" />
</LinearLayout>
-------------------------browser_activity.xml-----------------------
<?xml version="1.0" encoding="utf-8"?>---------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/browser_WV"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
----------------- WebViewActivity.java--------------------------
package com.app.yourappname;-------------------------------------------------------------
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class WebViewActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.webview_activity);
final EditText enterUrlET = findViewById(R.id.urlET);
Button launchUrlBTN = findViewById(R.id.goBTN);
launchUrlBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BrowserActivity.class);
intent.putExtra("URL", ""+enterUrlET.getText().toString().trim());
startActivity(intent);
}
});
}
}
-------------------BrowserActivity.java--------------------
package com.app.yourappname;--------------------------------------------------------------------------
import android.os.Bundle;
import android.webkit.WebView;
import androidx.appcompat.app.AppCompatActivity;
public class BrowserActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_activity);
WebView webView= findViewById(R.id.browser_WV);
String receivedMsg = getIntent().getStringExtra("URL");
if(receivedMsg.equals(""))
webView.loadData("<html><b>Hello This is WebView's bold text.</b></>", "text/html", "UTF-8");
else
webView.loadUrl("http://"+receivedMsg);
}
}
Add network/internet permissions and declare both activity names in Manifest file
-------------------------------- Manifest file ---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BrowserActivity"/>
</application>
</manifest>
-----------------------------------------------------------------------------
No comments:
Post a Comment