0

I have an Edit Text Field in my layout, but the problem is, it's not working because i have to type the full url in the field to open a website like "http://www.google.co.in" but i want it to be able to search also like "google" or "android" but i have no idea how to do it. below is my code (there is no edit text in the code). i found an answer here but i don't know how to do it

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);
}
}

Xml

<RelativeLayout 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"
tools:context=".WebViewActivity" >

  <LinearLayout
    android:id="@+id/urlContainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/urlField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="Enter URL to open" />

    <Button
        android:id="@+id/goButton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Open" />
</LinearLayout>

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/urlContainer" />

</RelativeLayout>
Community
  • 1
  • 1
Apk
  • 153
  • 1
  • 2
  • 18

1 Answers1

0

I have done the following steps,check if its useful for you.

  1. In manifest file add internet permission
    uses-permission android:name="android.permission.INTERNET"

  2. I created a layout for entering the search and another layout for displaying the result.

my main.xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<AutoCompleteTextView
    android:id="@+id/editTextInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enter search text" >

    <requestFocus />
</AutoCompleteTextView>


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:onClick="onSearchClick"
    android:text="Search" />

 </LinearLayout>

and my output layout with the webview looks like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</LinearLayout>
  1. my activity java file i wrote a on clik action for the button in main.xml as.

    public void onSearchClick(View v) { final WebView webView; final EditText editTextInput; try { editTextInput = (EditText) findViewById(R.id.editTextInput); String term = editTextInput.getText().toString(); setContentView(R.layout.webviewup); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); String url = "https://www.google.com/search?q=" + term; webView.loadUrl(url);

    } catch (Exception e) {

    } }

Thats all :)

the_learner
  • 196
  • 2
  • 9