Basic WebView
First, we are going to want to set up a WebView to add search functionality to.
SearchDemoActivity.java
package com.search.demo; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class SearchDemoActivity extends Activity { WebView mWebView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.webview); mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/"); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
SearchDemo Manifest
In your manifest make sure to add the internet permission so your web page loads.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.search.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SearchDemoActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>Test to make sure that your WebView is functioning correctly before moving on to the next step.
Add A Menu Option for Search
Now that we have a webpage loaded in our WebView we will add a search menu option for when the user wants to search the web page. Here is what our SearchDemoActivity.java looks like with the search menu option added:
SearchDemoActivity.java with Search Menu Option
package com.search.demo; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebView; public class SearchDemoActivity extends Activity { WebView mWebView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.webview); mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/"); } private static final int SEARCH_MENU_ID = Menu.FIRST; @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); menu.add(0, SEARCH_MENU_ID, 0, "Search"); return true; } public boolean onPrepareOptionsMenu(Menu menu){ super.onPrepareOptionsMenu(menu); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case SEARCH_MENU_ID: return true; } return true; } }Search User Interface
Now that we have a webpage to search and a menu option to select search we need to create a user interface to get the search word from the user. Also we will enable the user to cycle through the found words and to close the search interface. To do this I create a dynamic Linear Layout. I'm sure you could create a static UI using XML, but this is the way that made since to me. First add a Linear Layout to our main.xml:
Main.xml with Linear Layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/layoutId" ></LinearLayout> </RelativeLayout>Next we will have our Edit Box and buttons appear when the user selects search from the options menu. We will add a function called search to contain all of our search functionality. Here is what our SearchDemoActivity.java looks like with the added Linear Layout items:
SearchDemoActivity.java with Edit Box and Buttons
package com.search.demo; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; public class SearchDemoActivity extends Activity { WebView mWebView; private LinearLayout container; private Button nextButton, closeButton; private EditText findBox; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.webview); mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/"); } private static final int SEARCH_MENU_ID = Menu.FIRST; @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); menu.add(0, SEARCH_MENU_ID, 0, "Search"); return true; } public boolean onPrepareOptionsMenu(Menu menu){ super.onPrepareOptionsMenu(menu); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case SEARCH_MENU_ID: search(); return true; } return true; } public void search(){ container = (LinearLayout)findViewById(R.id.layoutId); nextButton = new Button(this); nextButton.setText("Next"); container.addView(nextButton); closeButton = new Button(this); closeButton.setText("Close"); container.addView(closeButton); findBox = new EditText(this); findBox.setMinEms(30); findBox.setSingleLine(true); container.addView(findBox); } }Search Functionality
Now we just need to add functionality to our buttons and edit box. To do this we will use the WebView built in search functionality. Most notably is the WebView.findall(). This is going to do the search functionality for us. Then we just have to provide the button functionality. To do this we will use WebView.findnext() and LinearLayout.removeAllViews(). We will also add a hint that is displayed in our edit box using EditText.setHint(). Here is what our SearchDemoActivity.java looks like with the added search functionality:
SearchDemoActivity.java with Search Functionality
package com.search.demo; import java.lang.reflect.Method; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; public class SearchDemoActivity extends Activity { WebView mWebView; private LinearLayout container; private Button nextButton, closeButton; private EditText findBox; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.webview); mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/"); } private static final int SEARCH_MENU_ID = Menu.FIRST; @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); menu.add(0, SEARCH_MENU_ID, 0, "Search"); return true; } public boolean onPrepareOptionsMenu(Menu menu){ super.onPrepareOptionsMenu(menu); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case SEARCH_MENU_ID: search(); return true; } return true; } public void search(){ container = (LinearLayout)findViewById(R.id.layoutId); nextButton = new Button(this); nextButton.setText("Next"); nextButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ mWebView.findNext(true); } }); container.addView(nextButton); closeButton = new Button(this); closeButton.setText("Close"); closeButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ container.removeAllViews(); } }); container.addView(closeButton); findBox = new EditText(this); findBox.setMinEms(30); findBox.setSingleLine(true); findBox.setHint("Search"); findBox.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event){ if((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))){ mWebView.findAll(findBox.getText().toString()); try{ Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE); m.invoke(mWebView, true); }catch(Exception ignored){} } return false; } }); container.addView(findBox); } }And that's all there is to it. Your WebView should now have complete search functionality with search word highlighting similar to a web browser.
I have problems with HTC Sense UI =/ can't use the Deletekey to delete chars from the entered string.. any advise ?
ReplyDeleteI'm not sure. This design uses EditText which is standard android Class. I would try creating a new project to test EditText on that phone. It may be because it is being created dynamically. So I would try putting it in XML and see if that works.
DeleteThis comment has been removed by the author.
Deletedont hilight search word in webview please give correct way
ReplyDeleteit give mediaplayer error 2
ReplyDeletewhat's problem
plz give solution my problem
Why It not highlight First world. If i want to search THE and write T than it not highlight but when i write H than it highlight T. How can solve this problem.
ReplyDeletehighlighted text not work for android 4.0
ReplyDeleteit working great for android 2.2 but it no show highlight for 4.0
ReplyDeleteHi,The type is not advisable for a small business because the URL is not search engine friendly in Web Design Cochin and the limited server capabilities your ISP offer may not be sophisticated enough for a small business website.Thanks.....
ReplyDeleteThe method findAll(String) from the type WebView is deprecated ? chane ?
ReplyDeleteThanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort..Android Training institute in chennai with placement | Android Training in chennai |Android Training in Velachery | android development course fees in chennai
ReplyDeleteeven after clicking the close button the search results color is not disappearing. Help me
ReplyDeletecan't resolve findall method
ReplyDeleteI have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeletejava training in annanagar | java training in chennai
java training in marathahalli | java training in btm layout
java training in rajaji nagar | java training in jayanagar
java training in chennai
It can be extremely frustrating to receive mysterious free phonecalls when you not recognize the phone number or caller. Like most people, you will probably want to find out who the mysterious caller is. If there is no message left by the caller if may add to the frustration. You might have just missed a very important call.
ReplyDeleteI would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
ReplyDeletePython training in marathahalli
Python training institute in pune
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteangularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
Useful blog, This is what I have looked for. Share more like this.
ReplyDeleteUiPath Training in Chennai
UiPath Training Institutes in Chennai
RPA Training in Chennai
Robotics Process Automation Training in Chennai
Blue Prism Training in Chennai
UiPath Training in Velachery
UiPath Training in Tambaram
Interesting blog with lots of information, keep up the good work and share more like this.
ReplyDeleteData Science Training in Chennai
Big Data Analytics Courses in Chennai
Machine Learning Training in Chennai
Microsoft Azure Training in Chennai
DevOps Training in Chennai
AWS Training in Chennai
Data Science Training in OMR
Data Science Training in Porur
Really this Blog is very advisable. very Interesting while reading this Concept.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
Thanks for the very useful article!
ReplyDeleteKeep us posted!
data science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
Join the top Python Training in Hyderabad at AI Patasala and take your career to an entirely new level in the field.
ReplyDeleteOnline Python Training in Hyderabad
Casinos Near Me - Harrah's Casino, Marina District, CA
ReplyDeleteVisit Harrah's 의왕 출장마사지 Marina District and other places to stay and play casino games 보령 출장샵 & get 대구광역 출장안마 in on 제주 출장마사지 the action 진주 출장샵 today! Click here for details of all the Harrah's Casinos