Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid WebView focus problem
    primarykey
    data
    text
    <p>I am fighting with focus management of WebView: WebView messes with focus management of classic components. Here is a simple application with just an EditBox and a WebView in it which loads Google!</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:and="http://schemas.android.com/apk/res/android" xmlns:webtag="http://schemas.android.com/apk/res/com.webtag" and:orientation="vertical" and:layout_width="fill_parent" and:layout_height="fill_parent"&gt; &lt;LinearLayout and:id="@+id/HorizontalScrollView02" and:layout_width="fill_parent" and:layout_height="wrap_content"&gt; &lt;EditText and:id="@+id/EditText01" and:layout_width="wrap_content" and:layout_height="wrap_content" and:text="@+id/EditText01"/&gt; &lt;/LinearLayout&gt; &lt;WebView and:id="@+id/uiContent" and:layout_weight="1" and:layout_width="fill_parent" and:layout_height="fill_parent"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>And here is the Java code:</p> <pre><code>public class WebtagActivity extends Activity { // UI components. private WebView mContent; private EditText mUrl; @Override public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContent = (WebView) findViewById(R.id.uiContent); mContent.getSettings().setJavaScriptEnabled(true); mContent.loadUrl("http://www.google.fr/"); } } </code></pre> <p>Based on this very simple example, it's impossible to get focus on the WebView (i.e. the google search terms input box)! However when adding "mContent.requestFocus();" during initialization, things get better. I can switch from my EditText to my WebView if I click on WebView first only... But behaviour gets soon very "erratic", with WebView sometimes not getting focus when touching it, sometimes getting it but staying with a focus rectangle around WebView fields (orange or green depending on your phone) after another touch to go back on EditText component (in which I can write text)!</p> <p>Anyway, a solution I found is to add a touchListener and request focus when clicking on a WebView:</p> <pre><code>mContent.setOnTouchListener(new View.OnTouchListener() { @Override 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; } }); </code></pre> <p>This solves almost all problems (switching from classic components to WebView and writing text) except one: WebView keeps the focusRectangle (green or orange) around fields whether it has focus or not. In the below screenshot, my EditText has the focus and receive text but WebView still looks like it has focus. I tried clearFocus but that doesn't do anything:</p> <p><a href="http://img139.imageshack.us/i/deviceny.png/">See the result</a></p> <p>Any idea how to solve this? Thanks a lot for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload