Android - WebView With FAB
WebView is a view that display web pages inside your application. You
can also specify HTML string and can show it inside your application
using WebView. WebView makes turns your application to a web
application.
Example
Here is an example demonstrating the use of WebView Layout. It
creates a basic web application that will ask you to specify a url and
will load this url website in the WebView.
Following is the content of the modified main activity file
src/Splash.java.
package com.hfen.app.webviewapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class splashscreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(2000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
//METHOD 2
/*
new Handler().postDelayed(new Runnable() {
// Using handler with postDelayed called runnable run method
@Override
public void run() {
Intent i = new Intent(MainSplashScreen.this, FirstScreen.class);
startActivity(i);
// close this activity
finish();
}
}, 5*1000); // wait for 5 seconds
*/
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
Following is the content of the modified main activity file src/MainActivity.java.
package com.hfen.app.webviewapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
import android.net.Uri;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import android.view.View;
public class MainActivity extends AppCompatActivity {
FloatingActionMenu materialDesignFAM;
FloatingActionButton floatingActionButton1, floatingActionButton2, floatingActionButton3, floatingActionButton4, floatingActionButton5, floatingActionButton6;
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
materialDesignFAM = (FloatingActionMenu) findViewById(R.id.social_floating_menu);
floatingActionButton1 = (FloatingActionButton) findViewById(R.id.floating_facebook);
floatingActionButton2 = (FloatingActionButton) findViewById(R.id.floating_twitter);
floatingActionButton3 = (FloatingActionButton) findViewById(R.id.floating_linkdin);
floatingActionButton4 = (FloatingActionButton) findViewById(R.id.floating_whats_app);
floatingActionButton5 = (FloatingActionButton) findViewById(R.id.floating_instagram);
floatingActionButton6 = (FloatingActionButton) findViewById(R.id.floating_youtube);
floatingActionButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu first item clicked
Intent facebookIntent = getOpenFacebookIntent(MainActivity.this);
startActivity(facebookIntent);
}
});
floatingActionButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu second item clicked
Intent twitterIntent = getOpenTwitterIntent(MainActivity.this);
startActivity(twitterIntent);
}
});
floatingActionButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu third item clicked
Intent linkdinIntent = getOpenLinkdinIntent(MainActivity.this);
startActivity(linkdinIntent);
}
});
floatingActionButton4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu first item clicked
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
String sharebody = "Your Link";
intent.putExtra(Intent.EXTRA_TEXT, sharebody);
startActivity(Intent.createChooser(intent, "Share via"));
//run share code
}
});
floatingActionButton5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu second item clicked
Intent instagramIntent = getOpenInstagramIntent(MainActivity.this);
startActivity(instagramIntent);
}
});
floatingActionButton6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO something when floating action menu third item clicked
Intent youtubeIntent = getOpenYouTubeIntent(MainActivity.this);
startActivity(youtubeIntent);
}
});
if (!isConnected(MainActivity.this)) buildDialog(MainActivity.this).show();
else{
}
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading Please Wait...",true);
mywebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings= mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("Your Link");
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.loadUrl(this.getIntent().getDataString());
mywebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
}
public static Intent getOpenFacebookIntent(Context context) {
try {
context.getPackageManager()
.getPackageInfo("com.facebook.katana", 0); //Checks if FB is even installed.
return new Intent(Intent.ACTION_VIEW,
Uri.parse("fb://page/108628529299989")); //Trys to make intent with FB's URI
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("FB LINK")); //catches and opens a url to the desired page
}
}
public static Intent getOpenTwitterIntent(Context context) {
try {
context.getPackageManager()
.getPackageInfo("com.twitter.android", 0); //Checks if Twitter is even installed.
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/")); //Trys to make intent with Twitter's's URI
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/")); //catches and opens a url to the desired page
}
}
public static Intent getOpenLinkdinIntent(Context context) {
try {
context.getPackageManager()
.getPackageInfo("com.linkedin.android", 0); //Checks if Linkdin is even installed.
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.linkedin.com/in/")); //Trys to make intent with Linkdin's URI
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.linkedin.com/in/")); //catches and opens a url to the desired page
}
}
public static Intent getOpenInstagramIntent(Context context) {
try {
context.getPackageManager()
.getPackageInfo("com.instagram.android", 0); //Checks if Instagram is even installed.
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.instagram.com/accounts/login/")); //Trys to make intent with Instagram's URI
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.instagram.com/accounts/login/")); //catches and opens a url to the desired page
}
}
public static Intent getOpenYouTubeIntent(Context context) {
try {
context.getPackageManager()
.getPackageInfo("com.google.android.youtube", 0); //Checks if YT is even installed.
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/")); //Trys to make intent with YT's URI
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/")); //catches and opens a url to the desired page
}
}
public boolean isConnected(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting())
{
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
else return false;
} else
return false;
}
public AlertDialog.Builder buildDialog(Context c) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("No Internet Connection");
builder.setMessage("You need to turn on your Internet Connection. Press OK to Exit Application");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
finish();
}
});
return builder;
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Are you Sure do you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Following is the content of the modified main activity file src/Splash.xml.
<?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:background="@drawable/splash_screen"
android:orientation="vertical"
android:weightSum="1">
android:weightSum="1">
</LinearLayout>
Following is the content of the modified main activity file src/Main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
tools:context="com.example.webviewapp.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/social_floating_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="11dp"
android:layout_marginRight="11dp"
android:layout_marginLeft="11dp"
fab:menu_animationDelayPerItem="100"
fab:menu_backgroundColor="@android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#da3c2f"
fab:menu_colorPressed="#dc4b3f"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_fab_label="Like Us"
fab:menu_fab_size="normal"
fab:menu_icon="@drawable/action_like"
fab:menu_labels_colorNormal="#333"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="6dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="8dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#f2f1f1"
fab:menu_labels_textSize="15sp"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66aff198"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="4dp"
fab:menu_showShadow="true">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="@drawable/action_fb"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_tw"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_linkdin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_in"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_whats_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_whatsapp"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_instagram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_insta"
fab:fab_size="normal" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_youtube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_yt"
fab:fab_size="normal" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>