HBT
HBT

Reputation: 57

WebView not showing content

all.

I'm having some trouble displaying a url content gotten from a EditText field, on a WebView on Android.

I just want to add a String inserted into an EditText field, to open a url on another activity, where a webView opens with the Google search url, searching for the given text in EditText.

This is my MainActivity:

package com.hbt.activities;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText term;
    String query;
    WebView intview;

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

    term = (EditText) findViewById(R.id.searchtext);
    query = term.getText().toString();

    intview = (WebView) findViewById(R.id.webView1);
    intview.getSettings().setBuiltInZoomControls(true);
    intview.getSettings().setJavaScriptEnabled(true);
    intview.getSettings().setDomStorageEnabled(true);
}

public void openWikipedia(View intview){

     Intent intent = new Intent(this, Wikipedia.class);
     intent.putExtra("query", query);
     startActivity(intent);
    }

public void openGoogle(View view){

     Intent intent = new Intent(this, Google.class);
     intent.putExtra("query", query);
     startActivity(intent);
    }
}

This is my Google.java:

package com.hbt.activities;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View; 
import android.webkit.WebView;
import android.widget.Toast;

public class Google extends Activity {
String query;
WebView intview;

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

public void opengoogle (View webView1){
    intview = (WebView) findViewById(R.id.webView1);
    intview.getSettings().setBuiltInZoomControls(true);
    intview.getSettings().setJavaScriptEnabled(true);
    intview.getSettings().setDomStorageEnabled(true);
    intview.loadUrl("https://www.google.pt");

    String query = "";

    Bundle bundle = getIntent().getExtras();
    if (bundle == null){
        Toast.makeText(getApplicationContext(), "Insert a search item.",Toast.LENGTH_LONG).show();
    }
    if (bundle != null) {
        query = bundle.getString("query");
    }


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK){
        onBackPressed();
        return true;
    }
    else{
        return super.onKeyDown(keyCode, event);
    }
}
}

And finally, the Wikipedia.java:

package com.hbt.activities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class Wikipedia extends Activity {

    String query;
WebView intview;

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

 public void openWikipedia (View view){
     intview = (WebView) findViewById(R.id.webView1);
     intview.getSettings().setBuiltInZoomControls(true);
     intview.getSettings().setJavaScriptEnabled(true);
     intview.getSettings().setDomStorageEnabled(true);

     String query = "";
     Bundle bundle = getIntent().getExtras();
     if (bundle == null){
        Toast.makeText(getApplicationContext(), "Insert a search item.",Toast.LENGTH_LONG).show();
     }
     if (bundle != null) {
         query = bundle.getString("query");
     }

     String url = "en.wikipedia.org/wiki/Special:Search?search="+ query;
     Intent i = new Intent(Intent.ACTION_VIEW);
     i.setData(Uri.parse(url));
     startActivity(i);

}   

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK){
         onBackPressed();
         return true;
     }
     else{
        return super.onKeyDown(keyCode, event);
     }
}
}

I really can't understand why the WebView doesn't show the content. Any help would be extremely appreciated. Thanks in advance.

As asked, here goes the Layout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >



<Button
    android:id="@+id/Wikipedia"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Google"
    android:layout_alignLeft="@+id/Google"
    android:layout_alignRight="@+id/Google"
    android:minEms="6"
    android:onClick="openWikipedia"
    android:text="@string/wikipedia" />

<Button
    android:id="@+id/Google"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:onClick="openGoogle"
    android:text="@string/google" />

<EditText
    android:id="@+id/searchtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Wikipedia"
    android:layout_alignLeft="@+id/Wikipedia"
    android:layout_alignRight="@+id/Google"
    android:layout_marginBottom="18dp"
    android:layout_marginTop="12dp"
    android:hint="@string/text_to_search_" />

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/searchtext"
    android:layout_alignLeft="@+id/searchtext"
    android:layout_alignParentTop="true" />

</RelativeLayout>

Next the Wikipedia layout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    />

</RelativeLayout>

and the Google 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

</RelativeLayout>

They are different because I was trying to approach the matter form several fronts.

Upvotes: 1

Views: 11347

Answers (2)

Kumar Bibek
Kumar Bibek

Reputation: 9117

Ok. The only things that you would want is the MainActivity and the main_activity.xml.

The other things, you don't really need as far as I can see.

Here's the code listing of the MainActivity:

public class MainActivity extends Activity {

    EditText term;
    String query;
    WebView intview;

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

        term = (EditText) findViewById(R.id.searchtext);
        query = term.getText().toString();

