#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    49
    Rep Power
    4

    Android webview back button help


    Hi,

    I cannot seem to get the back button to work in my app, for the back code i get a red underline under the mwebview which on hover says it cannot be resolved.

    My code is below, can someone tell me where im going wrong:

    Code:
    package com.mysite.myapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient; 
    import android.widget.Button;
    
    publicclass myappActivity extends Activity
    { 
    
    final Activity activity = this; 
    
    
    @Override
    publicboolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebView.canGoBack()) { 
    mwebView.goBack(); 
    returntrue; 
    } 
    returnsuper.onKeyDown(keyCode, event);
    }
    
    
    
    
    @Override 
    publicvoid onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    setContentView(R.layout.main);
    
    final WebView mwebView = (WebView) findViewById(R.id.webview); 
    mwebView.getSettings().setJavaScriptEnabled(true); 
    
    mwebView.setWebChromeClient(new WebChromeClient() { 
    publicvoid onProgressChanged(WebView view, int progress) 
    { 
    activity.setTitle("Loading..."); 
    activity.setProgress(progress * 100); 
    
    if(progress == 100) 
    activity.setTitle(R.string.app_name); 
    } 
    
    }); 
    
    mwebView.setWebViewClient(new WebViewClient() { 
    @Override 
    publicvoid onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
    { 
    // Handle the error 
    } 
    @Override 
    publicboolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
    view.loadUrl(url); 
    returntrue; 
    } 
    });
    
    mwebView.loadUrl(http://www.mysite.com); 
    
    Button webButton = (Button) findViewById(R.id.button1); 
    webButton.setOnClickListener(new OnClickListener() { 
    @Override 
    publicvoid onClick(View v) { 
    mwebView.loadUrl(http://www.mysite.com/page1.php); 
    }});
    
    Button webButton1 = (Button) findViewById(R.id.button2); 
    webButton1.setOnClickListener(new OnClickListener() { 
    @Override 
    publicvoid onClick(View v) { 
    mwebView.loadUrl("http://www.mysite.com/page2.php"); 
    }});
    
    Button webButton2 = (Button) findViewById(R.id.button3); 
    webButton2.setOnClickListener(new OnClickListener() { 
    @Override 
    publicvoid onClick(View v) { 
    mwebView.loadUrl("http://www.mysite.com/page3.php"); 
    }});
    
    Button webButton3 = (Button) findViewById(R.id.button4); 
    webButton3.setOnClickListener(new OnClickListener() { 
    @Override 
    publicvoid onClick(View v) { 
    mwebView.loadUrl("http://www.mysite.com/page4.php"); 
    }});
    
    Button webButton4 = (Button) findViewById(R.id.button5);
    webButton4.setOnClickListener(new View.OnClickListener() {
    publicvoid onClick(View view) {
    Intent myIntent = new Intent(view.getContext(), morescreen.class);
    startActivityForResult(myIntent, 0);
    }
    
    });
    
    Button webButton5 = (Button) findViewById(R.id.button6); 
    webButton5.setOnClickListener(new OnClickListener() { 
    @Override 
    publicvoid onClick(View v) { 
    mwebView.loadUrl("http://www.mysite.com/page5.php"); 
    }});
    
    }
    
    }
    Im very new to Android, in fact this is my first app so apologies if the answer is really simple.

    Thanks in advance

    Jay
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by jaylam
    Hi,

    I cannot seem to get the back button to work in my app, for the back code i get a red underline under the mwebview which on hover says it cannot be resolved.

    My code is below, can someone tell me where im going wrong:


    Im very new to Android, in fact this is my first app so apologies if the answer is really simple.

    Thanks in advance

    Jay
    You need some spaces in there!

    like
    Code:
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebView.canGoBack()) { 
    mwebView.goBack(); 
    returnt rue; 
    } 
    return super.onKeyDown(keyCode, event);
    }
    What is your webview called in the xml file, I suspect it is not mwebview ! Change the xml file to mwebview then see where you go.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0
    If it helps any here's one that I use !!

    main.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
       <TextView
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"
           android:text="This is the demo of WebView Client"
           android:textSize="20sp"
           android:gravity="center_horizontal">       
       </TextView>
       
       <WebView
           android:id="@+id/webview01"
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"
           android:layout_weight="1">
       </WebView>
       
       <ImageView 
           android:src="@drawable/ic_launcher"
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"/>  	
    
    </LinearLayout>
    Code:
    package com.jsh.WebView;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    /*
     * Demo of creating an application to open any URL inside the application and clicking on any link from that URl 
    should not open Native browser but  that URL should open in the same screen.
     */
    public class WebViewActivity extends Activity {
        /** Called when the activity is first created. */
    	
    	WebView web;
    	 
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            web = (WebView) findViewById(R.id.webview01);
            web.setWebViewClient(new myWebClient());
            web.getSettings().setJavaScriptEnabled(true);
            web.loadUrl("http://www.google.co.uk");
        }
        
        public class myWebClient extends WebViewClient
        {
        	@Override
        	public void onPageStarted(WebView view, String url, Bitmap favicon) {
        		// TODO Auto-generated method stub
        		super.onPageStarted(view, url, favicon);
        	}
        	
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		// TODO Auto-generated method stub
        		
        		view.loadUrl(url);
        		return true;
        		
        	}
        }
        
        // To handle "Back" key press event for WebView to go back to previous screen.
    	@Override
    	public boolean onKeyDown(int keyCode, KeyEvent event) 
    	{
    		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
    			web.goBack();
    			return true;
    		}
    		return super.onKeyDown(keyCode, event);
    	}
    }

IMN logo majestic logo threadwatch logo seochat tools logo