1

I followed this to Print in android (Thermal Printer)

This is My web-view Over there On-click It will Load My Android Activity Which is Out of Web-view...

For that I have Given this... to Open new activity from web-view..

public class Main_web extends AppCompatActivity {

private WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_m);

    webView = (WebView) findViewById(R.id.webm);

    WebSettings set = webView.getSettings();
    set.setJavaScriptEnabled(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    final ProgressDialog progressBar = ProgressDialog.show(Main_web.this, "Please Wait", "Loading...");

    webView.addJavascriptInterface(new WebAppInterface(this), "NewFUN");
    webView.requestFocusFromTouch();
    webView.setWebViewClient(new WebViewClient()

    {

        public void onPageFinished(WebView view, String url) {

            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

    });

    webView.setWebChromeClient(new WebChromeClient());

    String url = "www.google.com/m_app_eta.php";
    //String url = "file:///android_asset/tex.html";
    try {

        if (URLUtil.isValidUrl(url)) {
            webView.loadUrl(url);

        } else {
          //do something
        }
    } catch (Exception e) {
         //do something

    }

}

//Class to be injected in Web page

public class WebAppInterface {

    Context mContext;
    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void Print() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Are you sure you want to leave to next screen?");
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent chnIntent = new Intent(Main_web.this, Print_activity.class);
                        startActivity(chnIntent);
                    }
                });
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        alertDialog.show();
    }

}
}

Now My printer Activity is working Fine So here I want to pass a Value On click Button Which Opens Printer... But can Any one suggest me How to Receive same value in android Activity not in web-view...

All I need is In my web-view when I click on the Button its loading Android Activity Along with that I want to Pass A string value to new Activity

Why Should I pass Value means In that I have a Url so I can Print with that URL Here JS value is different form current URL

Update

I followed this But its Inside the Web-view I want same at Outside the web-view...

Means When I click on the web-view Its opening android activity with the same its should pass a value to my activity not the web-view

Update 1

I followed @Sujal Mandal answer But I dont Know How to use that Value In next activity Can any one suggest me... on this kind... in next activity It may in System.out.println or text-view or any other string So I can use can Any one suggest How to Use the JavaScript Value in other activity outside the web-view..

1 Answer 1

1

HTML JS CODE

<script type="text/javascript">
function Pa(value) {
       //value is the param received from onClick
       NewFUN.Print(value); //call the android method with value param
    }
</script>

<center>
    <h3>Sample HTML</h3>
    <div id="content">Click on Button To thermal print</div>
    <div>
        <input type="button" onClick="Pa('26997')" /><br/>
        </div>
</center>   

& change your android code to be like this

@JavascriptInterface
    public void Print(final String stringFromWebView) {
        //use stringFromWebView
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Are you sure you want to leave to next screen?");
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent chnIntent = new Intent(Main_web.this, Print_activity.class);
                        chnIntent.putExtra("STRING_DATA", stringFromWebView);
                        startActivity(chnIntent);
                    }
                });
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

In next activity receive the web JS response by using

String data = getIntent().getStringExtra("STRING_DATA");

at oncreate

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

7 Comments

This is fine But its not working... If I give this public void Print() Its Opening new Activity... But If I give this public void Print(String stringFromWebView) its not working Because.. I have Given this in JS function Pa(value) { NewFUN.Print(); } SO here what ever its there it will work NewFUN.Print(); ,NewFUN.Splash();.. but its not working when I give NewFUN.Print(String stringFromWebView);
Thanks for Responce @Sujal Mandal
NewFUN.Print('string') try this, why would you pass a variable declaration as a parameter anyway?
alternatively if you have a variable in js just pass the variable in the method it should work, e.g var valToSend="string stringer"; NewFUN.Print(valToSend);
Sorry I am not getting SO asked can you update your code
|

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.