I created a new function in a CustomWebViewClient class that needs to use the webViewObject. However, I dont seem to find a way how to access it without passing the parameter to the constructor.
This is what the class looks like:
public class CustomWebViewClient extends WebViewClient {
public CustomWebViewClient(){
myFunction();
}
private myFunction(){
//needs to use the webView object
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String check_url){
//some code here
return false
}
}
This is how to set the custom class to a webViewObject:
CustomWebViewClient myWebViewClient = new CustomWebViewClient();
webViewObject.setWebViewClient(myWebViewClient);
Notice that shouldOverrideUrlLoading function gets the parameter. So its possible its there somewhere.
Related
I seem to be stuck with a problem with an object communicating with my activity class. The object is a view object with an onClick method that when called I would like it to notify my activity class so that it can perform said action. Below is some example code of my situation (assume all conventional setup operations have already been made):
public class MainActivity extends AppCompatActivity{
//...other global methods and objects
//Does not have access to instantiated Entry object(s)
public void entryObjectWasClicked(){
//perform said action
}
}
public class Entry extends View implements View.OnClickListener{
//...other global methods and objects
//Does not have access to the MainActivity object
#Override
public void onClick(View v){
//send a message to the MainActivity to
//somehow call the entryObjectWasClicked() method
}
}
The only way (off the top of my head) that I could think about dealing with this problem is by creating a static method in MainActivity and then calling it from an anonymous MainActivity object in the onClick method of Entry. The problem with the static method approach is that any subsequent method/object/primitive usages in the static method force those methods/objects/primitives to be static. This defeats the purpose of then being able to have two different instances of the MainActivity object.
After some looking I came across using Broadcast messages, specifically using the LocalBroadcastManager to send an intent to the activity. This code example works for my model, but I want to know: is this the best way for me to go about sending messages to my MainActivity from my Entry object?
If there is a more effective way of doing all this, what would it be?
You're overcomplicating things. Don't override onClick for this. Instead, have your activity call setOnClickHandler on your view, which sets a callback that's called when the view is clicked. Then use the default implementation.
Since you extend view, i guess you want to use it inside a layout. That means you may want to create a Listener for that. Example:
public class Entry extends View implements View.OnClickListener{
private OnClickListener listener;
public void setListener(OnClickListener listener) {
this.listener = listener;
}
#Override
public void onClick(){
if (this.listener != null) this.listener.onClick(this);
}
}
How you can inflate your layout in your Activity and access your custom view.
public class MainActivity extends AppCompatActivity{
public void onCreate( ...) {
Entry entry = findViewById(R.id.entry);
entry.setListener(new OnClickListener(...));
}
}
Android injects a JS interface into a web view:
JavaScriptInterface javaScriptInterface = new JavaScriptInterface(this);
browser.addJavascriptInterface(javaScriptInterface, "qp");
The interface looks like this:
public class JavaScriptInterface {
private ILoadEpi iLoadEpi;
public JavaScriptInterface(ILoadEpi iLoadEpi) {
this.iLoadEpi = iLoadEpi;
}
#JavascriptInterface
public void passParameters(String fldMerchCode,
String fldMerchRefNbr,
String fldTxnAmt,
String fldTxnScAmt,
String fldDatTimeTxn,
String fldDate1,
String fldDate2
) {
Log.d("fldMerchCode", fldMerchCode);
Log.d("fldMerchRefNbr", fldMerchRefNbr);
Log.d("fldTxnAmt", fldTxnAmt);
Log.d("fldTxnScAmt", fldTxnScAmt);
Log.d("fldDatTimeTxn", fldDatTimeTxn);
Log.d("fldDate1", fldDate1);
Log.d("fldDate2", fldDate2);
iLoadEpi.loadEpi(fldMerchCode, fldMerchRefNbr, fldTxnAmt, fldTxnScAmt, fldDatTimeTxn, fldDate1, fldDate2);
}
}
How can a web app developed using TypeScript call this Android?
Or more broadly, how can a TypeScript application call an Android method?
Add a TypeScript definition for the JavaScriptInterface type that will be injected by Android. Then declare a variable with the name of the instance injected by Android, then use it as normal. In your example, the definition you need is:
interface JavaScriptInterface {
passParameters(fldMerchCode: string,
fldMerchRefNbr: string,
fldTxnAmt: string,
fldTxnScAmt: string,
fldDatTimeTxn: string,
fldDate1: string,
fldDate2: string) : void;
}
declare var qp: JavaScriptInterface;
The qp instance injected by Android will have the method passParameters available on it. The instance is created by Android with the name qp in your call to browser.addJavaScriptInterface(javaScriptInterface, "qp");. Note that, depending on how your passParameters function is used, you may need to declare the return type as any instead of void.
Here's a complete example based on the Android guide for binding JS:
In your HTML file, add:
<input type="button" value="Say hello" id ="button"/>
<script src="./generated/bundle.js"></script>
where I assume that your generated/transpiled JavaScript is located at ./generated/bundle.js, relative to the HTML file.
In your TypeScript file, add:
interface WebAppInterface {
showToast(toast: string) : any;
}
declare var android: WebAppInterface;
var button = document.getElementById('button');
button.onclick = ()=>android.showToast('Hello Android!');
Note that the linked Android example names the injected object android:
webView.addJavascriptInterface(new WebAppInterface(this), "android");
And in case the linked example changes or disappears, here is the example WebAppInterface.java:
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
here's a portion of my code:
public class Login extends Activity {
private class LoginUser extends AsyncTask<String, String, Boolean> {
#Override
protected void onPostExecute(Boolean returnResult) {
DialogSelectAccount dsa=new DialogSelectAccount(getParent());
dsa.show();
}
}
}
public class DialogSelectAccount extends Dialog implements android.view.View.OnClickListener {
public DialogSelectAccount(Activity a) {
super(a);
}
}
but when I run the app, it get a NPE error at the "super(a)" under the public DialogSelectAccount();
but when I changed my code to
public class Login extends Activity {
private class LoginUser extends AsyncTask<String, String, Boolean> {
#Override
protected void onPostExecute(Boolean returnResult) {
test();
}
}
public void test(){
DialogSelectAccount dsa=new DialogSelectAccount(this);
dsa.show();
}
}
it works. So what if I don't want to create a separate method like above and calls DialogSelectAccount directly inside the onPostExecute, what should I pass as the argument?
Thanks
So what if I don't want to create a separate method like above and calls DialogSelectAccount directly inside the onPostExecute, what should I pass as the argument?
answer:
DialogSelectAccount dsa=new DialogSelectAccount(Login.this);
This is rather general java question, for more on inner classes read here: Getting hold of the outer class object from the inner class object
The dialog class needs a Context attribute.
When you say getParent() - I suppose it does not return context.
You can keep the context attribute in a global class and retrieve it - though I will not recommend that.
I have been looking for a long time for a simple way to pass data (string type) from class to activity.
I found some tutorials about passing data from activity to class but is it possible to do the opposite, passing data from class to activity ?
if you import the class in your activity (which is also a class by the way) you can easily access the classes attributes.
example: MyClass.java
package edu.user.yourappname;
public class MyClass {
public string infoToPass = "whatever";
}
MyActivity.java
package edu.user.yourappname;
import edu.user.yourappname.MyClass
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String myString = MyClass.infoToPass;
}
}
i have no IDE to type this in atm it might contain some errors :S but i hope you get the idea.
if you need more specific help you have to provide a code sample.
also, what do you want to achieve exactly? maybie there's a different approach.
cheers!
Create Interface and implement that in your activity. Pass the activity instance in your class and and call that instance with interface method whenever you like.
To be more clear, create an interface and use it as following:
public interface SomeInterface{
public void passValue(String value);
}
public SomeActivity extends Activity implements SomeInterface{
// place any code you want in your activity, onCreate, onResume, etc.
private void someMethod(){
// Wherever in your activity, initialize your class with your activity.
SomeClass someClass = new SomeClass(this);
someClass.someMethod();
}
public void passValue(String value){
// do whatever you want with your value
}
}
public class SomeClass{
private SomeInterface someInterfaceInstance;
public SomeClass(SomeInterface someInterfaceInstance){
this.someInterfaceInstance = someInterfaceInstance;
}
public void someMethod(){
// Some code...
someInterfaceInstance.passValue("Hello World!");
// Some more code...
}
}
Here is a easy way of doing it -
By defining static variables
In your class, make the String whose value you want to pass public static like this -
public static String pass;
And then in you activity, you can directly access it since it's a public variable like this -
String receive = className.pass;
I have a WebView based app to which I want to add a 'night-mode'.
My first instinct was to just have the program render the right CSS values each time depending on whether night-mode was on or not, and it works great, but it means that the page has to be reloaded each time the mode is toggled.
Is there any way I can change the css values retro-actively without having to reload the page?
You could inject code into your page's DOM. So you could for example have a reset stylesheet, and then inject a day or night mode class with javascript into the dom as required like this:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:document.getElementById(id).style.property=new style");
}
}
HTH