I have an application in which an activity communicates with our server and gets the outstanding amount details. I want users to pay the outstanding amount using a payment gateway. for Payment Gateway, I have to call web page from webview and transfer outstanding amount details and other credentials from activity to the page loaded in webview. At the end of transaction I once again need to get some parameters back from webpage in webview to activity.
This is a two way communication which I want to achieve between an activity and a page loaded in webview. I tried to find out a way to do it but could not find a simple example which does such exchange.
You could achieve what you want using a javascript WebAppInterface as demonstrated here.
The main concept is that, you create a javascript interface inside the Activity holding your WebView.
private class WebPayInterface {
int amount;
boolean success;
#JavascriptInterface
public void PaymentFinished(int amount, boolean success) {
this.amount = amount;
this.success = success;
// do whatever you want in the parent activity.
}
}
Add the interface to your webView
webView.addJavascriptInterface(new WebPayInterface(), "WebPayInterface");
Finally in your html code using javascript you can call
WebPayInterface.PaymentFinished(100, true);
You can make use of Intent.putExtra() while navigating using Intents, Shared Preferences and also Bundle. Please read Android documentation for Shared Preferences, Intents, Bundle
Related
I have multiple activities where I show different instances of WebViews. Lets say I visit www.stackoverflow.com in one activity. If I see a link that points to this adress, it will be shown in purple (showing that it has been visited). Now, I have tried clearing every WebView object in multiple ways, and in different stages (including after loading URL's for every WebView object). E.g., these are the functions I've tried calling for each WebView object:
Just after finding each layout element in my activities' onCreate (findViewById...), I am setting:
wv.getSettings().setAppCacheEnabled(false);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
And then, in onPageFinished in my WebViewClient:
wv.clearHistory();
wv.clearCache(true);
wv.clearFormData();
wv.clearMatches();
wv.clearSslPreferences();
I am only looking for some guidance on how to think on this matter, rather than a full code example. Hence, I do not provide full code for my specific case.
Edit:
Actually, if there is a way to clear a SPECIFIC website URL from an instance of a WebView, I would really prefer this.
I have tried overridding doUpdateVisitedHistory like this:
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
super.doUpdateVisitedHistory(view, url, isReload);
view.clearHistory();
}
I am making one android app where one setting is must every user need to update that which is pincode.
So after successful login, if the user has not updated pincode yet, it needs to be updated mandatory before navigating to any other screen.
What is the best way to implement this? Any Ideas?
Update:
After answer, I meant to say that setting I will be fetching from firebase database as boolean flag. So my actual query is how to show that as a mandatory to get the user update? (i.e) either navigating to different activity or showing popup and getting the setting etc. I need UI design ideas.
What is the best practice?
It is not clear what is the point of this, and if you have a server side that controls that stuff, but I'll try to give a help.
If you have a Server controlling authentication:
On login, call the API of your service to check if has happened or not. You could save the answer in a "shared preference" (Android Documentation), so you don't call your API every time.
If you only want to have the application on a client side:
Use 1 to store values that indicate if the desired action was performed or not, which is verified right after the login.
EDIT:
If the action is mandatory, you could send the user to an activity to update the pin, this will happen until the action is performed.
Client side approach:
You can use SharedPreferences to persist a value, like a simple boolean, that will inform you if that the user already updated the pincode or not.
I would recommend you to perform the check in the onResume() of your Launcher Activity.
Putting it simple and explicit:
public static final String PREF_FILE_NAME = "PrefFileName";
public static final String PREF_IS_PIN_CODE_UPDATED = "PREF_IS_PIN_CODE_UPDATED";
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
Boolean isPinCodeUpdated = prefs.getBoolean(PREF_IS_PIN_CODE_UPDATED, false);
if (isPinCodeUpdated) {
// You can proceed
} else {
// Force the user to update the pin code, by prompting for instance a dialog where he can change the pin with setCancelable(false)
}
}
After you know that your user already updated the pin code you just need to set the preference to true.
SharedPreferences.Editor editor = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE).edit();
editor.putBoolean(PREF_IS_PIN_CODE_UPDATED, true);
editor.apply();
After this every time the user opens the app it will pass in the onResume() method and your isPinCodeUpdated var will be true and the app should proceed as usual.
From you question I am assuming that you didn't want a response in terms of UI but how could you have the information persisted.
Note: This implementation fails for multiple users in the same device. With few tweaks you can make it work for multiple users in the same device but in my opinion this verification should be done server side.
I need to go to the same page on a PDF document after screen change. I have found how to navigate to page here but I failed to get the current page number.
I have found some codes regarding this in MuPDF library but couldn't call it from main activity. In PageView class (MuPDFPageView extends PageView):
public int getPage() {
return mPageNumber;
}
The problem is I don't know how to call this from main activity. I tried something like this:
final MuPDFPageView pageView = new MuPDFPageView(getApplicationContext(), filePickerSupport, core, parentSize, sharedHqBm);
int i = pageView.getPage();
But failed to fill the parameters in. Anybody has a better or easier way to get the current page number?
Within MuPDFActivity, you can call:
mDocView.getDisplayedViewIndex()
and that will return you the current page number.
I am a total newbie at android (as well as this is my first post on StackOverflow) and I was wondering if there is a way of passing control from a web view displaying HTML to the actual android code. To make it a little more clear:
Suppose I have a HTML code that I am displaying in the web view in android, now I click the submit button (which is in the HTML page), is there a way to use that click to call a method in the android code?
A very high level view can be something like
if(Submit is clicked){ //submit would be the submit button in the html page
call xyz(); //xyz would be the android method
}
I would really appreciate it if you guys can help.
It is possible, refer to this.
There is a simple example there that should kick you off.
Javascript is the word of the day.
Use addJavaScriptInterface() to put a reference in the Javascript environment to some Java object.
Here is a sample application demonstrating this, to allow a page in a WebView to retrieve a location from the activity hosting the WebView.
You can use WebView addJavascriptInterface to export your method to the HTML page, here is a relevant snippet:
WebViewVariable.addJavascriptInterface(new MyJavaScriptInterface(context), "Android");
Here is the code for MyJavaScriptInterface :
class MyJavaScriptInterface {
Context localContext;
public MyJavaScriptInterface(Context ctx) {
localContext = ctx;
}
public void pageReady() {
// do your work here
}
}
You can now call the above class's method pageReady() from your HTML page like this Android.pageReady(). Remember though that you can only pass primitive data types (If I remember correctly).
Hope it helps
Everything I've read about Intents talks about using them to push data, or to start one Activity from another Activity. I want to pull data from an Activity that's already running.
The Tab Layout tutorial at http://developer.android.com/resources/tutorials/views/hello-tabwidget.html illustrates what I want to do. (My app is doing some engineering calculations instead, but the tutorial code provides a good analogy to my app.) The tutorial creates an app with three tabs, and each tab hosts a separate activity.
To expand on the example in the tutorial, suppose I select an artist in the Artists tab/activity. I want to be able to select the Albums tab/activity and have it display all the albums featuring that artist.
It seems to me that I need to use an Intent to do this. All of the tutorials I've found assume that I would create a "See albums" Button in the Artists tab/activity, and that pressing the Button would execute an Intent that starts the Albums activity and passes artistName.
I DO NOT want to create that Button. Real estate on the Artists layout is precious, and I have a perfectly good Albums tab, AND the HelloTabWidget activity already contains an intent to create the Albums tab.
Besides, a user will want to skip back and forth between Album and Artist in order to change artist selections, and the tabs are a perfectly good way to do this. There's no need to complicate the UI with another button.
So how can I have the Albums activity PULL artistName from the Artists activity when the Albums tab is selected (or the Albums layout is displayed), rather than have the Artists activity START Albums and PUSH the artistName?
Equivalents I can think of from other programming worlds:
Global variables. Discouraged in Android devt, right? And if they do exist, what are they called?
A getter, like artistName = Artists.getArtistName(); . I get the feeling that it's not that easy.
Writing to, and reading from, a file - that is, mass storage or non-volatile memory. I don't need the artistName value to be permanent. It will be reset to null every time the user launches the application.
So how is it done in the Android world? Do I use an Intent - and if so, how?
Global variables were the right answer.
I thought Java discouraged their use, but a couple of links that appeared in the "Related" links on the right margin of this window mentioned them directly. One was "Android: How to declare global variables?" and the other was "how to pass value betweeen two tab in android". Both pointed to the Application Class as the place to define global variables and methods. Armed with this new knowledge, I found an article called "Android Application Class" on the Xoriant blog that expanded on the StackOverflow answers.
It's best to review those three links first. I need to add some tips to what those authors have said.
Your Application class has to be in its own separate file. (That might be a "duh" to some people, but not to everybody.) Here's a good framework for an example called Something.java:
public class Something extends Application {
// Put application wide (global) variables here
// Constants are final, so they don't have to be private
// But other variables should be declared private;
// use getters/setters to access them
public final boolean FEET = false;
public final boolean METERS = true;
private boolean units = FEET;
#Override
public void onCreate() {
super.onCreate();
// Put any application wide (global) initialization here
}
// Put application wide (global) methods here
public boolean getUnits() {
return units;
}
public void setUnits(boolean whichOne) {
units = whichOne;
}
}
I'm using Eclipse with the ADT plug-in, in Windows XP. Eclipse doesn't always behave properly if you edit XML code directly, so it's best to open AndroidManifest.xml, then select the Application tab and enter your application name in the Name field. You don't need to put a dot or period in front of the name. Just type in the name of your class, like "Globals" or "MyApplication" or whatever. (Note that this is the default application in your Manifest. You don't have to create a separate <application></application> tag.
This step may not be necessary on an actual Android device, but it was necessary for the emulator: you need to use the getApplicationContext() command in every onCreate() and every method that will be accessing the global variables and methods. I tried to put it outside of onCreate() with the rest of my activity wide variables, and it didn't work. Putting it inside every method seems wasteful, but both the emulator and the Android device work fine with it that way. Here's a sample showing how I used it:
public void fooBar() {
// Access to global variables and methods
final Something s = (Something)getApplicationContext();
// ...
// This next line demonstrates both a global method and a global variable
if (s.getUnits() == s.FEET) {
// do something with feet
} else {
// do something with meters instead
}
// ...
}
Those were the only hiccups I encountered. The three references that I have listed, taken together, are otherwise pretty complete.