1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Check my updated answer. You should have a WebView inside your Layout and set its layout programmatically. Using "android.intent.action.VIEW" will start an implicit intent that is left for Android system to decide how to open. It will not load it in the WebView inside your layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.