Real time Notification Android Studio - java

I'm trying to create an APP from a Website. I've tried WebView in Android Studio [JAVA]. It worked fine with some minor issues. Now I want to implement Real time Pop up Notification in this App. But I don't know how to do it.
API of this Website:
https://api.v2.just.edu.bd/api/notices?page=null&ref=null&tag=&search=&limit=20
This is the code in main.java
package com.example.just;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://just.edu.bd/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}

Related

How to open another link in a Webview Android Application?

My website is having one external link which directs to api.whatsapp.com once you click on the chat with us floating button on my website.
I have created a Webview Android application that opens my website in it. But when I click on the Chat With Us button then it gives me the error as there is no functionality to open it up.
Can anyone please tell me the code to fix this issue?
package com.GadgetGlaze.GadgetGlaze;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://www.gadgetglaze.com/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
You should return false in your shouldOverrideUrlLoading when u want to handle links in the browser itself. Try this:
#Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return false;
}

I want to show my externalurl link coming from api in webview in my application

My Code :
My Adapter
#Override
public void onBindViewHolder(#NonNull SliderAdapter.SliderHolder holder, int position) {
final HomePage.ContentList item = homePageList.get(position);
SliderHolder viewHolder = (SliderHolder) holder;
viewHolder.txt_category.setText(item.getCategoryName());
viewHolder.title_item_list_pager.setText(item.getTitle());
if (item.getImage() != null)
Glide.with(holder.itemView).load(item.getImage()).into(viewHolder.img);
Log.d("TEST103-2", "PV");
viewHolder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), NewsDetail.class);
intent.putExtra("newsdetail",homePageList.get(position).getExternalUrl());
v.getContext().startActivity(intent);
}
});
}
news detail class
package com.platin.android;
import android.os.Bundle;
import android.webkit.WebView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class NewsDetail extends AppCompatActivity {
WebView newsdetail;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
newsdetail = findViewById(R.id.newsdetail);
newsdetail.getSettings().setJavaScriptEnabled(true);
newsdetail.loadUrl(getIntent().getStringExtra("newsdetail"));
}
}
I want to show my externalurl link coming from api in webview in my application.
When I click for detail, it opens on google chrome. but I want it to show in application.
set setWebViewClient in your WebView also you can enable javaScript
webView.loadUrl(getIntent().getStringExtra(LINK));
webView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
and Clint is:
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}

changing ActionProvider to appcompat-v7

In order to extend AppCompatActivity in a MainActivity.java file, the following was migrated from:
package me.site.site;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ShareActionProvider;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://third.second.tld/");
mWebView.setWebViewClient(new com.androidwebviewapp.karthik.MyAppWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
return super.onCreateOptionsMenu(menu);
}
}
To
package me.site.site;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("https://third.second.tld/");
mWebView.setWebViewClient(new me.site.site.MyAppWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}});
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item=menu.findItem(R.id.share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
** mShareActionProvider.setOnShareTargetSelectedListener(this); **
return(super.onCreateOptionsMenu(menu));
}
}
complains with an error referring to the code highlighted above
Error:(67, 33) error: method setOnShareTargetSelectedListener in class
ShareActionProvider cannot be applied to given types;
required: OnShareTargetSelectedListener
found: MainActivity
reason: actual argument MainActivity cannot be converted to OnShareTargetSelectedListener by method invocation conversion

progress bar in webview keeps loading even after web page finished loading

Here is my code
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl(url);
}
public class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.findViewById(R.id.progressBar).setVisibility(View.GONE);
//if (progressBar.getVisibility() == View.VISIBLE)
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("carsaleindiaofficial.com/?m=1")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
The progress bar and webview works fine but when the page is finished loading the progress bar still remains? When I have loaded the page the progress bar visibility doesn't dissapear/change to GONE? Where is the error?
Ive tried both progressBar.setVisibility(View.INVISIBLE) and progressBar.setVisibility(View.GONE); but nothin seems to work.
Instead of using customizing webclient. you use default webclient. your problem is replace this.
view.setWebViewClient(new WebViewClient());
add this
view.setWebViewClient(new MyAppWebViewClient());
and also
progressBar.setVisibility(View.INVISIBLE);
not
view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
full code
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends ActionBarActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maine);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar1);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView1);
view.setWebViewClient(new MyAppWebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
//view.loadUrl(url);
view.loadUrl("http://m.facebook.com/");
// view.loadUrl("file:///android_asset/web.html");
}
public class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
Log.i("pageFinished", "yesss");
progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.maine, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
just changed WebViewClient to MyWebViewClient, progressBar to progressBar1 & ActionBarActivity to AppCompatActivity and every code seems to work :) thank again every1.. :) #Devendra & #akhil :)
my final code
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends AppCompatActivity {
private WebView view;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) this.findViewById(R.id.progressBar1);
String url = "http://www.carsaleindiaofficial.com/?m=1";
view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new MyWebViewClient());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl(url);
}
public class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("pageFinished", "yesss");
progressBar.setVisibility(View.GONE);
Log.i("progressBar", "Gone");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
Log.i("progressBar", "Visible");
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.i("ShldOvrideUrl", "working");
return true;
}
}
#Override
public void onBackPressed() {
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
view.loadUrl(view.getUrl());
return super.onOptionsItemSelected(item);
}
}
After intent of webview url you have to write progressbar.visibility="View.GONE"
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
FGallery.this.progressBar.setProgress(100);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
FGallery.this.progressBar.setProgress(0);
super.onPageStarted(view, url, favicon);
}
}
This is working for me. you can set it as WebViewClient.

Android Webview Back Button not working

I have been having a issue with a webview backbutton. I've never had this issue before regarding webview backbutton.
When I push the back button the logcat gives me the following error:
12-05 23:56:23.264: W/KeyCharacterMap(3743): Load KCM of non-default device may incur unexpected result
12-05 23:56:23.264: W/UnimplementedWebViewApi(3743): Unimplemented WebView method onKeyDown called from: android.webkit.WebView.onKeyDown(WebView.java:2323)
I've never seen this error before. Does anyone have a idea on what could be causing this?
Code:
package org.development;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ImagenesActivity extends Activity {
ListView list;
Intent intent;
TextView toma_foto;
Button button_foto;
ImageAdapter adapter;
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagenesactivity);
toma_foto = (TextView) findViewById(R.id.toma_foto);
button_foto = (Button) findViewById(R.id.button_foto);
list=(ListView)findViewById(R.id.list);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setWebViewClient(new WebViewClient());
mWebView.clearCache(true);
mWebView.loadUrl("about:blank");
mWebView.clearView();
mWebView.loadUrl("http://mysite23.com/activity");
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
//view.loadUrl("javascript:document.getElementsByClassName('elgg-page-header').style.visibility='hidden';alert('testing');");
}
});
File file = new File(Environment.getExternalStorageDirectory() + "/genx/");
String[] filas = file.list();
if(filas==null)
filas = new String[0];
adapter=new ImageAdapter(this, filas);
list.setAdapter(adapter);
}
public void foto (View view){
/**We send to the camera Activity*/
//Intent j = new Intent(this, CameraActivity.class);
//startActivity(j);
//finish();
mWebView.loadUrl("about:blank");
mWebView.clearView();
mWebView.loadUrl("http://mysite23.com/activity");
}
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack() == true) {
mWebView.goBack();
} else {
ImagenesActivity.super.onBackPressed(); //Replace MyActivity With the name of your activity.
}
}
}

Categories