Unable to program detection behavior on Webview and xml Textview - android

I am trying to change the visibility of my textview inside onCreate method, but I cant seem to get a handle for it. Or, perhaps something else is wrong. Also, the getUrl() from the WebView class is not behaving as it should. It seems that it detects the url i am looking for after i leave the page with the url i am trying to detect. After reading the documentation, it made it somewhat clear based on the definition of the method getUrl(). Here is what the documentation says: "Gets the URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed."
Here's what I mean:
I click on "link within the webpage that has url am trying to detect call it url5", then pressed back button, then click on "some other link within webpage call it url6" . Now, when I go back out of link with url6, the link for url5 is detected and I should be able to setVisibility of my textview as VISIBLE, which i tried but nothing happens. Well, here's my code. Please advise of anything that I might be doing wrong.
"xml format"
<RelativeLayout>
specifications for the relative layout...
<TextView>
android:visibility="invisible"
other specifications such as match_parent for both height and width
/>
<LinearLayout>
specifications of linear layout...
<LinearLayout/>
</RelativeLayout>
"inside onCreate"
relative_layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.content_main, null);
text_view = (TextView)relative_layout.findViewById(R.id.text);
WebView w = (WebView) findViewById(R.id.webview);
w.setWebViewClient(new MyWebViewClient());
//outside of onCreate inside private class MyWebViewClient
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
if(W.getUrl().equals("url I want to detect") ||
W.getUrl().equals("another url I want to detect")){
text_view.setVisibility(View.VISIBLE); //HOWEVER NOTHING SHOWS AT ALL
relative_layout.setVisibility(View.INVISIBLE);
}
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && w.canGoBack()) {
if(text_view.isShown()){
w.goBack();
text_view.setVisibility(View.INVISIBLE);
relative_layout.setVisibility(View.VISIBLE);
}else w.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

Cannot open Android Webbrowser using Action_View

I am trying to open the standard android webbrowser by clicking on a textview.
I defined the android:autoLink="web" in the textview and then use an onTouchListener to start a browserintent:
// On Touch Listener
chatText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent event) {
// view.performClick();
view.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN){
view.performClick();
openBrowser(chatText.getText().toString());
}
return false;
}
});
// Start Browser function
public void openBrowser(String url) {
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (webIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(webIntent);
}
}
However, everytime I click a link on my textview I get the error:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Although I do have the flags added to my newly started activity, anyone knows what I do wrong here?
I am calling the activity from a class ChatArrayAdapter which extends an ArrayAdapter, however, I pass the appropriate context to the newly started activity
Going over your code, your chat text has some URL in it. To make things easier, Android has Linkify class and the android:autoLink XML attribute. These help to automatically highlight links and perform the standard action when you click on them. All of this is handled out of the box.
Your chatText is probably a TextView. You can use one of the two ways I mentioned like so:
In XML:
In your chatText's XML add the following attribute:
android:autoLink="web"
In code:
Linkify.addLinks(chatText,Linkify.WEB_URLS);

How to disable the content touch in WebView without afecting the double tap zoom .

Acually i wanted to load the content in the webview , which is in a xhtml file .
I want to disable the accessibility to the content . Click on the link/texts must not navigate to that particular page .
More particularly , I have the contents page of a book in the xhtml file and click on the contents must not navigate to the particular page .
WebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Will work good but this stops the doulble tap zoom as well . I want to stop accessing the content without affecting the tap zoom .
And one more is that , what does webview.getSettings().setAllowContentAccess(false); refer to in webview .
Thanks in Advance .
If you only want to prevent navigations (user following links) then you need to use the shouldOverrideUrlLoading API:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});

Current webview with CanGoBack feature inside Tab

I am creating application with dynamic tabhost, each tab has dynamic webview using the following tutorial,
http://www.pocketmagic.net/?p=1132
The above is created as Custom control(unable post the source code). I have extended this control with main activity. In keydown event i have checked the webview able to go back or not. If can, then it should load previous url in the current webview. But this is working if i have worked in the same tab. If i have moved two pages and shifted to second tab and then first tab, now pressing back button Application exists. Could you please tell me how to get the current webview to go back.
I have solved this Issue :-)
Solution:
1. Created One class named MyWebView that should extends WebView.
2. Created 4 MyWebView instances from User Side.
3. That MyWebView should have one WebView locally and it should not be static( here i did a mistake).
4. Override the Layout of WebView inside the MyWebView class with Progressbar and WebView inside the FrameLayout.
5. For every tab creation we have to set the Content as instance of MyWebView.
6. Now handle as follows,
UserClass.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView1!=null)
{
currentWebView= webView1.getWebView();
if(currentWebView!=null && currentWebView.canGoBack() == true)
{
currentWebView.goBack();
}
else
//Show Alert to Quit the Application
}
else
//Show Alert to Quit the Application
return true;
}
}
}
MyWebView.java:
public WebView getWebView()
{
return webview;
}
That is worked for me...

Disable selectable links within WebView

I have a webView that is contained within a scrollView. Everything is then contained within a viewPager. When I fling to the next view page, the links that are in the middle of the page are being focused (highlighted with orange around the text). This causes the page to jump down to the nearest link.
Is there a way to disable links from being focusable on touch? I've tried all the settings for the webView such as focusable = false, clickable = false, focusable in touch mode = false, and nothing seems to be working.
I managed to do it programmatically like so:
WebView myWebView = (WebView) storyItem.findViewById(R.id.myWebView);
myWebView.setFocusableInTouchMode(false);
myWebView.setFocusable(false);
This makes the links non-focusable, which solved my problem!
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
This prevents the webview from opening any links and will also allow you to navigate the page without problems

How to load a part of an webpage in android webview?

I would like to make an app, that shows a part of the site.
Lets say that i have this site:
I only want to show this:
https://mail.google.com/mail/?ui=2&ik=d5146163e8&view=att&th=132496ec47863fea&attid=0.1&disp=inline&realattid=f_gsbtyog80&zw
Is there anyway to do this?
I hope some of you could help me.
Gaauwe
You could load the url to a WebView and then set the WebView's scroll position and zoom level to display only a specific part of the web page. If you don't want to change zoom level you could initially scroll the WebView to the required location and then override the WebView's onTouch method so that it will only scroll upto the required cordinates.
public class MyWebViewActivity extends Activity implements PictureListener{
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setPictureListener(this);
// disable scroll on touch
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.loadUrl(url);
}
#Override
public void onNewPicture(WebView view, Picture picture) {
// TODO Auto-generated method stub
webView.scrollTo(x, y);
}
}
One of the way is to use PHP on your own server and CURL the webpage into it and strip out all the unnecessary things .
After that you only load it into your webview.
*This is one of the method, there are others method better then this. *
Another way is coldfusion cfhttp .. not sure,about this http://www.quackit.com/coldfusion/tutorial/coldfusion_http.cfm

Categories

Resources