How to load a webpage into webview in android app? - android

I am trying to make a simple android app that loads a webpage into a WebView.
package com.example.abhi.molinahealthcare;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Molina_HealthCare extends ActionBarActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
WebView myWebView = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_molina__health_care, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ACTIVITY:-
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I was not able to open it. I have the INTERNET permission declared in the AndroidManifest.xml

You are not initializing webview object of WebView which you are using in openURL method. do it by initializing webview instead of myWebView in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
webview = (WebView) findViewById(R.id.webview);
...

Related

YouTube only plays audio in WebView

https://drive.google.com/open?id=12rzhjzYvWTKGIQhlhP5OSCINhOAWCU73I want to show audio and video from YouTube using a Web View in Android, but on my mobile device I have a black screen while the video is playing. Audio is playing normally.
I give you two codes, one code that successfully displays audio and video (code 1). the second code (code 2) only successfully displays audio. why this happened? even though there isn't any change just the source of the different you_tube pages
code 1 :
package inducesmile.com.playvideoinandroid;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity {
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
mWebChromeClient = new MyWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://murid.gkdi.org/test/out.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (RelativeLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
#TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
#Override
public void onBackPressed() {
if (mCustomViewContainer != null)
mWebChromeClient.onHideCustomView();
else if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
This is my code 2 :
package inducesmile.com.playvideoinandroid;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity {
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
mWebChromeClient = new MyWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://murid.gkdi.org/test/in.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (RelativeLayout) findViewById(R.id.activity_main);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
#TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
#Override
public void onBackPressed() {
if (mCustomViewContainer != null)
mWebChromeClient.onHideCustomView();
else if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
use video api of youtube to play youtube videos in android app
https://developers.google.com/youtube/iframe_api_reference

Opening an activity from a fragment

I have a bottom navigation bar that switches between 5 fragments. One of these 5 buttons should open WebView (newsfeed) and display some HTML. The code that calls the newsfeed.xml is this:
public class newsfeedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false);
return v;
}
}
Now I followed a youtube tutorial to pull the HTML from a website (code below). In the tutorial, the code is in it's own activity and not called within a fragment. I am not sure how to combine these 2 code snippets to create the webview inside my fragment. Help would be much appreciated!
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.website.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I assume you want to load the webview into the fragment?
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false)
WebView webview = (WebView) v.findViewById(R.id.webview);
webview.loadUrl("http://www.website.com")
return v;
}
So inflate the Layout and afterwards call findviewbyid with your new layout and then you could do anything you want with your webview and it is loaded in your Fragment. Hopefully the solution for your question.
Take WebView in your newsfeed layout, and do the same thing in fragment.
You can use this code anywhere in the fragment to open the activity:-
startActivity(new Intent(getActivity(),ActivitytoOpen.class));

WebView controller Android

I am working on WebView. I don't know whether it's possible or not. I am opening an url inside of this view which is basically a login page. Now want to get the information whether user has successfully logged in or not. If yes then navigate to custom page (Android native) else keep on that page.
Thanks in advance.
We can achieve it via using this..
Create a function of javascript in the HTML page of Login Success/Failue HtmL page.
in this javascript function ,call your native Function by Using addJavascriptInterface:
package com.example.androidjsdemo;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
// Here you can Start WhatEver Activty launch///////
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webMy = (WebView) findViewById(R.id.webMy);
webMy.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings setting = webMy.getSettings();
setting.setJavaScriptEnabled(true);
//File file = new File("andoid");
webMy.loadUrl("file:///android_asset/mine.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
mine.html
<HTML>
<script>
function called()
{
// alert("hi");
Android.showToast("Hi Testing");
}
</script>
<Body>
Hi Sumit
<button onclick="called()">click me</button>
</Body>
</HTML>
activity_main.xml
<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="com.example.androidjsdemo.MainActivity" >
<WebView
android:id="#+id/webMy"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
When user complete successful singing he should redirect to other web page with url that indicate the login was successful. it can look something like http://www.yourwebsite.com/islogin?yes.
than all you need to do is to check in your app what the login status by verify that the url contain islogin and that it set to yes and do whatever you want.
here a small sample to help you begin:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isUserLoggedIn(url)){
doWork();
return true;
} else {
return false;
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
public void onPageFinished(WebView view, String url) {
}
});
For Example below is your login URL which you load in webview for login
String loginURL = "http://www.myurl.com/login";
Two possibility, when user click on login button
1) Success
Navigate to Other page as per web code. So we need to check it and open our native application screen.
2) Fail
Same login page but display error message according to user input.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(loginURL)) {
// Login Fail
return false;
} else {
// Login Success
// Open Android Native screen
return true;
}
}
});

How to get link in a webview and pass it on to the google doc viewer in android?

Okay so here is my main activity class.
In my WebView there are various links (mostly PDF). Now when a user clicks a link then what I'd like to do is i would like to load the URL in a variable then pass this variable to Google doc viewer code inside the WebView only to show the PDF contents.
package com.aeonindustries.skfgi;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("file:///android_asset/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
So how am going to do that?
Thanks in advance.
Ok, here some code:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// insert here code to handle your goal
return true;
}
And somewhere in your Activity:
webView.setWebViewClient(new MyWebViewClient());

Trying to create menu item links on android

Can't get it to link to webpages. "Reset" and "About" should do. Using webview. Need for the links to open within the webview.
here's the MainActivity.java:
package com.test.apppackage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("https://www.google.com");
setContentView(mWebview );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.menu_reset:
mWebview .loadUrl("https://www.bbc.co.uk");
setContentView(mWebview );
break;
}
return super.onOptionsItemSelected(item);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
here's the activity main.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_reset"
android:title="#string/menu_reset"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about"
android:showAsAction="never"
/></menu>
here's the strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">App name</string>
<string name="menu_reset">Reset</string>
<string name="menu_about">About app</string>
You are confusing android.R and R
Your items are called #+id/menu_reset, which is created in your com.test.apppackage.R.java file.
But you are later comparing to android.R.id.menu_reset, which would be in android.R.id, but doesn't exist.
You need to use only your R.java in this case. Use com.test.apppackage.R.id.menu_reset.
In some cases, you can use android values, but anyway mixing won't work.
edit
apparently, android.R.menu_reset doesn't even exist
more edit
R is actually R.id, of course

Categories

Resources