onClick button that sends to webpage


  1. sigurros

    sigurros Active Member

    I am fairly new to Android Developing and have made it through making most of my first app and the last time I had a problem I came here and was pleased with the feedback and generosity of members to help so I figured I'd try again.

    The current problem I am having is trying to use the buttons on my menu page to open a webpage. There are four buttons on the menu, each will open a different webpage when clicked.

    If I could just get an example to open up one of them I could easily figure out the others.

    Do I need to create a new xml/.java for each of the buttons?

    This is the xml for my menu

    HTML:
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3.  
    4. <LinearLayout android:id="@+id/LinearLayout01" xmlns:android="http://schemas.android.com/apk/res/android"
    5. android:layout_height="fill_parent" android:layout_width="wrap_content" android:background="@android:color/white"><RelativeLayout android:layout_below="@+id/TextView01" android:id="@+id/RelativeLayout02" android:layout_height="fill_parent" android:layout_width="fill_parent"></RelativeLayout><RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@color/menubg"><Button android:id="@+id/Button01" android:text="@string/failedbanklist" android:clickable="true" android:layout_below="@+id/TextView01" android:layout_centerHorizontal="true" android:layout_marginLeft="50px" android:layout_marginRight="0px" android:fadeScrollbars="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="25px" android:textColor="@color/loadtxt" android:textSize="6pt" android:textStyle="normal|bold" android:typeface="serif" android:height="25px"></Button><Button android:id="@+id/Button02" android:text="@string/pressrls" android:layout_below="@+id/Button01" android:layout_centerHorizontal="true" android:layout_width="fill_parent" android:layout_marginLeft="50px" android:clickable="true" android:layout_height="wrap_content" android:layout_marginRight="0px" android:fadeScrollbars="true" android:height="25px" android:textSize="6pt" android:layout_marginBottom="25px" android:layout_marginTop="25px" android:textColor="@color/loadtxt" android:textStyle="normal|bold" android:typeface="serif"></Button><Button android:id="@+id/Button03" android:text="@string/helpfrm" android:layout_below="@+id/Button02" android:layout_centerHorizontal="true" android:layout_marginLeft="50px" android:clickable="true" android:layout_height="wrap_content" android:layout_marginRight="0px" android:fadeScrollbars="true" android:height="25px" android:textSize="6pt" android:textColor="@color/loadtxt" android:layout_width="fill_parent" android:layout_marginBottom="25px" android:textStyle="normal|bold" android:typeface="serif"></Button><Button android:id="@+id/Button04" android:layout_height="wrap_content" android:text="@string/btn4" android:layout_width="fill_parent" android:layout_marginLeft="50px" android:layout_centerHorizontal="true" android:layout_below="@+id/Button03" android:textColor="@color/loadtxt" android:textSize="6pt" android:layout_marginBottom="25px" android:typeface="serif" android:textStyle="normal|bold" android:clickable="true" android:fadeScrollbars="true" android:height="25px"></Button><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/TextView02" android:layout_below="@+id/Button04" android:text="@string/txt3" android:textColor="@android:color/black"></TextView><TextView android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:text="@string/mnuttl" android:textColor="@color/bkk" android:textStyle="bold" android:textSize="21pt" android:typeface="serif" android:layout_above="@+id/RelativeLayout02" android:layout_width="fill_parent" android:gravity="center" android:background="@android:color/white"></TextView><ImageView android:id="@+id/ImageView01" android:src="@drawable/rkwimagesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="12px" android:paddingLeft="6px"></ImageView><ImageView android:id="@+id/ImageView02" android:paddingRight="6px" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:paddingTop="14px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/rkwimagesmall"></ImageView>
    6.  
    7. </RelativeLayout>
    8. </LinearLayout>
    This is the .java for my menu
    Code (Text):
    1.  
    2. package android.bankclosures;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6.  
    7.  
    8.  
    9.  
    10. public class MenuActivity extends Activity {
    11.     /** Called when the activity is first created. */
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.menu);
    16.        
    17.  
    18.    
    19.        
    20.  
    21.     }
    22.  
    23.        
    24.     }
    25.  

    Basically, I want the "@+id/Button01" to take me to:

    FDIC: Failed Bank List



    I have another xml/.java that sends me to that page. I just don't know how to get from where I am using the onClick method to take me to the page.

    If I am able to send the user to the webpage from my MenuActivity.java page, you can ignore what is below this.

    This is the other xml/.java that just takes you to the webpage, I wasn't sure if I had to send(from button click) the user to this, then this takes them to webpage.


    Webpage XML

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <WebView xmlns:android="http://schemas.android.com/apk/res/android"
    3.         android:id="@+id/webview"
    4.         android:layout_width="fill_parent"
    5.         android:layout_height="fill_parent"
    6.        
    7.     />

    Webpage .Java

    Code (Text):
    1.  
    2. package android.bankclosures;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.KeyEvent;
    7. import android.webkit.WebView;
    8. import android.webkit.WebViewClient;
    9.  
    10. public class Clsdbanks extends Activity {
    11.     WebView mWebView;
    12.  
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.clsdbanks);
    16.        
    17.         mWebView = (WebView) findViewById(R.id.webview);
    18.         mWebView.getSettings().setJavaScriptEnabled(true);
    19.         mWebView.loadUrl("http://www.fdic.gov/bank/individual/failed/banklist.html");
    20.        
    21.  class HelloWebViewClient extends WebViewClient {
    22.               @Override
    23.               public boolean shouldOverrideUrlLoading(WebView view, String url) {
    24.                   view.loadUrl(url);
    25.                   return true;
    26.               }
    27.           }
    28.           mWebView.setWebViewClient(new HelloWebViewClient());
    29.     }
    30.           @Override
    31.           public boolean onKeyDown(int keyCode, KeyEvent event) {
    32.               if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    33.                   mWebView.goBack();
    34.                   return true;
    35.           }
    36.           return super.onKeyDown(keyCode, event);
    37.     }
    38.          
    39.     }
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  




    Any and all help is appreciated, I know it will be frustrating to explain something that I should already know but I am self-teaching myself this for an internship and had very little java experience and no android sdk experience and start a few weeks ago.

    Thanks again

    Siggi

    Advertisement
  2. sigurros

    sigurros Active Member

    Here is my first attempt at it. Not sure if I am heading in the right direction but I am getting an error that says

    on line 44. I have bolded the line to make it easier to find.


    Here is the .java
    Code (Text):
    1.  
    2. package android.bankclosures;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.KeyEvent;
    7. import android.view.View;
    8. import android.webkit.WebView;
    9. import android.webkit.WebViewClient;
    10. import android.widget.Button;
    11.  
    12. public class MenuActivity extends Activity {
    13.     WebView mWebView;
    14.  
    15.  
    16.     /** Called when the activity is first created. */
    17.     @Override
    18.     public void onCreate(Bundle savedInstanceState) {
    19.         super.onCreate(savedInstanceState);
    20.         setContentView(R.layout.menu);
    21.        
    22.        Button Button01=(Button)findViewById(R.id.Button01);
    23.        Button01.setOnClickListener(new View.OnClickListener() {
    24.        
    25.         @Override
    26.         public void onClick(View view) {
    27.             mWebView = (WebView) findViewById(R.id.webview);
    28.             mWebView.getSettings().setJavaScriptEnabled(true);
    29.             mWebView.loadUrl("http://www.fdic.gov/bank/individual/failed/banklist.html");
    30.            
    31.      class HelloWebViewClient extends WebViewClient {
    32.                   @Override
    33.                   public boolean shouldOverrideUrlLoading(WebView view, String url) {
    34.                       view.loadUrl(url);
    35.                       return true;
    36.                   }
    37.               }
    38.               mWebView.setWebViewClient(new HelloWebViewClient());
    39.         }
    40.               public boolean onKeyDown(int keyCode, KeyEvent event) {
    41.                   if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    42.                       mWebView.goBack();
    43.                       return true;
    44.               }
    45.            [B]   return super.onKeyDown(keyCode, event);[/B]
    46.         }
    47.              
    48.        
    49.  
    50.            
    51.        
    52.     });
    53.        
    54.  
    55.    
    56.        
    57.  
    58.     }
    59.  
    60.        
    61.     }
    62.  
    63.  

Share This Page