ps34
ps34

Reputation: 183

Open a URL within an Android application

In my Android App, I have a EditText, a spinner and a button. I would like to change the url with the EditText input and spinner choice and when I click the button I want to see the url result in my application not in the browser.

I have managed to change the url, I even can open it in the browser but I couldn't be able to open it in WebView.

I made necessary permissions for internet in Android Manifest. But it is not working. Thanks for help.

Here is my code:

    private Spinner search;
    public int click = 0;
    public char cri;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library);

        final String[] data = getResources().getStringArray(R.array.search_array);
        search = (Spinner)findViewById(R.id.search_spin);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data);
        search.setAdapter(spinnerArrayAdapter); 

        search.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l){
                click = click + 1;
                if(click>0)
                {
                    if(i == 0)
                        cri = 'X';
                    else if(i == 1)
                        cri = 't';
                    else if(i == 2)
                        cri = 'a';


                    Button search = (Button)findViewById(R.id.searchButton);
                    search.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v){

                            EditText key = (EditText) findViewById(R.id.searchKey);
                            String StKey = key.getText().toString();
                            String URL = "";

                            if(cri == 'X')
                                URL = "http://anadolu.lib.metu.edu.tr/mobilepac/browse.php?SEARCH=" +StKey+ "&kriter=X&Submit=Search";
                            else if(cri == 't')
                                URL = "http://anadolu.lib.metu.edu.tr/mobilepac/browse.php?SEARCH=" +StKey+ "&kriter=t&Submit=Search";
                            else if(cri == 'a')
                                URL = "http://anadolu.lib.metu.edu.tr/mobilepac/browse.php?SEARCH=" +StKey+ "&kriter=a&Submit=Search";


                            WebView webView = (WebView)findViewById(R.id.webView1);
                            webView.loadUrl(URL);

                        }});
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }  

Upvotes: 0

Views: 124

Answers (2)

Tugrul
Tugrul

Reputation: 1808

Try this.

String url = "http://www.exampleurl.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Upvotes: 0

Deniz
Deniz

Reputation: 12530

you need to try like this to load url in web view inside app.

    WebView webView = (WebView)findViewById(R.id.webView1);
    webView.loadUrl(URL);
    webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });

Upvotes: 1

Related Questions