Convert my localhost website into android app - android

I want to create a web view of a localhost website or not online website how can I do that its picking online websites link but not localhost is there a way to do this?
I have tried giving the link using localhost and ip but it’s not picking my website.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.getJavaScriptEnabled();
mywebview.loadUrl("http://192.000.00.0/traveland/index.php");
mywebview.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed()
{
if(mywebview.canGoBack())
{
mywebview.goBack();
}
else
{
super.onBackPressed();
}
}
}
MainActivity.xml
<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"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
android:id="#+id/webview"/>
</RelativeLayout>```
It shows all the online websites not the offline ones how can that be done?

Your pc and android must be in the same network, once you have turned on your web server (WAMP, XAMPP, Etc.) configure the vitualhots, with the firewall disabled from your pc.

If your localhost is working well and you want to make it an app that connects with the server localhost through your android then download the "ngrok" on your PC. there are tons of tutorials that will teach you how to host localhost websites online once you do that you are going to paste that ngrok URL which is online version of your website and then name your app give your email and just submit like a normal form and you will be receiving email and there will be two links. one for public link sharing of your app and another is private app management which will allow you to change some functions of your app like URL
and others just change the URL to your localhost URL and then you will be able to access your contents of localwebsite without ngrok keep in mind if you change layout of your web application then you must update your app through same process again
link for making app with this website: https://gonative.io/

Related

WebView is clipping the webpage in android 4.4

WebView is clipping the webpage on Android 4.4. Here is how it looks:
It is opening correctly in google chrome. I want it to open like this in the webview.
Following is my code:
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/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView .getSettings().setLoadWithOverviewMode(true);
webView .getSettings().setUseWideViewPort(true);
webView.loadUrl("https://viftbox.com/");
}
}
For using a recent WebView engine on Android and you can't use Android System WebView or Google Chrome:
Prior to Android 5.0 improvements to the Android WebView was limited to OS upgrades/updates. So the recommendation was to use the NOW DEPRECATED Crosswalk Project as the WebView. While the site no longer exists, its ghost exists on archive.org I don't know if enough of the site survives there to be useful. This might be a solution if you are unable to change hardware and need more recent webkit features.
As the Crosswalk Project is no longer updated, or it doesn't have the features you need, I've learned that Mozilla has a GeckoView which wraps their Gecko render engine.
Note that according to Mozilla's documentation they have their own APIs so GeckoView is NOT a drop-in replacement for Android's WebView, so some effort must be made for integration.
Either way including a custom webview engine will add to the APK size.

Discord OAuth2 login Fail via Android Webview

I'm using discord to let the user login on my android app.
The code is as follows:
MainActivity.xml
<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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/webview_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<WebView
android:id="#+id/authWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
app:layout_anchorGravity="center" />
</FrameLayout>
DiscordOAuth.java:
package com.example.oauth2test;
import android.app.Activity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
class DiscordOAuth {
private static final String LOG_TAG = "Discord OAuth 2 Login";
public void getAuthentication(Activity activity) {
String authURL = "https://discordapp.com/login";
WebView authWebView = activity.findViewById(R.id.authWebView);
authWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
authWebView.getSettings().setAppCacheEnabled(true);
authWebView.getSettings().setLoadWithOverviewMode(true);
authWebView.getSettings().setUseWideViewPort(true);
authWebView.getSettings().setBuiltInZoomControls(true);
authWebView.getSettings().setSupportMultipleWindows(true);
authWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
authWebView.setWebViewClient(new WebViewClient());
authWebView.loadUrl(authURL);
authWebView.setVisibility(View.VISIBLE);
}
}
when the above is executed, you will get a login page, but when you try to loging, the loading animation will appear for a moment, and you will be back on the login page without even a success or a failure
after searching, i suspect that the problem has something to do with multiple windows and how to deal with them, but i can't be sure.
The final goal is to use oauth2 via a WebView to login to discord and get the username of the user
Implement AuthCallback interface in your class & you will get response in callback methods
Discord internally needs WSS connection to complete authorization. WSS connection requires DOM storage to store client information. However in Android webview, DOM storage is disabled by default. you can change this in WebSettings by webSettings.domStorageEnabled = true

Android App not connecting to internet on emulator

The app does not connect to the internet. I have
<uses-permission android:name="android.permission.INTERNET" />
in my manifest and I checked the other applications on the emulator for example youtube and google search works but the application isn't able to load the page. This is my main java file.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.loadUrl("https://en.wikipedia.org/");
}
}
and this is my main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.hoda.myapplication.MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webz"></WebView>
</android.support.constraint.ConstraintLayout>
this is the error i get
[ERROR:gl_surface_egl.cc(289)] eglChooseConfig failed with error EGL_SUCCESS
Try enabling javascript and override the webviewclient to avoid redirection:
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.setWebViewClient(new WebViewClient());
myWebview.loadUrl("https://en.wikipedia.org/");
and update the constraints of your WebView:
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/webz"></WebView>
The problem was with the emulator's graphics I believe as trying #diegoveloper 's solution on the emulator still did not load the page but trying it on an actual android device was successful.
Do this
Restart the emulator (as suggested in another post and in the comments of this post)
If that doesn't work, restart your device.
Worked for me.
Add activity in your AndroidManifest.xml
android:name="com.(your browser activity)"
android:screenOrientation="portrait">

Problem with webview not loading

I am having a bad experience with webview which does not load the web page which I request.
I cannot load google or any other page with a webview. I have put in xml:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_marginLeft="250px"
android:layout_marginTop="80px"
android:layout_width="180px"
android:layout_height="160dip"
/>
I then put in the code:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
This shows up stating the webpage is not available.
I have also added the permission to the manifest.
I have another activity within this application which loads a youtube url fine using:
startActivity(new Intent( Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=XS998HaGk9M")));// Starts an intent to watch the video
I'm not sure what this could be and really need advice on this as I need to get it working.
Thanks
Edit: I also cannot access any webpage within the actual emulator itself. By searching in the search bar within the emulator this says the same thing when connecting to Google.
I'm not sure why this would connect to youtube with an intent and not a webview
Edit: This is not even connecting to youtube now, it says the same as above. This is messed up as I need this to work for my project tomorrow. If the webview keeps going down this is not very reliable. I may have to change the device I'm working with as with android things keep going wrong.
Edit: I have just come back after a few hours without touching the code or the emulator and when I run the application the youtube video was back on and I can browse within the emulator. But I still cannot connect via webview. VERY UNRELIABLE :(
Make sure you have included Permissions for Internet Access
<uses-permission android:name="android.permission.INTERNET" />
I had the same problem. It seems to be solved when putting the webview code elsewhere than in the MainActivity.onCreate() method . For example, put the code containing webview.loadUrl(…) into the onClick method of a button. The WebView appears empty on launching the activity, and correctly filled when clicking the button .
Does the emulator have internet access? I have noticed similar behavior within the emulator at times and it is due to the emulator not starting up properly. The only work-a-round I've been able to come up with is to restart the emulator until it has internet access (usually one or two times).
-Dan
package com.Example.Browser;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.google.com/"); //Set Home page
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback on touch
//mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
//mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls
//mWebView.getSettings().setDisplayZoomControls(false);//Always Hide Zoom Controlls(Requires Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)
mWebView.requestFocus(View.FOCUS_DOWN);//Enable WebView Interaction
//mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
//public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {//Open URL on Error
//mWebView.loadUrl("http://www.google.com");//Open URL on Error
//mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show Offline HTML file or Image on Error
// }
// });
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<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"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Load an SWF into a WebView

I'm having problems with this. If I go to an SWF directly in the browser, it works fine. If I attempt to use loadUrl on an SWF file it stays blank and loads nothing.
Figured it out. You have to enable plugins.
webview.getSettings().setPluginsEnabled(true);
Niky, you have a code example here.
I have used this example to test this code and confirm it works. In this example the qualibus.swf is in contained within the assets of the app. Please test this on an actual device, as on the emulator it show a blank page (probably the flash player is not present on the emulator)
Test3Activity.java:
package com.blabla.test3;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result:
The function WebView.getSettings().setPluginsEnabled(); method has
been deprecated since API level 9, and was removed in API level 18.
You can use the newer function
WebView.getSettings().setPluginState(WebSettings.PluginState.ON);
which was added in API level 8 and was deprecated in API level 18.
According to the WebSettings Documentation API levels beyond 18 will
not support plugins; I'm assuming it's because the main plugin to
support was flash which adobe is no longer developing for mobile.
Quoted from source
So, for now you can use it till 18, and handle compatibility with higher APIs (sadly)

Categories

Resources