Mayur More Mayur More - 4 months ago 53
Android Question

Android WebView findnext does not scroll to next selected word

When I call

webView.findAll(word);
it does show all the occurences as selected and
webView.findNext(true)
also shows a different background color, but it does not scroll the page to the next selected word.

I have to manually scroll the page to see the selected words. I am expecting that, whenever I call
webView.findNext(true)
it should auto scroll the page to the next word. I am using Android 6.0 device. I have also tried with
webView.findAllAsync(word);
. But I am not able to scroll the webView page to the selected word. Please help!

EDIT:

layout file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >

<WebView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_for_dummies"
android:textColor="@android:color/black"
android:padding="7dp"/>
</ScrollView>


menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools" >

<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="ifRoom|collapseActionView"/>
<item
android:id="@+id/next"
android:icon="@android:drawable/arrow_down_float"
android:title="Next"
android:visible="false"
appcompat:showAsAction="always"/>
<item
android:id="@+id/prev"
android:icon="@android:drawable/arrow_up_float"
android:title="Previous"
android:visible="false"
appcompat:showAsAction="always"/>

</menu>


public class DummiesActivity extends AppCompatActivity {

WebView wvDummies;
Context context;
Float scale = 0.5F;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.dummies_guide);

wvDummies = (WebView) findViewById(R.id.tv_for_dummies);

wvDummies.getSettings().setSupportZoom(true);
wvDummies.getSettings().setBuiltInZoomControls(true);
wvDummies.getSettings().setJavaScriptEnabled(true);
wvDummies.loadData(getString(R.string.app_name123), "text/html",
"UTF-8");

getSupportActionBar().setTitle("Dummies guide");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.dummies_search_menu, menu);

SearchManager searchManager = (SearchManager) context
.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Search");

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@SuppressLint("NewApi")
@Override
public boolean onQueryTextSubmit(String query) {

if (!query.isEmpty()) {
menu.findItem(R.id.next).setVisible(true);
menu.findItem(R.id.prev).setVisible(true);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
wvDummies.findAllAsync(query);
} else {
wvDummies.findAll(query);
}

try {
Method m = WebView.class.getMethod("setFindIsUp",
Boolean.TYPE);
m.invoke(wvDummies, true);
} catch (Throwable e) {
e.printStackTrace();
}
} else {
menu.findItem(R.id.next).setVisible(false);
menu.findItem(R.id.prev).setVisible(false);
}
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
try {
Method m = WebView.class.getMethod("setFindIsUp",
Boolean.TYPE);
m.invoke(wvDummies, false);
} catch (Throwable ignored) {
}
}
return true;
}
});

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
Log.d("", "setOnCloseListener called");
try {
Method m = WebView.class.getMethod("setFindIsUp",
Boolean.TYPE);
m.invoke(wvDummies, false);
} catch (Throwable ignored) {
}
return false;
}
});
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
finish();
break;

case R.id.next:
wvDummies.findNext(true);
break;

case R.id.prev:
wvDummies.findNext(false);
break;
}
return super.onOptionsItemSelected(item);
}
}

Answer

You are not able to auto scroll with the findNext() because of the ScrollView in the layout file. Change it to the following code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <WebView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tv_for_dummies"
        android:textColor="@android:color/black"
        android:padding="7dp"/>
</LinearLayout>
Comments