ihtkwot
ihtkwot

Reputation: 1252

In android how do you search the internet and return the results in an activity?

I want the user to input their search query into a search widget and then have the search results displayed to them and then when they click on a url that they want, I need to be able to save that url for use in other parts of my application.

Is this possible, and if not what are some of the restrictions preventing this?


EDIT -- I figured I'd add the code for my activity that has the webview.

public class State extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final boolean customTitleSupport = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.state);

        if( customTitleSupport ){
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title );
        }

        final TextView myTitleText = (TextView)findViewById(R.id.my_title);
        if( myTitleText != null ){
            myTitleText.setText(R.string.app_name);
        }

        WebView stateBrowser = (WebView)findViewById(R.id.state_search);
        stateBrowser.loadUrl("http://www.google.com/");
    }
}

Upvotes: 0

Views: 830

Answers (1)

Anantha Sharma
Anantha Sharma

Reputation: 10108

you need to use something called WebViewClient

public class WebViewTestActivity extends Activity implements
        View.OnClickListener {
    /** Called when the activity is first created. */
    Button btn = null;
    WebView myWebView =null;
    EditText et =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        et = (EditText) findViewById(R.id.editText1);
        myWebView = (WebView) findViewById(R.id.webview);
// WebViewClient in use.
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                et.setText(url);
            }
        });
    }

    @Override
    public void onClick(View source) {
        if (btn.getText().equals("Go!")) {
            myWebView.loadUrl(et.getText().toString());
        }
    }

}

`

Upvotes: 1

Related Questions