I have an activity and i want it to display a website using webview. But i am not able to do it with my existing code.
(EDIT: I made the necessary changes as told by Nirmal Raj but now i am getting an error that "Cannot resolve method findViewById(int)" i have edited my code please look at it and identify the problem.)
BlogAcitivity.java
package thenerdimite.nuttybuddies;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class BlogActivity extends Fragment {
View myView;
WebView webView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_blog, container, false);
return myView;
webView = (WebView)myView.findViewById(R.id.blogview);
webView = (WebView)findViewById(R.id.blogview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
}
}
The error "Cannot resolve method findViewById(int)" coming with a red color. What changes should i do in this code so that the webview can work. Thanks!
I am not sure what you want but I will give you a sample of how to implement a webview.
Layout:
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webs"/>
Add this to the layout in which you want to show the WebView.
Java Code:
webView = (WebView)findViewById(R.id.webs);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
Since you are using fragments the only difference will be in findViewById() function. You have to call myView.findViewbyId().
Also don't forget to add this to the manifest
<uses-permission android:name="android.permission.INTERNET"/>