2

Ok, so I've got my app done, and I'm working on minor tweaks for it, and one thing is I would prefer my web links to launch in webview instead of the stock browser... I've tried so many things and I keep getting errors and eclipse keeps launching the debug perspective and I'm stuck..

// XXXX.edu Button
        Button XXXX_Button = (Button)findViewById( R.id.XXXX_Button );
        XXXXXX_Button.setOnClickListener( new View.OnClickListener()
        {
        public void onClick(View v)
        {
            Uri uri = Uri.parse( "http://www.XXXXXX.edu/" );
            startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
        }
        });
peter
  • 23
  • 1
  • 4

1 Answers1

0

You need to extend WebViewClient, and launch the url within that.

public class WebActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        webview = (WebView) findViewById(R.id.wv);
        webview.setWebViewClient(new WebC());
        webview.loadUrl(baseUrl);
    }

    public class WebC extends WebViewClient
    {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
    ... etc.

And in your layout xml,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

    <WebView
        android:id="@+id/wv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>
Rajath
  • 11,787
  • 7
  • 48
  • 62
  • I dont understand how to use the part in my java, file, i got the part in my xml, but the java file is messing me up, do i need to create a string for each of my links and then put in the @string/website1, and string website 1 is my link to site. Like i said this is completely new to me and im just starting at developing android apps, so i really appreciate all the help.. – peter Apr 04 '11 at 16:05
  • In the above code, just replace `baseUrl` with your hard-coded path. Once you get it working, you can look at [String Resources](http://developer.android.com/guide/topics/resources/string-resource.html) – Rajath Apr 05 '11 at 05:26