Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open url in webview activity

Tags:

How to open Url in Webview activity

Hi,

i want to open link in WebView activity right now my code is scan barcode & open link directly to browser but i want to change it and open in Webview how can i do this please help me to fix this issue

thanks

here is code of BarcodeScannerActivity

   public class BarcodeScannerActivity extends AppCompatActivity {

    String scanContent;
    String scanFormat;
    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode_scanner);
        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator scanIntegrator = new IntentIntegrator(BarcodeScannerActivity.this);
                scanIntegrator.setPrompt("Scan");
                scanIntegrator.setBeepEnabled(true);

                //enable the following line if you want QR code
                //scanIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);

                scanIntegrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
                scanIntegrator.setOrientationLocked(true);
                scanIntegrator.setBarcodeImageEnabled(true);
                scanIntegrator.initiateScan();
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanningResult != null) {
            if (scanningResult.getContents() != null) {
                scanContent = scanningResult.getContents().toString();
                scanFormat = scanningResult.getFormatName().toString();
            }

            Toast.makeText(this, scanContent + "   type:" + scanFormat, Toast.LENGTH_SHORT).show();

            textView.setText(scanContent + "    type:" + scanFormat);



            Intent browseintent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
            startActivity(browseintent);

        } else {
            Toast.makeText(this, "Nothing scanned", Toast.LENGTH_SHORT).show();
        }
    }
}

Webview Activity Code

        public class SecondActivity extends AppCompatActivity {
            Button b1;
            EditText ed1;

            private WebView wv1;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_second);

                b1=(Button)findViewById(R.id.button);
                ed1=(EditText)findViewById(R.id.editText);

                wv1=(WebView)findViewById(R.id.webView);
                wv1.setWebViewClient(new MyBrowser());

                b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = ed1.getText().toString();

                        wv1.getSettings().setLoadsImagesAutomatically(true);
                        wv1.getSettings().setJavaScriptEnabled(true);
                        wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                        wv1.loadUrl(url);
                    }
                });
            }

            private class MyBrowser extends WebViewClient {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            }
        }
like image 246
user1833487 Avatar asked Mar 19 '18 08:03

user1833487


People also ask

How do I open an HTML file in WebView?

The WebView method to load our file takes a URI , so we need to access the HTML file using that URI . Since we stored it in the assets folder, we can access it using file:///android_asset/{file_name} . Now let's load that file in our MainActivity .

How do I open URL in WebView flutter?

Android-only settings: forceWebView – If set to null or false , the URL is opened in the device's default browser; otherwise, the URL is launched in a WebView. enableJavaScript – If set to true , JavaScript is enabled in WebView. enableDomStorage – When the value is set to true , WebView enables DOM storage.

What is a WebView URL?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


2 Answers

Replace the following code

Intent browseintent=new Intent(Intent.ACTION_VIEW, 
  Uri.parse("http://www.example.com/index.php?iduser="+ scanContent));
  startActivity(browseintent);

with below code

Intent browseintent=new Intent(this, SecondActivity.class);
browseintent.putExtra("url","http://www.example.com/index.php?iduser="+ scanContent);
startActivity(browseintent);

This will open the secondactivity with url in intent extras. You can set it to your edittext or you can use it directly to your webview. You can receive the url in the second activity using the following code

String url = getIntent().getExtras().getString("url");

You can use it in your button click as follows

b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = getIntent().getExtras().getString("url");

                    wv1.getSettings().setLoadsImagesAutomatically(true);
                    wv1.getSettings().setJavaScriptEnabled(true);
                    wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                    wv1.loadUrl(url);
                }
            });
like image 105
Ramees Avatar answered Sep 19 '22 12:09

Ramees


You try this, it should open link with webview:

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://vk.com/zabroshkiborika");
like image 27
Tung Tran Avatar answered Sep 21 '22 12:09

Tung Tran