Alan Fletcher
Alan Fletcher

Reputation: 283

Android get url with onclick

Hello I am trying to get the value of a url with onclick but it is not working for me. I am usign the following code.

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;


public class MainActivity extends Activity {
String valueOfURL = null;
WebView webView;
Intent browserIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.example.com"));
    startActivity(browserIntent);
}

public String handleLinkClicked(String value) {
    if (value.startsWith("http"))  

    valueOfURL = value;
    return valueOfURL;

}
public View.OnClickListener myhandler = new View.OnClickListener() {
    public void onClick(View view) {
    handleLinkClicked(valueOfURL);
    webView.loadUrl("http://docs.google.com/gview?embedded=true&url=valueOfURL");
    }};
}

The first page will load fine but when i click on a pdf on that page it wont load. Basically what I am trying to do is get the value of the URL and pass it to a variable that is then used to be read via google docs. It is a pdf file and its URL looks like this http://wwww.example.com/c/document_library/get_file?

Can anyone see where I am going wrong and maybe point me in the right direction.

Thanking You

Upvotes: 1

Views: 1150

Answers (1)

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

You did not set your onClickListener() myhandler to any View. Your code (browserIntent) starts an implicit intent that will automatically start a Web browser (new Activity not linked to your application) to handle your URL. If you need to control the browsing from your App, you should implement a WebView inside your Layout and set your onClickListener to it instead of using implicit intents. Check this link on how Android handles implicit intents

Upvotes: 1

Related Questions