Newbie on mobile apps.
I'm trying to use webview on my android app. But there is a problem in my code. I used the example from https://developer.android.com/ but its now working somehow. Can you please check my code....
WebView myWebView = (WebView) findViewById(R.id.webview);
android says "cannot resolve symbol webview"...
whole code is here ->
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webview); // here is the problem
myWebView.loadUrl("http://www.example.com");
Check if you have a WebView in your activity_main.xml with an id attribute with value #+id/webview like in the example below:
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Make following changes to your webView in xml:
<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"/>
Related
I have an app and i want to open the URL with Reading Mode
Without ads or something the reader don't need to see
How can i do that haw can i enable the reading mode on in kotlin ?
Try it with WebView,
In .xml file add this code:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
And inside activity(or fragment)
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
I am trying to create an android app which will do nothing but would just redirect to the website . Now after opening the app if any button is clicked on it opens up in crome . How to stop that .
below is the onCreate() function in my Main Activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);`
below is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
It works with me.
Add this settings on your webView:
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new myWebViewClient());
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
And append this add this private class:
private class myWebViewClient extends WebViewClient {
}
I have made an webview app. but it doesn't work.
He is stuck when i test it on an android device
FullscreenActivity.java
package com.solidos.neshaniha;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class FullscreenActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView.loadUrl("http://www.mywebsite.nl/");
}
}
activity_fullscreen.xml
<FrameLayout 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="#0099cc"
tools:context=".FullscreenActivity" >
</FrameLayout>
Who can help me?
Thanx
You don't have a webview in there at all. Change your layout to:
<FrameLayout 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="#0099cc"
tools:context=".FullscreenActivity" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Then in your Activity do:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://m.neshaniha.org/");
}
Also make sure this is in your manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
You didn't initialize the webview.
Like this :
private WebView webView;
webview = (Webview)findViewById(R.id.webview1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.loadURL("nananan");
Add webView in xml as below:
<FrameLayout 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="#0099cc"
tools:context=".FullscreenActivity" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Please initialize the WebView as below:
WebView webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://m.neshaniha.org/");
First go like that:
WebView myWebView = (WebView) findViewById(R.id.webview);
You should define the #+id of your WebView, like that:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
In your java code, you declared your WebView as a member variable, but you are not initialising it to anything. Therefore when you try to open the URL in it, you are getting a NullPointerException. There are two issues with your code.
First, you need to add the WebView to your layout:
<FrameLayout ...>
<WebView android:id="#+id/webview" ... />
</FrameLayout>
Then in your java code you need to find this webview and assign it to your variable before loading the url:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
webView = (WebView)findViewById(R.id.webview);
webView.loadUrl("http://m.neshaniha.org/");
}
I'm trying to create a simple Android application that is just a webview. I've been following a tutorial (http://www.mkyong.com/android/android-webview-example/) but have adapted it to have just a webview instead of a button that opens a webview.
When I include the code, I get an error on the line saying "webview cannot be resolved or is not a field". Any ideas on how to troubleshoot? Full code is below:
Error where line occurs:
setContentView(R.layout.webview);
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Change setContentView(R.layout.webview) to setContentView(R.layout.actvity_main), or rename you xml file to webview.xml
You can do the same using xml or programaticaly as below
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //should be activity_main
webView = (WebView) findViewById(R.id.webView1);//find id of the view defined in activity_main
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
OR
public class MainActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(MainActivity.this);// webview in mainactivity
setContentView(webView);// set the webview as the layout
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
if you are following the example as you said then in the android manifest file remove android:theme="#android:style/Theme.NoTitleBar" /> and try to run it.
I'm a beginner in learning Android, please help me with this.
I keep getting this error:
02-19 16:55:00.330: E/AndroidRuntime(2335):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.mkyong.android/com.mkyong.android.WebViewActivity}:
java.lang.NullPointerException
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView mywebview = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mywebview.getSettings();
mywebview.loadUrl("http://www.google.com");
}
my xml file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I think it's this line
webView.getSettings().setJavaScriptEnabled(true);
You never say what webView is. You declare mywebview, but not webView