2

I have an app that uses WebViews (minSdkVersion 17). It would be very helpful if it was possible to add a search (and highlight found result(s)) function. The thing is that my programming skills are very low and the search for the Holy Grail returned no good answers.

I found this topics (and some moe, including other forums): how to search text in webview , Android - Highlight text in WebView above android version 4.0.3 , search text on WebView android and a few others, but all of the are either incomplete for me to understand how they work or the code found is too old and there are way to may errors to fix (due to changes in android studio, I guess) for me to make something useful from them.

My XML from the activity:

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/footer"
    android:layout_below="@+id/layoutId">

    <WebView
        android:id="@+id/webviewautoturisme"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</ScrollView>

<RelativeLayout
    android:id="@+id/layoutId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="Caută"
        android:textColor="#ffffff"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/findBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@+id/nextButton"
        android:hint="Caută după localitate"
        android:maxLines="1" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="95dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">

    <Button
        android:id="@+id/buttoBACK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:layout_centerHorizontal="true"
        android:minWidth="150dp"
        android:text="@string/sBackMainMenu"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />
       </RelativeLayout>
</RelativeLayout>

Is is something that I found, made some small improvements to the code, since it was a bit outdated, and I wanted to use the java also, but I had no luck. I also found out that there is a SearchView, so in another activity I used:

<SearchView
    android:id="@+id/searchField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:queryHint="Căutare după localitate"
    android:orientation="horizontal">
</SearchView>

and the Java :

package ro....;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class PKWinListActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pkwinlist);

    MobileAds.initialize(getApplicationContext(), "ca-app-pub-....");
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("....").addTestDevice("....").addTestDevice("....").build();
    mAdView.loadAd(adRequest);

    findViewById(R.id.buttoBACK)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }

            });

    if (isMobileDataEnabled()) {

        WebView myWebView = (WebView) findViewById(R.id.webviewautoturisme);
        myWebView.loadUrl("https://www...");

        myWebView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

    } else {
        //Mobile data is disabled here
        new AlertDialog.Builder(this).setTitle(getString(R.string.sDataReqMes))
                .setMessage(getString(R.string.sDataReqEnable)).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
            }
        }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }).show();


    }
}


private boolean isMobileDataEnabled() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobileDataEnabled = connectivityManager.getActiveNetworkInfo();
    return mobileDataEnabled != null && mobileDataEnabled.isConnected();
}

}

The problem is that I can't figure out how to do the search button work, since the previous solutions I found are outdated. How to make the search button work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Robert
  • 123
  • 2
  • 9
  • 2
    Please stop [deleting](http://stackoverflow.com/questions/43111636/how-to-search-for-text-in-a-webview-andoid-using-searchview-or-edittext) and [reposting](http://stackoverflow.com/questions/43105157/how-to-search-for-text-in-a-webview-andoid) this question. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working will bump it to the top of the active queue. – Mike M. Mar 31 '17 at 07:20

0 Answers0