BaseInputConnection commitText not sending text to a WebView - android

I'm developing a simple web browser that has Buttons on top of a WebView. These buttons work as text input shortcuts customizable by the user. Let's say the user types a lot "1234567890", he can set that text in a button shortcut and every time he taps it, the text is sent to the WebView. To accomplish this I'm using BaseInputConnection.
My problem is that the text is not sent to the WebView. But if it's a shortcut for a key press it works.
This is what I have:
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
wv = findViewById(R.id.wvMain);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setWebViewClient(new WebViewClient() {
...
}
...
}
public void onShortcutButtonClick(View view) {
Shortcut sc = (Shortcut) view.getTag(); // Shortcut is a custom class that holds the type and text of the shortcut among other stuff.
switch (sc.getType()) {
case Shortcut.TYPE_KEY: {
BaseInputConnection inputConnection = new BaseInputConnection(wv, true);
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0)); // Works! "0" is sent to whatever field the cursor is in the page of the WebView.
}
break;
case Shortcut.TYPE_TEXT: {
BaseInputConnection inputConnection = new BaseInputConnection(wv, true);
inputConnection.commitText("1234567890", 0)); // Doesn't work.
}
break;
...
}
}
sendKeyEvent works but commitText doesn't. I don't know if this is the best way to send text to a WebView. I also tried sending the text to the active element of the page using JavaScript but it doesn't work most of the time.

Related

Unable to program detection behavior on Webview and xml Textview

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

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 disable text selection in a webview in Android?

My application playing HTML promotions in WebView, Html promotions having text so if user press long click android standard dialog appear Copy/Share/Find/Web Search, so is there any option to disable text selection dialog for webview ?
Help me on the same.
You need to do this way:
WebView webView = (WebView) findViewById(R.id.webView);
webView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
webView.setLongClickable(false);
// Below line prevent vibration on Long click
webView.setHapticFeedbackEnabled(false);
Hope this will help you.

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