0

I am new at android and I am trying when someone type a string in search view when click search then this typed string or whatever should in web view at the google search for this. I have manually gave a string and it is working but I need to implement a search. I have added uses permission for Internet in Manifest

This is my code.

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/webView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22000003">

        <EditText
            android:id="@+id/search_for"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType=""
            android:autofillHints="" />
        <Button
            android:id="@+id/myButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </SearchView>

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="45dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.47000003" />
</android.support.constraint.ConstraintLayout>

 public class MainActivity extends AppCompatActivity {
    private WebView webView;

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

        final EditText edit = (EditText) findViewById(R.id.search_for);
        final Button searchBtn = (Button) findViewById(R.id.myButton);
        final String searchText = edit.getText().toString();

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    webView.loadUrl("http://www.google.com/search?q=" + searchText);
            }
        });
    }

}

This is my Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zhuniqia.searchwithus">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>

3 Answers 3

1

Xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/search_for"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_toLeftOf="@id/myButton"
        android:layout_height="wrap_content"
        android:hint="Search" />

    <Button
        android:id="@+id/myButton"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_width="60dp"
        android:text="Go"
        />

</RelativeLayout>

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

Java Class

public class MainActivity extends AppCompatActivity {

public WebView myWeb;
public Button myBtn;
public EditText myEdit;

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

    myWeb = findViewById(R.id.webView);
    myBtn = findViewById(R.id.myButton);
    myEdit = findViewById(R.id.search_for);

    myEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                final String myText = myEdit.getText().toString();
                myWeb.loadUrl("http://www.google.com/search?q=" + myText);
                return true;
            }
            return false;
        }
    });



    myBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String myText = myEdit.getText().toString();
            myWeb.loadUrl("http://www.google.com/search?q=" + myText);
        }
    });



    myWeb.getSettings().setJavaScriptEnabled(true);
    myWeb.setWebChromeClient(new WebChromeClient());
    myWeb.setWebViewClient(new WebViewClient());
    myWeb.loadUrl("https://www.google.com");


}}
0

i cannot give you a full answer now, but maybe it will useful for you to think towards setText and getText functions

0

Use EditText and a Button

final EditText edit =  (EditText) findViewById(R.id.text_xyz);
final Button searchBtn = (Button) findViewById(R.id.myBuuton);

get text from EditText

String searchtext = edit.getText().toString();

Set Button to search the text you entered

searchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            webView.loadUrl("http://www.google.com/search?q=" + searchtext);
        }
    });
4
  • please check new answer Oct 31, 2018 at 11:14
  • It is working man thank you so much, but when click on keyboard at the search icon this doesn't work only in the button GO do you have any idea how to make that to work to ? Oct 31, 2018 at 11:15
  • Use this block of code ` myEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { final String myText = myEdit.getText().toString(); myWeb.loadUrl("google.com/search?q=" + myText); return true; } return false; } });` Oct 31, 2018 at 14:19
  • Let us continue this discussion in chat. Oct 31, 2018 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.