Sunday 8 November 2015

Professional Android WebView Application With Splash Screen and Share Button.



Features:

Having Splash Screen
Loading Animation before Website Loads into App
Share Button on The Action Bar
.
Requirements:
Windows or Mac Computer With
Android Studio & JDK 7 Installed
No need of Knowledge of Coding.

NOTE: In this guide i will be asking you to copy and paste code into your file. That indicates you first remove complete code particular file and paste my code.

Steps :
Open Android Studio and Select File=>New Project.
Follow Screens.....
Set Application Name and package name what ever you want.


Select Target SDK as per your Needs



Don't change these details
Now Navigate to MainActivity.java and double click on it.
Now Copy and Paste below Code into MainActivity.java file. Replace my project name with your's in 1st line of code. In Line No 27 replace "http://androidtechfreakat.blogspot.in/" with your url. Don't use www. prfix. paste in same same format you are looking. Next Change custom share data in red color with lines you want to sown share message in line numbers 71 & 72  and give your website url  in line 27th Line.
package com.androidwebviewapp.advait;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://androidtechfreakat.blogspot.in/");
mWebView.setWebViewClient(new MyAppWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Convert Website to Android Application");
intent.putExtra(Intent.EXTRA_TEXT," Vist https://androidtechfrakat.blogspot.in if you Want to Convert your Website or Blog to Android Application");
return intent;
}
}

MainActivity.java hosted with by GitHub                                                                            view raw


Now Right Click on your Project name udnder java folder and select create New Class and Name it Splash and copy and paste below code into Splash.java file. Replace my project name with your's in 1st line of code.

package com.androidwebviewapp.advait;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
@SuppressLint("NewApi")
public class Splash extends Activity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ActionBar actionBar = getActionBar();
actionBar.hide();
Thread t =new Thread(){
public void run(){
try{
sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent i =new Intent(Splash.this,MainActivity.class);
startActivity(i);
}
}
};
t.start();
}
@Override
public void onPause(){
super.onPause();
finish();
}
}
/**
* Created by Advait T on 05-Jul-15.
*/
 Splash.java hosted with by GitHub                                                                                  view raw

                            
Now Create one more java class file like above and name it as MyAppWebViewClient now copy and paste below code into MyAppWebViewClient.java file. And give your website url without any www or http prefixes as i gave there in line number 15 Replace my project name with your's in 1st line of code
package com.androidwebviewapp.advait;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Advait's on 19/5/2015.
*/
public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("androidtechfreakat.blogspot.in")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
MyAppWebViewClient.java hosted with by GitHub                                                            view raw
Now we are done with Java Files. If android studio shows any errors ignore them. All errors will be gone by the end of this tutorial.

Now Copy Below Code into activity_main.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:indeterminate="false"
android:layout_gravity="center" />
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</LinearLayout>
 activity_main.xml hosted with by GitHub                                                                        view raw

Now We Will Create new layout under folder layouts. For that Right Click on Layout and select new Xml File as shown in below figure. Name Layout as activity_splash.


Now Copy and Paste Below Code into activity_splash.xml.
  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/vert_loading"
tools:context=".Splash" >
</RelativeLayout>

activity_splash.xml hosted with by GitHub                                                                       view raw  


Now open menu_main.xml in menu folder and paste below code into it. 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/share"
android:title="@string/share"
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>

menu_main.xml hosted with by GitHub                                    view raw                                                                                                                                                               
Now Open AndroidManifest.xml file From manifests folder and copy below code into it. replace project name with your project name in the 3rd line.
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidwebviewapp.advait" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


AndroidManifest.xml hosted with by GitHub                          view raw            
 Now Finally Open Values Folder and place below code in strings.xml and change app_name string value to yours in line number two.


                 











                  
                         
     
                                  







  

<resources>
<string name="app_name">Android WebView App</string>
<string name="hello_world">Hello world!</string>
<string name="share">Share</string>
<string name="action_websearch">Web search</string>
</resources>

strings.xml hosted with by GitHub                                                                                    view raw

By this we had finished all coding Part.

Now open computer and navigate to your Project folder YourProjectName\app\src\main\res, there will be four folders with name mipmap change ic_launcher.png with your icon but don't change name and only png format are supported. And place an image file with name vert_loading.png in all folders. vert_loading.png will be your Startup screen.

At last our Project structure looks like below image...



Now go to buil in top menu of Android Studio and select Generate signed Apk and epxort your Application. After Succesfull export copy it to your phone and install. Or Upload to play store.


Most Thanks: Karthik M
















































12 comments:

  1. Well done Mr.Advait Thakur...........I would like to thanks for sharing your knowledge with us .Best of luck for ur future carrier .Rgds/Ravi Thakur

    ReplyDelete
  2. Well done Mr.Advait ....At the outset, I would like to thanks for sharing valuable information and ur knowledge . Best of luck for ur future carrier .Rgds/Ravi Thakur

    ReplyDelete
  3. can you pl. show how to handle custom exception when there is no internet connection and also to view other file formats for example pdf ..

    ReplyDelete
  4. can you pl. show how to handle custom exception when there is no internet connection and also to view other file formats for example pdf ..

    ReplyDelete
  5. Thanks for your effort.
    Is there any way so that the links open in the same window and not in external browser?

    ReplyDelete
  6. Hi, why my website cannot be opened in the app?

    ReplyDelete
  7. but my app which i made with these code does not loads up the checkout page in the app itself it open ups in the browser is there any solution for that

    ReplyDelete
  8. hi Advait, when i generate the app, i get this error message 4 times: Error:Tag attribute name has invalid character ';'.

    I copy your source, and replace in the project ¿i must replace or add? Thanks in advance

    ReplyDelete
    Replies
    1. Can you share your code pls..so that I can check what mistake you made...

      Delete
    2. thank you, i solved, the mistake was mine, i only paste your code, but without delete te code by default.
      I generate the app, it works, but my web its not displayed inside your app, it shows a message, for open chrome or the other navigator, it works like i push the chrome icon on my device ¿why? thanks

      Delete
  9. Finally it works perfectly, Thanks for share with us your knowledge, Advait.
    Best regards, and thank you.

    ReplyDelete