Android’s Web View permits you to integrate a webpage as an area of the app. Web View comes with all the options that of a desktop browser like managing history, cookies, HTML 5 support and heap a lot of. victimization web view you'll be able to build very cool apps like group action HTML 5 games within the app.
In this article we tend to planning to learn the essential usage of Web View ranging from displaying a netpage to putting together an easy in-app browser that has some web site and search web address.
1:Create a android project in MainActivity.xml put below code for web browser design
main_activity.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="10dp"
tools:context="com.example.aaru.mybrowser.MainActivity">
<EditText
android:id="@+id/editSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="https://www.google.com"
android:inputType="none" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:backgroundTint="@color/colorAccent"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/editSearch"
android:text="Search" />
<Button
android:id="@+id/buttonGoogle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#f75252"
android:layout_marginTop="96dp"
android:text="Google"
android:layout_below="@+id/buttonSearch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/buttonFacebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="58dp"
android:text="Facebook"
android:layout_below="@+id/buttonGoogle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:backgroundTint="#45c2c9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/buttonGmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:backgroundTint="#b34fda"
android:layout_alignParentStart="true"
android:layout_below="@+id/buttonFacebook"
android:layout_marginTop="60dp"
android:text="Gmail" />
</RelativeLayout>
package com.example.aaru.mybrowser;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button buttonSearch,buttonGoogle,buttonFacebook,buttonGmail;
EditText editSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editSearch=(EditText)findViewById(R.id.editSearch);
buttonSearch= (Button) findViewById(R.id.buttonSearch);
buttonGoogle= (Button) findViewById(R.id.buttonGoogle);
buttonFacebook= (Button) findViewById(R.id.buttonFacebook);
buttonGmail= (Button) findViewById(R.id.buttonGmail);
//all id get
buttonSearch.setOnClickListener(this);
buttonGoogle.setOnClickListener(this);
buttonFacebook.setOnClickListener(this);
buttonGmail.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,WebViewActivity.class);
if (v.equals(buttonSearch)){
String url=editSearch.getText().toString();
i.putExtra("k1",url);
startActivity(i);
}else if (v.equals(buttonGoogle))
{
i.putExtra("k1","https://google.co.in");
startActivity(i);
}else if (v.equals(buttonFacebook)){
i.putExtra("k1","https://facebook.com");
startActivity(i);
}else if (v.equals(buttonGmail)){
i.putExtra("k1","https://mail.google.com");
startActivity(i);
}
}
}
2.Create empty activity name is webview activity put below code in web_view_activity.xml and WebViewActivity.java
<?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"
android:orientation="vertical"
tools:context="com.example.aaru.mybrowser.WebViewActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
></WebView>
</LinearLayout>
package com.example.aaru.mybrowser;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebViewActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView=(WebView)findViewById(R.id.webView);
String url=getIntent().getStringExtra("k1");
if (url.contains("https://")){
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
//let's run it
//Give a Internet Permission in manifests file
}else {
Toast.makeText(this, "Something Went wrong", Toast.LENGTH_SHORT).show();
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Enjoy Coding<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
No comments:
Post a Comment