Actionbar not shown on second activity with webview - java

I am working on a project in Android Studio. My MainActivity shows the Actionbar. I created a new empty activity named Teachers. It shows the Actionbar when it's empty. I then created a webview. But when I input this webview code on Teachers activity, the Actionbar is not shown in the app (even though the webview works properly).
package com.codepade.shohel.eee7brur;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Teachers extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teachers);
webView = (WebView) findViewById(R.id.teachersweb);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/teachers/index.html");
webView.setWebViewClient(new WebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack() == true){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

in your Activity you can try getActionBar().show();

Related

Whatapp share from Android webview is not working

I'm new to Android & I know this is repeatedly answered question & I found lot of conversations on this question but finally same answer. When I tried that answer, it is not working.
I converted my website into Android app, it is working fine !! when I tried to share the link from my webview to whatsapp, it is giving below error
net::ERR_UNKNOWN_URL_SCHEME
To fix this, I have seen many suggestions with shouldOverrideUrlLoading. I tried but no luck
My Code:
package com.example.abcdefgh;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
public
class MainActivity extends AppCompatActivity {
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("http://www.abcdefgh.com");
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient {
#Override
public
boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} else {
return false;
}
}
}
#Override
public void onBackPressed() {
if (mywebView.canGoBack()) {
mywebView.goBack();
} else {
super.onBackPressed();
}
}
}
I don't know what is the issue, please help me

WebView = Web page not available

I am trying to use webView to display an HTML file that is on the device and not the internet. I have my html files in the /Download folder. When I launch the application I get the following error:
Webpage not available
The webpage at file:///storage/sdcard0/Download/manuals/test/index4.html might be temporarily down or it may have been moved permanently to a new web address.
I know the file is there but it will not display it.
Here is my code:
package com.asstechmanuals.techmanual;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
File fileStandard = new File("/storage/sdcard0/Download/manuals/test/index4.html");
File fileNewStandard = new File("/storage/sdcard0/Download/manuals/test/index4.html");
File fileKitKat = new File("/storage/sdcard0/Download/manuals/test/index4.html");
if(fileStandard.exists())
mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
else if(fileNewStandard.exists())
mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
else if(fileKitKat.exists())
mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
else
mWebView.loadUrl("file:///storage/sdcard0/Download/manuals/test/index4.html");
mWebView.setWebViewClient(new vwClient());
}
private class vwClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
if (url.toLowerCase().contains(".pdf"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);
}
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#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;
}
}
In your manifest xml file, make sure you have permission to read that folder for your app and permission to use internet (even though you actually are not, it is still required).
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

Adding Back Button/Home Function and External Browser to Android App WebView

I have tried many things and keep receiving a crash error on my Android app. My app is a simple webwrapper app using the webview function. Basically, I have webview displaying a website that has a mobile site. What I am trying to do is make it so that
back button will take the user back to the previous screen
back button on the app (which I put in the layout), will also take the user to the previous screen.
I also want the home button on the phone to go back to the home screen and exit the app completely.
Lastly, the mobile app has external links that should be going into a browser and not staying within the app (example: visit full site).
Below is my code, any help for any of the 1-4 questions would be helpful! :-)! If you need to see my XML dont hesitate to ask.
package com.example.envision;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class Envision extends Activity {
/** back button functionality NOT WORKING CAUSES A CRASH
WebView webview;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
} */
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_envision);
WebView webview = (WebView) findViewById(R.id.webView1);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setSaveFormData(false);
websettings.setSavePassword(false);
webview.loadUrl("http://envisionfinancial.ca/m/");
webview.setHorizontalScrollBarEnabled(false);
webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setBackgroundColor(128);
webview.setWebViewClient(new EnvisionWebViewClient());
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
public void visible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
webview.setVisibility(10);
logo.setVisibility(0);
bar.setVisibility(0);
}
public void unvisible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
webview.setVisibility(0);
logo.setVisibility(10);
bar.setVisibility(10);
}
private class EnvisionWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url){
webview.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
view.loadUrl("http://envisionfinancial.ca/m/");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
#Override
public void onPageFinished(WebView view, String url) {
unvisible();
}
}
/** exits the app?? review */
public void finishActivity(View v) {
finish();
}
}
Are you getting a NullPointerException in onKeyDown? That is because you do not assign the WebView to the instance variable:
webview = (WebView) findViewById(R.id.webView1);
The back functionality can be implemented like this:
public void onBackPressed() {
if (webview.canGoBack())
webview.goBack();
else
super.onBackPressed();
}
See this :
http://developer.android.com/guide/webapps/webview.html
Scroll to Binding JavaScript code to Android code
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)

Back button triggering stopped unexpectedly

I've had an incredibly difficult time getting the below app to not show an error when the back button is pressed. All I want is the site to load a web page and the back button to perform as it normally does. Does anyone see anything wrong here? The LogCat shows no errors and with the emulator it doesn't break, but it always breaks on my tablet.
MyWebViewClient.java
package com.MySite;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
MySite.java
package com.mysite;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
public class MySite extends Activity {
WebView DCWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
DCWebView.getSettings().setJavaScriptEnabled(true);
DCWebView.loadUrl("http://mobile.mysite.com");
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && DCWebView.canGoBack()) {
DCWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
You are declaring a local variable called DCWebView inside of onCreate(). Change the line
WebView DCWebView = (WebView) findViewById(R.id.webview);
to
DCWebView = (WebView) findViewById(R.id.webview);
Then, when the back key is pressed, there will be a back stack (which is currently false when you call DCWebView.canGoBack(), since your global DCWebView was never initialized), the back button will be handled the way you are expecting.
What error are you getting when you hit back on a tablet? Have you tried overriding onBackPressed instead of onKeyDown?

onKeyDown method crashing app

I have the following code that is working but when I hit the hardware back button the app crashes,
package com.fnesse.tmpmobile;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressWarnings("unused")
public class TmpmobileActivity extends Activity
{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(0x00000000);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
//Error Code Here
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.themorningtonpeninsula.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I found somewhere that I needed to add,
WebView webView;
Above the onCeate method, which allowed webView to resolve. So now I get no errors but it crashes and the app runs on my phone but crashes when I hit the hardware back button.
Does anyone know why that would be happening?
Cheers,
Mike.
I'll try to do my magic guest on the error message. I think, this error is caused by the way to initialize webView variable. You see, on the class level, you declare:
WebView webView;
And then, on onCreate() you initialize webView and also redeclare it again:
WebView webView = (WebView) findViewById(R.id.webview);
But, this redeclare step resulted in class level webView to be uninitialized.
To fix it, try to change this line:
WebView webView = (WebView) findViewById(R.id.webview);
Into:
webView = (WebView) findViewById(R.id.webview);
And you should now have class level webView initialized and ready for use in whatever method you have.
Here's few resources explaining Java variable scope:
http://www.java-made-easy.com/variable-scope.html
http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm
You are creating a new Webview in your onCreate() method instead of the Class field Webview. Change
WebView webView = (WebView) findViewById(R.id.webview);
into:
webView = (WebView) findViewById(R.id.webview);

Categories