        intview = (WebView) findViewById(R.id.webView1);
        intview.getSettings().setBuiltInZoomControls(true);
        intview.getSettings().setJavaScriptEnabled(true);
        intview.getSettings().setDomStorageEnabled(true);
    }

    public void openWikipedia(View intview) {
        String url = "http://en.wikipedia.org/wiki/" + query;
        this.intview.loadUrl(url);
    }

    public void openGoogle(View view) {
        String url = "https://www.google.pt?q="+query;
        this.intview.loadUrl(url);
    }
}

Now, the main activity's layout. No changes required as compared to the original one you have. But, for clarity, I am posting it.

<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=".MainActivity" >



<Button
    android:id="@+id/Wikipedia"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Google"
    android:layout_alignLeft="@+id/Google"
    android:layout_alignRight="@+id/Google"
    android:minEms="6"
    android:onClick="openWikipedia"
    android:text="Wikipedia" />

<Button
    android:id="@+id/Google"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:onClick="openGoogle"
    android:text="Google" />

<EditText
    android:id="@+id/searchtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Wikipedia"
    android:layout_alignLeft="@+id/Wikipedia"
    android:layout_alignRight="@+id/Google"
    android:layout_marginBottom="18dp"
    android:layout_marginTop="12dp"
    android:hint="Text to search" />

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/searchtext"
    android:layout_alignLeft="@+id/searchtext"
    android:layout_alignParentTop="true" />

</RelativeLayout>

Now, once you press the button, Google or Wikipedia, your webview should start loading the display the content on it. Give it some time.

For Wikipedia: I noticed, it has redirects, so finally, it will open the new URL in the default browser. You will need to handle this.

For Google: The URL is probably not setup correctly, but you could look into it.

Though, the main problem of the WebView not loading properly should be fixed now.

--------------------Update-------------------------------

Now, change these things:

MainActivity.class

public class MainActivity extends Activity {

    EditText term;
    String query;
    WebView intview;

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

        term = (EditText) findViewById(R.id.searchtext);
        query = term.getText().toString();

        intview = (WebView) findViewById(R.id.webView1);
        intview.getSettings().setBuiltInZoomControls(true);
        intview.getSettings().setJavaScriptEnabled(true);
        intview.getSettings().setDomStorageEnabled(true);
    }

    public void openWikipedia(View intview) {
        String url = "http://en.wikipedia.org/wiki/" + query;
        this.intview.loadUrl(url);
    }

    public void openGoogle(View view) {
        Intent intent = new Intent(this, Google.class);
        intent.putExtra("query", term.getText().toString());
        startActivity(intent);
    }
}

Google.class

public class Google extends Activity {
    String query;
    WebView intview;

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

    public void opengoogle() {
        intview = (WebView) findViewById(R.id.webView1);
        intview.getSettings().setBuiltInZoomControls(true);
        intview.getSettings().setJavaScriptEnabled(true);
        intview.getSettings().setDomStorageEnabled(true);
        intview.loadUrl("https://www.google.pt");

        String query = "";

        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            Toast.makeText(getApplicationContext(), "Insert a search item.",
                    Toast.LENGTH_LONG).show();
        }
        if (bundle != null) {
            query = bundle.getString("query");
            intview.loadUrl("http://www.google.com?q=" + query);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

Upvotes: 1

brwngrldev
brwngrldev

Reputation: 3704

Add this inside your onclick methods to get the current value of the EditText, before sending the intent: query = term.getText().toString();

public void openWikipedia(View intview){
     query = term.getText().toString();
     Intent intent = new Intent(this, Wikipedia.class);
     intent.putExtra("query", query);
     startActivity(intent);
    }

public void openGoogle(View view){
     query = term.getText().toString();
     Intent intent = new Intent(this, Google.class);
     intent.putExtra("query", query);
     startActivity(intent);
    }

Set up the webviews in onCreate():

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

    intview = (WebView) findViewById(R.id.webView1);
    intview.getSettings().setBuiltInZoomControls(true);
    intview.getSettings().setJavaScriptEnabled(true);
    intview.getSettings().setDomStorageEnabled(true);
    intview.loadUrl("https://www.google.pt");

    String query = "";

    Bundle bundle = getIntent().getExtras();
    if (bundle == null){
        Toast.makeText(getApplicationContext(), "Insert a search item.",Toast.LENGTH_LONG).show();
    }
    if (bundle != null) {
        query = bundle.getString("query");
    }

}

That should work.

Upvotes: 0

Related Questions