6

I am using the compatibility package and I have a Fragment who returns a WebView in onCreateView. The problem is if the fragment isn't added during onCreate of the Activity then when a textbox is clicked inside of the WebView the softkeyboard is not displayed. If the device is rotated after the the custom web fragment has been added, recreating the activity, then the softkeyboard is displayed when clicking on a text field.

Just to be clear Here are the two different scenarios

public void onCreate(Bundle state){
   if(state == null){
      WebFragment web = new WebFragment();
      getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
   }
}



public void onClick(View v){
      WebFragment web = new WebFragment();
      getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
}

In the first case when adding the fragment during the Activity onCreate method then the WebView contained in the fragment works as it should when text fields are pressed. However, in the second example nothing happens when clicking a text field in the webview unless you rotate the device after the webview had been displayed. Can someone offer up a solution, if I have to create a new activity for my fragment to work properly it sorta beats the purpose of the fragment in the first place.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • @schwiz: I am not seeing this problem with this sample app: https://github.com/commonsguy/cw-advandroid/tree/master/Honeycomb/FeedFragments This is an RSS feed reader, displaying a feed item's page in a `WebView` inside a `Fragment`. Hence, this fragment is dynamically added using a transaction based on a `ListFragment` item click. The soft keyboard works just fine. – CommonsWare Apr 18 '11 at 21:31
  • @CommonsWare thanks for the reply, I was able to reproduce this bug in the project you linked by making these changes http://pastebin.com/4WGhD0Mk I don't have a tablet so I can't test it on the triple pain set up but my concern is for phones anyways. – Nathan Schwermann Apr 19 '11 at 01:21
  • @schwiz: I don't know what `res/layout/test.xml` is, and I do not know how that `test()` method is called. Please consider ZIPping up a full project that demonstrates the problem and posting it somewhere. – CommonsWare Apr 19 '11 at 13:05
  • @CommonsWare sorry its just a simple layout with a button http://pastebin.com/2kFaa6cA with the attribute onClick="test". Here is the full project let http://dl.dropbox.com/u/673057/FeedFragments.tar.gz – Nathan Schwermann Apr 19 '11 at 17:37
  • @schwiz: The soft keyboard appears perfectly fine in your sample code on a XOOM. I am able to reproduce your problem on phones, though, like a Nexus S and a Droid X. I will try to clean this up and submit a bug report. – CommonsWare Apr 19 '11 at 18:56
  • @CommonsWare thanks for confirming! Guess I'm stuck with using an activity for now, let me know if you can think of a work around. Would be nice if we could get a fix in the compatibility package. – Nathan Schwermann Apr 19 '11 at 21:25
  • @schwiz: In case you want to keep tabs on it, here's the issue I filed with your sample code (trimming out extraneous stuff from my original project) attached: http://code.google.com/p/android/issues/detail?id=16291 – CommonsWare Apr 21 '11 at 23:48
  • @CommonsWare thanks I will be keeping an eye on this for sure! – Nathan Schwermann Apr 22 '11 at 00:59

2 Answers2

12

Add this to the onCreateView method of the Fragment

webview.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
        }
        return false;
    }
});  

Tested with android 2.3.3 and 3.2.0.

Based on issue #7189 and StackOverflow

Community
  • 1
  • 1
k7k0
  • 1,021
  • 9
  • 14
0

As well solution by RuslanK is worked for me

Create a class that extend the WebView

private class MyWebView extends WebView {

public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
 }

// Note this!
@Override
public boolean onCheckIsTextEditor() {
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev)     {
    switch (ev.getAction())         {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            if (!hasFocus())
                requestFocus();
        break;
    }

    return super.onTouchEvent(ev);
}
}

then add to a xml layout your view:

<com.example.view.MyWebView
    android:id="@+id/vw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

and so on, so on int the code...

MyWebView webView = (MyWebView) parent.findViewById(R.id.vw);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.requestFocus(View.FOCUS_DOWN);
webView.loadUrl(url);
Community
  • 1
  • 1
validcat
  • 6,158
  • 2
  • 29
  • 38