Mike
Mike

Reputation: 97

Webview buttons stop working after website loads

I have created a simple webview with two buttons below it. If I press one of the button the view seems to create a new view called "web" then the page loads and I still have my buttons below but they don't function. If I use the back button on the phone it takes me to the opening view blank page and the buttons work again? Sorry I am new..

I just want it to load in the original view and have the buttons continue to function.

Do I have to suppress the creation of a new view somehow?

Kind Regards,

-Mike

** and I am not sure why my code always has extra crap when I post it because it does't when I copy it to the clipboard. **

enter image description here

Webscreen Class

package com.example.cam;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Webscreen <URL> extends Activity {
    WebView webview1;

    public static final String URL = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String turl = getIntent().getStringExtra(URL);

        webview1 = (WebView)findViewById(R.id.webview01);

        webview1.clearCache(true);
        webview1.loadUrl(turl);
    }
}

cam Class

package com.example.cam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class cam extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Add Click listeners for all buttons
        View firstButton = findViewById(R.id.button1);
        firstButton.setOnClickListener(this);
        View secondButton = findViewById(R.id.button2);
        secondButton.setOnClickListener(this);
    }

    // Process the button click events
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button1:
                Intent j = new Intent(this, Webscreen.class);
                j.putExtra(com.example.cam.Webscreen.URL, 
                    "http://m.yahoo.com");
                startActivity(j);
            break;
            case R.id.button2:
                Intent k = new Intent(this, Webscreen.class);
                k.putExtra(com.example.cam.Webscreen.URL, 
                    "http://m.google.com");
                startActivity(k);
            break;      
        }
    }
}

Upvotes: 1

Views: 1092

Answers (2)

Aelaf
Aelaf

Reputation: 118

On button click you are recreating your activity with the intent of displaying the web page/s.

Intent j = new Intent(this, Webscreen.class);
        j.putExtra(com.example.cam.Webscreen.URL, 
            "http://m.yahoo.com");
        startActivity(j);

it is only creating new activity on top of the new activity you need to load the web page on button click instead.

//on button1 click 
mWebView.loadUrl("http://m.yahoo.com");

Upvotes: 0

Venkatesh Suvarna
Venkatesh Suvarna

Reputation: 102

I don't know why are you not using Button class and using View Class. Use

Button b1=(Button)findViewById(R.id.button1);

instead of

View b1=findViewById(R.id.button1);

Use relative layout for placement of buttons

Upvotes: 1

Related Questions