In this video, I am going to show How to Convert a Website into an Android Application using Android Studio. In this tutorial, we will learn How to Convert a Website into an Android Application using Android Studio. So let's create a project.
Step 1 – Create a new Android project.
Provide Activity name as EasyOnlineConverter
Step 2 – Add a webView to your activity
Code Link Github: https://github.com/sahilgulati007/WebsiteToApp
#androidAppDevelopment
#websiteToAndroidApp
#AndroidStudio
----------------------------------------
AndroidManifest.xml
-----------------------------------------<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.polkapuffs.polkapuffs"> <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" android:usesCleartextTraffic="true"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
-------------------------------
MainActivity.java
------------------------------
package in.polkapuffs.polkapuffs; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.provider.Settings; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; import java.net.URISyntaxException; public class MainActivity extends AppCompatActivity { // @Override // protected void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // } WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // getActionBar().hide(); wv=findViewById(R.id.webview1); if(isNetworkAvailable()){ //Toast.makeText(this,"available",Toast.LENGTH_LONG).show(); //wv.setWebChromeClient(new WebChromeClient()); wv.setWebViewClient(new MyWebViewClient()); wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl("http://polkapuffs.in/"); } else { Toast.makeText(this," Internet not available",Toast.LENGTH_LONG).show(); Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent); while (true){ if(isNetworkAvailable()){ //wv.setWebChromeClient(new WebChromeClient()); wv.setWebViewClient(new MyWebViewClient()); wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl("https://polkapuffs.in/"); break; } } } } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("intent://")) { try { Context context = view.getContext(); Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); if (intent != null) { view.stopLoading(); PackageManager packageManager = context.getPackageManager(); ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (info != null) { context.startActivity(intent); } else { String fallbackUrl = intent.getStringExtra("browser_fallback_url"); view.loadUrl(fallbackUrl); // or call external broswer // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl)); // context.startActivity(browserIntent); } return true; } } catch (URISyntaxException e) { } } return false; } } private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN){ switch(keyCode) { case KeyEvent.KEYCODE_BACK: if(wv.canGoBack() == true){ wv.goBack(); }else{ finish(); } return true; } } return super.onKeyDown(keyCode, event); } }
------------------------------activity_main.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:layout_height="match_parent" tools:context=".MainActivity"> <WebView android:id="@+id/webview1" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
----------------------------styles.xml
----------------------------
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
No comments:
Post a Comment