get WebView instances stored in a ViewAnimator - android

I have some WebView instances that i stored in a ViewAnimator programmatically. Firstly i constructed each WebViesw. Then i add them into the ViewAnimator
WebView webview = new WebView(context);
viewAnimator.addView(webview, index, layoutparams);
While a webView being added into the ViewAnimator, an AsyncTask instance is executed to load webpage from a specific URL.
My question, how can i get a webView from that ViewAnimator for a particular index value. I want to do some specific actions to the webView, such as reload or stop. I have done this way..
WebView currentWebView = (WebView) viewAnimator.getChildAt(index);
currentWebView.reload();
But the currentWebView returned null. Is that the right way to get webView instance from ViewAnimator? Please help...

I don't know why getChildAt() return null, but as a workaround, you can maintain your own list of WebView. This is a stupid question, but are you sure your index is valid?

Related

All WebViews somehow shares visited history

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();
}

Use Resources in Fragments

I am using a ViewPager in my app, based on a FragmentStatePagerAdapter.
I have hundreds of Fragments that are loaded and displayed dynamically in this ViewPager, all this is working well but I sometimes get null pointers when trying to access resources using:
getResources().getString(R.id.example);
What is the best way to access Resources in Fragments? Would it be a good idea to create a global variable and initialize it (maybe in onActivityCreated())?
I am also concerned about the memory itself, as I am not sure which one is the heaviest between using a global variable on every fragment or directly accessing the resources from the Activity (getResources()).
Make this a global variable
private View view;
and then on onCreateView(), add this line
view = inflater.inflate(R.layout.fragment, container, false);
and now you can access the resources
view.getResources().getString(R.string.title);
you can use:
getActivity().getResources().getString(R.id.example);

Android: setImgVisibility() error

In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)

Passing control from html to android to call an android method

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

ArrayAdapter for Webview inside Gridview

Following on from an earlier question I'm trying to get multiple (widgets or in this case WebViews) inside a GridView.
So I wasn't really sure how to go about it whether to make my own adapter (seemed scary at the time) or create an ArrayAdapter<WebView>.
I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.
GridView contentGrid;
LinearLayout contentLayout;
WebWidget[] webWidgets;
WebWidget web1;
WebWidget web2;
WebWidget web3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web1 = new WebWidget(this, "http://www.google.ie");
web2 = new WebWidget(this, "http://www.facebook.com");
web3 = new WebWidget(this, "http://www.youtube.ie");
webWidgets = new WebWidget[]{web1,web2,web3};
contentGrid = (GridView) findViewById(R.id.contentGrid);
//Heres my attempt at an adapter for webview
ArrayAdapter<WebView> adapter = new ArrayAdapter<WebView>(this,
android.R.layout.simple_list_item_1, webWidgets);
contentGrid.setAdapter(adapter);
Unfortunately what comes out is the string version of the WebView.
So question is can I do it this way or am I better of making my own CustomAdapter?
NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1
No matter how you implement your Adapter, you cannot reliably put scrollable widgets in other scrollable widgets, at least where they scroll in the same direction. Since both WebView and GridView scroll vertically, you will get unreliable results trying to combine them this way.
With that, on to some of your statements and questions:
I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.
An ArrayAdapter can adapt a Java array or an ArrayList of whatever data type you like.
Unfortunately what comes out is the string version of the webview...NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1
The second constructor variable is what you want the cells in your GridView to look like. In your case, you are specifying a built-in layout resource that is a TextView. Hence, you are telling Android you want all your grid cells to be TextView widgets. Android will call toString() on the objects in your array, pour that result into the TextView, and the TextViews will go in your grid cells.
If you want your ArrayAdapter to be returning things other than a TextView, you will need to override getView() and handle more of that yourself, possibly using a layout file of your own creation.
So question is can I do it this way or am i better of making my own CustomAdapter?
As noted at the outset of my answer, what you want simply will not work reliably.
Pretending for the moment that having WebViews in a GridView would work, the simplest solution would be for you to override getView() in your own subclass of ArrayAdapter, as I mentioned previously. Here is a free excerpt from one of my books that goes over this process.

Categories

Resources