Question

I have a problem with the concatenation of the strings that I will I enter.

This is the class:

package com.isma.multisitesearch.Siti;

import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Webviews.GoogleWebView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Google extends Activity implements OnClickListener {
    private String TAG_ACT = "Caricamento ricerca";
    public EditText googletext;
    private Button googlebutton;
    private String googleurl = "https://www.google.it/search?q=";
    public static String newgoogleurl;
    public String space = " ";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.google);
        googletext = (EditText) findViewById(R.id.googletext);
        googlebutton = (Button) findViewById(R.id.googlebutton);
        googlebutton.setOnClickListener(this);
        newgoogleurl = googleurl + googletext.getText().toString();
        newgoogleurl.replaceAll(space, "%20");
        System.out.println(newgoogleurl);
    }

    @Override
    public void onClick(View v) {
        Log.v(TAG_ACT, "in corso");
        Intent intent = new Intent(Google.this, GoogleWebView.class);
        startActivity(intent);
    }
}

And this is the WebView to which I want to get the concatenation:

package com.isma.multisitesearch.Webviews;

import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Siti.Google;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GoogleWebView extends Activity {
    private WebView googlewebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlewebview);
        googlewebview = (WebView) findViewById(R.id.googlewebview);
        googlewebview.getSettings().setJavaScriptEnabled(true);
        googlewebview.getSettings().setLoadsImagesAutomatically(true);
        googlewebview.setWebViewClient(new WebViewClient());
        googlewebview.loadUrl(Google.newgoogleurl);
    }
}

The app works fine but when I go to write the search text in the EditText, the google but I get the main page where you can enter the search instead of the url with already entered the search text.

I hope you know to help me, after this I was able to run the app.

Was it helpful?

Solution

Try this:

@Override
public void onClick(View v) {
    newgoogleurl = googleurl + googletext.getText().toString();
    newgoogleurl = newgoogleurl.replaceAll(space, "%20");
    Log.v(TAG_ACT, "in corso");
    Intent intent = new Intent(Google.this, GoogleWebView.class);
    startActivity(intent);
}

OTHER TIPS

Note that

newgoogleurl.replaceAll(space, "%20");

has no effect, you need to code:

newgoogleurl = newgoogleurl.replaceAll(space, "%20");

Have a look here:

http://javarevisited.blogspot.it/2011/12/java-string-replace-example-tutorial.html

Try below code:

String url;
url = googleurl+googletext.getText().toString();

if( url.contains(" ")){
    newgoogleurl=url.replaceAll(" ","%20");
}else{
    newgoogleurl=url;
}

This is containing validation part also.

Strings are immutable. That means that you cannot modify an instance of String, and need to create a new one each time you want to modify it.

In your case, replaceAll does not modify the String you call it on, but rather returns a new String with your modification applied.

Which is why you need to affect the returned value to the reference of your String.

newgoogleurl = newgoogleurl.replaceAll(space, "%20");

Moreover, you should read the documentation for replaceAll, because I don't think it does what you want. You probably want to use replace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top