0

I have a WebView along with edit text as a search bar but instead of typing the whole url in the field, i want it to make searches also like "facebook" and "google" i found one answer here but i have no idea how to do it, can anybody help me with this?

How to show Android Google default search results in webview?

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
private WebView mWebView;

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

    CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
    mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("http://www.google.com/"); //Set Home page
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
    mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
    mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
    mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
    mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback     on touch
    mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
    //mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls 
    //mWebView.getSettings().setDisplayZoomControls(false);//Requires Api 11
    mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
    String appCachePath =     getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
    mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
    mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
    mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)


}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{


webview.loadUrl(url);
return true;
}
}

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {

 if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())

{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Community
  • 1
  • 1
Apk
  • 153
  • 1
  • 2
  • 18

1 Answers1

0

As it is stated in that answer, you need to get the text you get from the EditText in the onClick method of the button and append it to the URL https://www.google.com/search?q=query_string

AndyFaizan
  • 1,833
  • 21
  • 30
  • Have you learnt the basics of layouts and views? I don't see any EditText where you'd enter any search queries. (Not being rude or anything.) – AndyFaizan Feb 15 '14 at 09:04
  • Hmmm thought so. Well, you need to add an editText and a button. Enter the search query in the editText and in the onClick method of the button, you need to fetch the search query and do the operation I mentioned in the answer. If you don't know how to handle editText and buttons, you need to learn all that. It's pretty basic stuff. – AndyFaizan Feb 15 '14 at 09:12
  • Lol. Just learn the basics first. That includes views etc. Once you get the hang of it, we can continue from the web view. "Tab sir ke upar se nahi niklega" :P – AndyFaizan Feb 15 '14 at 09:20
  • Try this http://android-helper.blogspot.in/2011/04/android-simple-edittext-example.html – AndyFaizan Feb 15 '14 at 09:23
  • If you've created apps, then what can be the problem?? – AndyFaizan Feb 15 '14 at 09:23
  • Which stage are you stuck on? – AndyFaizan Feb 15 '14 at 16:12
  • Refer the link I posted earlier in the comments. You need to create objects of the EditText and Button views and link them to the elements in the layout using findViewById(). – AndyFaizan Feb 15 '14 at 16:23
  • In that case, you need to learn Java first man. You have to take it one step at a time. – AndyFaizan Feb 15 '14 at 16:29
  • Then just ask some friend who knows Java. – AndyFaizan Feb 16 '14 at 13:31