My Blog App Has Been Crashed - java

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);

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>

How to implement TvView on Android TV?

Goal
To create a reduced TvView that displays on top of a WebView.
This is specifically for live TV.
(I've seen numerous solutions that address streaming content, but this is intended for video that's delivered through the coaxial cable.)
Where I'm At Now
The WebView portion works fine, and I've learned that a RelativeLayout is required in order to layer views.
Also, TvView doesn't seem to be recognized, so I'm guessing that I'll probably need to use a SurfaceView instead.
activity_main.xml
MainActivity.java
Questions
I've seen the TvInput, example applications, but it's extremely excessive. I'm not trying to recreate the TvInput service, just leverage on existing framework.
Is it possible to simply call on the existing, TV service that's already on the Android device, and display it in a view?
If it is possible, how is it accomplished?
If it's not possible, what's the simplest method of implementing it? (repository links would be great.)
I've searched all over for answers but can't seem to find anything.
Thank you in advance for any assistance.
Code in Text Format
As requested by tung.
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/main_browse_fragment"
android:name="com.company.app.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.app.MainActivity"
tools:deviceIds="tv"
tools:ignore="MergeRootFrame" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TvView
android:id="#+id/TvView"
android:layout_width="816dp"
android:layout_height="404dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout >
MainActivity.java
package com.company.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.media.tv.TvView;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
public class MainActivity extends Activity {
private WebView mWebView;
private TvView mTvView;
private WebSettings mWebSettings;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mWebView = new WebView(this);
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.loadUrl("http://10.28.98.150:1000/");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
this.setContentView(mWebView);
mTvView = new TvView(this );
mTvView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
this.setContentView(mTvView);
}
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
String channelUrl="content://android.media.tv/channel/"+channelNumber;
mTvView.tune(mInputId, Uri.parse(channelUrl));
input id is available in TV database(refering to which source channel is coming from).
If you pass these info to tvview it will start playing.

Adding a progress bar to android webview 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);
}
}
}

Android WebView shows blank screen

I'm trying to create a WebView app that shows a meteor web app using Tiago Scolari's sample.
When i load the apk in my phone i see the background changes but the log in button doesn't show.
Anyone has a clue how to make this work?
--Edit:
Adding -
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
- shows me the log in button and gets me to the facebook log in.
After logging in using facebook i'm presented with a white screen.
Any more advice?
Some code:
main.xml (layout):
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainWebView">
</WebView>
</LinearLayout>
AndroidMobileAppSample.java (java wrapper):
package tscolari.mobile_sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://pastime.meteor.com/");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
oke I just tested it on my 2.3.4 android and yea there is no facebooklogin. Its a WebView problem. Changed the code and still the login dosent appear...

Categories

Resources