Search code examples
androidandroid-studioandroid-webviewsearchview

Values are passing from edittext to veriables


<?xml version="1.0" encoding="utf-8"?>
<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">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sourceET"
        android:hint="Source City"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/destinationET"
        android:hint="Destination City"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/searchBtn"
        android:text="Search" />

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

</LinearLayout>

This is xml

public class MainActivity extends AppCompatActivity {

    WebView webView;
    EditText sourceCity,destinationCity;
    Button searchBtn;
    String url = "";

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

        sourceCity = findViewById(R.id.sourceET);
        destinationCity = findViewById(R.id.destinationET);
        searchBtn = findViewById(R.id.searchBtn);
        webView = findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());

        String source = sourceCity.getText().toString();
        String dest = destinationCity.getText().toString();

        url = "https://www.google.com/maps/dir/"+source+"/"+dest;


        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl(url);

            }
        });
    }
}

This is java

I have make search view in web view.I am using google map url and concat two veriables which store edit text values but values are not passing from edittext to veriable why values are not passing i am trying to find problem.It's does'not show any error.How to fix it.


Solution

  • Because you need to initialize this variables to the editText values within the onclick() of the button.

    public class MainActivity extends AppCompatActivity {
    
        WebView webView;
        EditText sourceCity,destinationCity;
        Button searchBtn;
        String url = "";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sourceCity = findViewById(R.id.sourceET);
            destinationCity = findViewById(R.id.destinationET);
            searchBtn = findViewById(R.id.searchBtn);
            webView = findViewById(R.id.webview);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient());
    
            searchBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                String source = sourceCity.getText().toString();
                String dest = destinationCity.getText().toString();
                url = "https://www.google.com/maps/dir/"+source+"/"+dest;
    
                webView.loadUrl(url);
    
                }
            });
        }
    }
    

    Use the above code block for the desired results.