How to Convert Any Website into a Professional Android App: Complete Source Code...
How to Convert Website into Android App: Complete Source Code...
So, Let's Get Started...
Learn the Complete Android Apps Development here. And In this Post, I am giving you the Complete Source Code of How to Convert Any Website into a Professional Android App. And If you want to learn it by Video Tutorials you can Checkout this. The Source codes and Video Tutorials are given below:
Complete Playlist for How to Convert Any Website into a Professional Android App with Complete Source Codes...
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="coding.apps.fiverr">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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">
<service
android:name=".MyFirebaseMessagingService" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
In MainActivity.java
package coding.apps.fiverr;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.Manifest;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar progressBar;
ProgressDialog progressDialog;
RelativeLayout relativeLayout;
Button btnNoInternetConnection;
SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.myWebView);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
swipeRefreshLayout.setRefreshing(false);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
progressBar = findViewById(R.id.progressBar);
progressDialog = new ProgressDialog(this);
btnNoInternetConnection = findViewById(R.id.btnNoConnection);
relativeLayout = findViewById(R.id.relativeLayout);
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
swipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.YELLOW);
if (savedInstanceState != null){
webView.restoreState(savedInstanceState);
} else {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
checkConnection();
}
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(final String url, final String userAgent, final String contentDisposition, final String mimetype, long contentLength) {
Dexter.withActivity(MainActivity.this)
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimetype);
String cookie = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookie);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading File... Please wait");
request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url,contentDisposition,mimetype));
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(MainActivity.this, "Downloading Started...", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(MainActivity.this, "Permission Denied... Allow from Setting to save", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
setTitle("Loading...");
progressDialog.show();
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);
progressDialog.dismiss();
setTitle(view.getTitle());
}
super.onProgressChanged(view, newProgress);
}
});
}
public void checkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
String webpage = "https://www.fiverr.com";
if (wifi.isConnected()) {
webView.loadUrl(webpage);
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
} else if (mobileNetwork.isConnected()) {
webView.loadUrl(webpage);
webView.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
} else {
webView.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
}
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure you want to exit?");
builder.setMessage("Subscribe this channel Android Tutorials to learn Android Development.");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNeutralButton("Rate Us", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.peace.TextScanner");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_forward:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_refresh:
checkConnection();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
}
In SplashScreen.java
package coding.apps.fiverr;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class SplashScreen extends AppCompatActivity {
TextView title, subtitle, credit;
Animation buttomAnim, sideAnim;
int SPLASH_TIMER = 3000; //for 3 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
//casting for textview
title = findViewById(R.id.title);
subtitle = findViewById(R.id.subtitle);
credit = findViewById(R.id.credit);
//Animation Casting
buttomAnim = AnimationUtils.loadAnimation(this, R.anim.buttom_anim);
sideAnim = AnimationUtils.loadAnimation(this, R.anim.side_anim);
//Set Animation Textview or we can say a layout
title.setAnimation(buttomAnim);
subtitle.setAnimation(sideAnim);
credit.setAnimation(buttomAnim);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIMER);
}
}
In MyFirebaseMessagingService.java
package coding.apps.fiverr;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static int NOTIFICATION_ID = 0;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
generateNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
private void generateNotification(String title, String body) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (NOTIFICATION_ID > 1073741824){
NOTIFICATION_ID = 0;
}
notificationManager.notify(NOTIFICATION_ID++, builder.build());
}
}
In buttom_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:fromXDelta="50%"
android:duration="1500"/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500"/>
</set>
In side_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%"
android:fromYDelta="0%"
android:duration="1500"/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500"/>
</set>
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="9dp"
android:layout_marginTop="-2dp"
android:progress="20"
android:visibility="gone" />
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ImageView
android:id="@+id/noConnectionLogo"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true"
android:src="@drawable/no_internet" />
<TextView
android:id="@+id/txtNoConnection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/noConnectionLogo"
android:text="No Internet Connection"
android:textAlignment="center"
android:textSize="26sp" />
<Button
android:id="@+id/btnNoConnection"
android:layout_width="140dp"
android:layout_height="55dp"
android:layout_below="@id/txtNoConnection"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="@color/colorAccent"
android:text="Retry"
android:textColor="#ffffff"
android:textSize="22sp" />
</RelativeLayout>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
In splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/colorPrimary"
android:layout_height="match_parent"
tools:context=".SplashScreen">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="@font/libre_baskerville_bold"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.354" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="@font/libre_baskerville_bold"
android:text="Best Freelancing Site"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="@+id/title"
app:layout_constraintStart_toStartOf="@+id/title"
app:layout_constraintTop_toBottomOf="@+id/title" />
<TextView
android:id="@+id/credit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="@font/libre_baskerville_bold"
android:gravity="center"
android:text="by \n Android Tutorials"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/subtitle"
app:layout_constraintStart_toStartOf="@+id/subtitle"
app:layout_constraintTop_toBottomOf="@+id/subtitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you have any Doubt or Error then Checkout the Playlist given above. And If Still, you have
Doubt or Error Comment below:
Thanks for Watching...
إرسال تعليق