Good day to all,
I'm making a simple app(for practice purposes) that uses a WebView in android studio. I want my app to toast all the URL's that I enter/browse. Ex, When I open Facebook it will toast its URL and when I log-in to my account it will toast again my facebook account, sounds like that. Here's my code. I'm new to stuff thing, any suggestions or help will be highly appreciated. Thank you so much.
xml layout
<?xml version="1.0" encoding="utf-8"?>
<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.nevigrof.ymuc.Back_order">
<WebView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:id="#+id/webview1"/>
</RelativeLayout>
Java Codes
#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_survey_form);
webview = (WebView) findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webview.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
webview.loadUrl("http:/https://www.facebook.com/");
webview.getSettings().setJavaScriptEnabled(true);
String webUrl = webview.getUrl();
Toast.makeText(Back_order.this, webUrl, Toast.LENGTH_LONG).show();
}
}
I suppose MyWebViewClient inherits from WebViewClient; you should override
onPageStarted and/or onPageFinished in MyWebViewClient class.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Toast
}
Have a look to :
WebViewClient onPageFinished Android doc
Load an HTML document Android training
In your code, you are only displaying the toast once, when you first display facebook.com in the webview. You should add code to notify you if the webview has displayed another page (i.e. another URL). You can use WebViewClient.shouldOverrideUrlLoading(WebView view,String url) or other code to check if a new page is loaded/being loaded.
Related
I'm making an Android App with Java in Android Studio for my website using webview, but i've a lot of links which go out of website. How can i detect if link redirect from www.mywebsite.com to www.anotherwebsite.com and open it in another page intent?
You can use the code below.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
and create and inner class
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
You can find complete documentation here
I want to play a video in the WebView but somehow I get an blackscreen on the video but I can hear the sound of it. Would be really glad if someone can help me
public class MainActivity extends AppCompatActivity {
WebView displayYoutubeVideo;
TextView button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String frameVideo = "<html><body>Video From YouTube<br><iframe
width=\"560\" height=\"315\"
src=\"https://www.youtube.com/embed/WtO3_tF8niU\" frameborder=\"0\"
allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.youtube_view);
displayYoutubeVideo.getSettings().setUserAgentString("Mozilla/5.0
(Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
}
}
I guess you might be missing android:hardwareAccelerated="true" in the permissions of your activity in the manifest file. Since you are playing a flash video this should work out to make the WebView flash enabled. This is something that I encountered and the resolution was to add this permission.
This link may help you.It seems you missed some properties for the web view.
https://inducesmile.com/android/embed-and-play-youtube-video-in-android-webview/
You must activate Hardware acceleration if your android:targetSdkVersion is lower than 11 and have to set webChromeclient to your webview
displayYoutubeVideo.setWebChromeClient(new WebChromeClient());
and in your manifest file add below line
<application android:hardwareAccelerated="true" ...>
What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview
this will be a bit annoying to explain, but bear with me:
So i have a simple android app with buttons, one opens a facebook page (first checks if the app is installed, and will open in that, else will open same page in browser), the other opens a website url in the browser
The problem i'm having is that, instead of opening them as seperate applications, it seems to be opening the browser/fb app inside my original app.
So if i open the fb page through my app, and click the back button, it brings me back to my app's home screen. If i click the fb button, and minimize the app, and go into the ACTUAL fb app, it hasn't changed anything there.
So my app is not actually using the standalone fb app, just calling it up inside itself.
(hope that makes sense...) here is the code i used;
//just defined button variables
button facebook, shop;
//now for the onCreate method
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook = (Button) findViewById(R.id.facebook);
shop = (Button) findViewById(R.id.shop);
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{ //left out the profile id on purpose...
Intent facebook = new Intent(Intent.ACTON_VIEW, Uri.parse("fb://profile/xxxx"));
startActivity(facebook);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.facebook.com/example")));
}
}
});
The shop button is set up the same, only without the try/catch, it will only open in the browser.
How do i make it so that it sends the request to the actual fb app/browser, instead of what it's currently doing? Any help/tips are gratefully welcome :)
Create a new layout with layout/a.xml
Open the layout file & then add a WebView, assign an ID so you can access the widget later.
<?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/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Insert Permission in your manifest file as you are accessing INTERNET in your application
<uses-permission android:name="android.permission.INTERNET" />
Inside the new Activity -->WebActivity.java
public class WebActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.facebook.com");
}
}
If you want to handle navigation inside the webView on your own
mywebview.setWebViewClient(new WebViewClient())
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
Now to redirect from your main activity to these new activity ::
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
boolean isFacebookInstalled = true;
}
catch( PackageManager.NameNotFoundException e ){
isFacebookInstalled=false;
}
if(isFacebookInstalled)
{
//start the facebook app
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);
}
else
{
Intent facebook = new Intent(getApplicationContext(),WebActivity.class);
startActivity(facebook);
}
});
Same way you can also insert for your shop button
Right now when I push a specific button it starts an intent to open a web page into a webView, but instead it opens a Browser Application, revealing the address bar which is not what I want.
After doing some research I found out I can use the shouldOverrideUrlLoading() method to stop this from happening and just open the page in a webView, but I don't know how to implement this method in my code, help would be greatly appreciated!
This is what I have in the class where I'm opening the web page:
public class Codes extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.codes);
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.rgb(228, 108, 10));
}
}
WebView codes = (WebView)findViewById(R.id.codesWebView);
codes.getSettings().setJavaScriptEnabled(true);
codes.loadUrl("http://dev10.affirmaconsulting.com:24009/keyentry");
shouldOverrideUrlLoading(codes, "http://dev10.affirmaconsulting.com:24009/keyentry");
}
}
I had this same problem, that pages were not loading in my webview but instead via the browser application. I resolved the issue by implementing a WebViewClient class and overriding the shouldOverrideUrlLoading method.
WebViewClient webClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
};
webView.setWebViewClient(webClient);
The following link proved to be helpful:
http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/
We do this exact task in one of our activities and haven't ran into this issue. Here is what ours looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.document_viewer_activity);
WebView wv = (WebView) findViewById(R.id.documentViewerWebview);
wv.getSettings().setJavaScriptEnabled(true);
String url = getString(R.string.document_viewer_url) +
getIntent().getExtras().getString("media");
wv.loadUrl(url);
}
With the webview defined in the layout file looking like this:
<WebView
android:id="#+id/documentViewerWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Hope this helps.