Adding a progress bar to android webview java - java

please i am very new in android development, i have my website which i load in webview and is working now i want to add a progress bar to show when a link is processing to render and hide the progress bar after page is loaded.
please i have see a lot of examples but i kept on getting error while trying to implement it, please can someone help me using me own webview for example.
package com.mypage.mypage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class SourceProjectActivity extends AppCompatActivity {
private WebView projectLoaderView;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_source_project);
projectLoaderView = (WebView)findViewById(R.id.sourcejailview);
WebSettings webSettings = projectLoaderView.getSettings();
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
//local offline loader
webSettings.setCacheMode( WebSettings.LOAD_DEFAULT );
projectLoaderView.loadUrl("http://google.com");
projectLoaderView.setWebViewClient(new WebViewClient());
SourceProjectActivity.this.progress.setProgress(0);
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
//super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
#Override
public void onBackPressed(){
if(projectLoaderView.canGoBack()){
projectLoaderView.goBack();
}else {
super.onBackPressed();
}
}
}
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app=""
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_source_project"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypage.mypage.SourceProjectActivity">
<WebView
android:id="#+id/sourcejailview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<ProgressBar
style="#android:style/Widget.Material.Light.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_x="124dp"
android:layout_y="43dp"
android:id="#+id/progressBar"
android:layout_height="wrap_content" />
</WebView>
</RelativeLayout>

WebViewClient doesn't have onProgressChanged() method. Instead of that you can use WebChromeClient and set progressbar as below:
private class MyWebViewClient extends WebViewClient {
public void onProgressChanged(WebView view, int newProgress) {
progress.setProgress(newProgress);
//Maybe you want to hide it once it reaches 100
if (newProgress == 100){
progress.setVisibility(ProgressBar.GONE);
}
}
}

Related

Autosuggestion is not enable on webview using websetting

I have found it impossible to correctly set the autocompletion of fields in html forms in an android studio webview:
activity_main.xml:
<?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=".MainActivity">
<RelativeLayout
android:id="#+id/container"
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">
<ProgressBar
android:id="#+id/progressbar_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:usesCleartextTraffic="true" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.webview;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private ProgressBar progressBar;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webview);
progressBar = findViewById(R.id.progressbar_id);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setGeolocationEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSaveFormData(true);
WebViewClient webViewClient = new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
myWebView.setVisibility(View.GONE);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
myWebView.setVisibility(View.VISIBLE);
}
};
myWebView.setWebViewClient(webViewClient);
myWebView.setSoundEffectsEnabled(true);
myWebView.loadUrl("https://tcg-wallet.ga/home");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
i have try using:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
but nothing happens there are no suggestions, I expected something like:
I don't know what I need to add to enable those suggestions!

How Can I program a notification push when my website public a new post?

I am programming an Android app with Java with Android Studio. My app shows the website on screen and I would like to notificate the articles I will publish in the future. How can I do this? I have OneSignal Push in my webpage, if it is useful for the process
MainActivity
package secretosdemalaga.com;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.onesignal.OneSignal;
public class MainActivity extends AppCompatActivity {
public WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
// OneSignal Inicializacion
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
// Ver Web en la app
setContentView(R.layout.activity_main);
mywebView = findViewById(R.id.am_webview);
WebSettings ajustes = mywebView.getSettings();
ajustes.setJavaScriptEnabled(true);
mywebView.loadUrl("https://educacioninformatica.es");
mywebView.setWebViewClient(new WebViewClient());
}
//Cerrar la app o volver atras
#Override
public void onBackPressed() {
if (mywebView.canGoBack()) {
mywebView.goBack();
} else {
super.onBackPressed();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/caparefresco"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/am_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

Why I can not do full screen

No full screen button appears on webview while watching a video, what's the cause?
I will not go to these matters please help.
I am sorry for my English
MainActivity.java
package net.aniplus.aniplus;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://youtube.com");
final ProgressDialog progressDialog=ProgressDialog.show(this, "Mobil Video İzle", "Bağlantınız Kontrol Ediliyor", true);
progressDialog.show();
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(MainActivity.this, "Bir hata oluştur!", Toast.LENGTH_SHORT);
progressDialog.dismiss();
}
});
}
}
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.aniplus.aniplus.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView" />
</RelativeLayout>
I am trying for hours but I didn't get it to work :(
On android manifest change or add Activity's theme.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"

Issues loading a webview and native youtube api video on same layoud

I'm working on a small app using YouTube API & webview to load the channel's chat, now there is no errors everything loads fine I can see the video I can see the chat fine but when I try to interact with the chat it doesn't seems to work, I have tried many things but nothing seems to work and I was wondering if anybody could give me a hand solving this issue.
LiveTube.java
package com.live.tube;
import com.live.tube.R;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class LiveTube extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "AIzaSyBMwV1heA9q55LybRCK5dXXTJWADRbmDh4";
private static final int RECOVERY_DIALOG_REQUEST = 1;
//Live Channel//";
public static final String VIDEO_ID = "eTWrGNka-ik";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_tube);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
//loadVideId
YPlayer.loadVideo(VIDEO_ID);
}
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient());
myWebView.clearCache(true);
myWebView.clearHistory();
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.loadUrl("https://www.youtube.com/live_chat?v=h98Psy5NZPo");
}
}
activity_live_tube.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="180dp" />
</FrameLayout>

My Blog App Has Been Crashed

here i have a question. I have already make my blog app. But, when i run this app, suddenly it crashed. Here my source code
package blog.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity
{
private WebView myWebView;
private WebSettings myWebSettings;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView=(WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://teknorats.blogspot.com");
WebSettings myWebSettings=myWebView.getSettings();
myWebSettings.getJavaScriptEnabled();
myWebView.setWebViewClient(new WebVewClient());
}
private static class WebVewClient extends WebViewClient {
public WebVewClient() {
}
}
public class WebAppInterface{
Context mContext;
WebAppInterface(Context c) {
mContext=c;
}
private WebAppInterface() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(new WebAppInterface(),"Android");
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
And this is the 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"
android:id="#+id/webview"
android:src="#drawable/tr_launcher"
>
</LinearLayout>
Please, i need your correction with that code.
If you ask where i got this code, i got this code from here http://developer.android.com/guide/webapps/webview.html
It seems that in your xml file LinearLayout tag has id webview and you are trying to get
cast LinearLayout to WebView.
So change
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:src="#drawable/tr_launcher"
>
</LinearLayout>
to
<Webview xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview">
</Webview>
and you has already defined myWebView as class variable so change
WebView myWebView=(WebView) findViewById(R.id.webview);
to
myWebView=(WebView) findViewById(R.id.webview);

Categories

Resources