user5520878
user5520878

Reputation:

How to show webView and hide other elements in Android

I am very new to Android development, going through some tutorials I developed Android code using Eclipse. Here, I am trying to load a WebView with some URL after clicking a button, and then I want to hide the button.

I am using Linear Layout. For this, I used the following code to get this working but it is not loading the WebView.

public class MainActivity extends ActionBarActivity {

    EditText edit;
    TextView text;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit = (EditText) findViewById(R.id.editText);
        text = (TextView) findViewById(R.id.textView3);
        webView = (WebView) findViewById(R.id.webView1);

        Button b = (Button)findViewById(R.id.button_show);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String name = edit.getText().toString();
                text.append(name);
                webView.loadUrl("http://www.google.com");
                System.out.println("Loaded Successfully--");
            }
        });
    }

The message "Loaded Successfully--" prints in the console, but WebView is not available.

I tried the following code too,

EditText edit;
TextView text;
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edit = (EditText) findViewById(R.id.editText);
    text = (TextView) findViewById(R.id.textView3);
    webView = (WebView) findViewById(R.id.webView1);

    Button b = (Button)findViewById(R.id.button_show);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String name = edit.getText().toString();
            text.append(name);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view,
                        String url) {
                    webView.loadUrl("http://www.google.com");
                    System.out.println("Loaded Successfully--");
                    return true;
                }
            });
        }
    });
}

This time "Loaded Successfully--" doesn't print. What am I missing here?

Edit

My XMl file (activity_main.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:gravity="fill"
    tools:context="com.example.asdf.MainActivity" >

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/back" />

   <EditText
       android:id="@+id/editText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/imageView1"
       android:layout_alignLeft="@+id/imageView1"
       android:layout_marginBottom="24dp"
       android:ems="10"
       android:hint="@string/edit_hint"
       android:textColor="#ffffff"
       android:textSize="20dip" >

       <requestFocus />
   </EditText>

   <Button
       android:id="@+id/button_show"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/editText"
       android:layout_alignRight="@+id/imageView1"
       android:text="@string/button_title" />

   <TextView
       android:id="@+id/textView3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/imageView1"
       android:layout_alignLeft="@+id/imageView1"
       android:layout_alignRight="@+id/imageView1"
       android:layout_below="@+id/button_show"
       android:text="@string/edit_hint"
       android:textColor="#ffffff"
       android:textSize="20dip" />

   <TextView
       android:id="@+id/textView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_centerVertical="true"
       android:gravity="center"
       android:text="As we know the relevant data has been wide-spreaded across various sites under many intentions, factualnote is a type of social software tool in which factual data are brought forward or narrow down to the web users."
       android:textColor="#ffffff"
       android:textSize="20dip"
       android:textStyle="bold" />

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_above="@+id/textView2"
       android:layout_alignParentLeft="true"
       android:layout_marginBottom="22dp"
       android:gravity="center"
       android:text="Factualnote is an web annotation application, which helps the users to mark the specific text, element, page, video, etc in a web page and share it to like-minded people."
       android:textColor="#ffffff"
       android:textSize="20dip"
       android:textStyle="bold" />

</RelativeLayout>

Upvotes: 2

Views: 3443

Answers (6)

user5520878
user5520878

Reputation:

I found the mistake in this project, reason is I missed the following line to add in manifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

Now it is fine. Thank you all for your support..

Upvotes: 1

Hitesh Sahu
Hitesh Sahu

Reputation: 45110

Below code search in google for input search key, when show key is presses webview begin to load url and hide serch layout. if user press Back key from webview then search layout will be visible for next search input.

enter image description here enter image description here

Your activity class should look like this

package com.example.test;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

    private static final String GOOGLE_SERACH_URL = "https://www.google.com/search?q=";

    private WebView webView;
    private FrameLayout customViewContainer;
    private WebChromeClient.CustomViewCallback customViewCallback;
    private View mCustomView;
    private CustomWebChromeClient mWebChromeClient;
    private CustomWebViewClient mWebViewClient;

    private EditText searchKeyEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        searchKeyEditText = (EditText) findViewById(R.id.editText);
        webView = (WebView) findViewById(R.id.webView1);

        // SetUp WebView
        customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer);

        mWebViewClient = new CustomWebViewClient();
        webView.setWebViewClient(mWebViewClient);

        mWebChromeClient = new CustomWebChromeClient();
        webView.setWebChromeClient(mWebChromeClient);
        webView.getSettings().setJavaScriptEnabled(true);

        // Important for PayUMoney
        webView.getSettings().setDomStorageEnabled(true);

        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSaveFormData(true);

        Button b = (Button) findViewById(R.id.button_show);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                findViewById(R.id.search_layout).setVisibility(View.GONE);

                webView.loadUrl(GOOGLE_SERACH_URL + searchKeyEditText.getText().toString());
            }
        });
    }

    public boolean inCustomView() {
        return (mCustomView != null);
    }

    public void hideCustomView() {
        mWebChromeClient.onHideCustomView();
    }

    @Override
    public void onPause() {
        super.onPause(); // To change body of overridden methods use File |
                            // Settings | File Templates.
        webView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume(); // To change body of overridden methods use File |
                            // Settings | File Templates.
        webView.onResume();
    }

    @Override
    public void onStop() {
        super.onStop(); // To change body of overridden methods use File |
                        // Settings | File Templates.
        if (inCustomView()) {
            hideCustomView();
        }
    }

    class CustomWebChromeClient extends WebChromeClient {
        private View mVideoProgressView;

        @Override
        public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
            onShowCustomView(view, callback); // To change body of overridden
                                                // methods use File | Settings |
                                                // File Templates.
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {

            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }
            mCustomView = view;
            webView.setVisibility(View.GONE);
            customViewContainer.setVisibility(View.VISIBLE);
            customViewContainer.addView(view);
            customViewCallback = callback;
        }

        @Override
        public View getVideoLoadingProgressView() {

            if (mVideoProgressView == null) {
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
            }
            return mVideoProgressView;
        }

        @Override
        public void onHideCustomView() {
            super.onHideCustomView(); // To change body of overridden methods
                                        // use File | Settings | File Templates.
            if (mCustomView == null)
                return;

            webView.setVisibility(View.VISIBLE);
            customViewContainer.setVisibility(View.GONE);

            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);

            // Remove the custom view from its container.
            customViewContainer.removeView(mCustomView);
            customViewCallback.onCustomViewHidden();

            mCustomView = null;
        }
    }

    class CustomWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        private int webViewPreviousState;

        private final int PAGE_STARTED = 0x1;

        private final int PAGE_REDIRECTED = 0x2;

        Dialog dialog = new Dialog(MainActivity.this);

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            webViewPreviousState = PAGE_STARTED;

            if (dialog == null || !dialog.isShowing())
                dialog = ProgressDialog.show(MainActivity.this, "", "Loading Please Wait", true, true,
                        new OnCancelListener() {

                            @Override
                            public void onCancel(DialogInterface dialog) {
                                // do something
                            }
                        });
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            if (webViewPreviousState == PAGE_STARTED) {
                if (null != dialog)
                    dialog.dismiss();
                dialog = null;
            }

        }
    }

    @Override
    public void onBackPressed() {

        if (findViewById(R.id.search_layout).getVisibility() == View.GONE) {
            findViewById(R.id.search_layout).setVisibility(View.VISIBLE);
        } else {
            super.onBackPressed();
        }

    }
}

Layout for main activity

<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="com.example.test.MainActivity1" >

    <LinearLayout
        android:id="@+id/search_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:weightSum="4" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Search" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <Button
            android:id="@+id/button_show"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Show" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/search_layout"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" />

        <FrameLayout
            android:id="@+id/customViewContainer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>

Helper layout file for youtube like web contents video_progress.xml

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

    <ProgressBar android:id="@android:id/progress"
                 style="?android:attr/progressBarStyleLarge"
                 android:layout_gravity="center"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>

    <TextView android:paddingTop="5dip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="loading"
              android:textSize="14sp"
              android:textColor="?android:attr/textColorPrimary"/>
</LinearLayout>

Internet permission for web contents

 <uses-permission android:name="android.permission.INTERNET" />

Alternatively you can use this fragment class to load web contents in your app

https://github.com/hiteshsahu/Android-Universal-Web-Content-Loader

Upvotes: 0

Dinesh Saini
Dinesh Saini

Reputation: 335

    webView=(WebView)view.findViewById(R.id.powered_webview);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(AppConstant.POWEREBY_URL);
    return view;
}

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

Upvotes: 0

vlad sol
vlad sol

Reputation: 99

you can use this method to know when the page is loaded.

mWebview.setWebViewClient(new WebViewClient() {

            boolean error = false;

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                error = true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                //hide your button
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });


  mWebview.loadUrl(url);

Upvotes: 0

Ramkumar.M
Ramkumar.M

Reputation: 691

TRY THIS In your onClickListener

edit.setVisibility(View.GONE);
text.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
 b.setVisibility(View.GONE);

Upvotes: 1

Jaimin Modi
Jaimin Modi

Reputation: 1677

@Ratwanska, You can use Intent for doing such url loading as below...

 if (Common.isOnline(YourActivity.this)) {
                try {
                    String url = "YOUR URL";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Common.displayToast(YourActivity.this, getResources().getString(R.string.strErrorInternetConnection));
            }

The above code will open your specified link in browser with the use of Intent and you also don't need to worry about hiding your button..!

I think its better way than using Webview and hiding your button.

Note : 'Common' is my custom class created to handle common stuff and 'isOnline' is the function inside that class to check the Internet connectivity.

Upvotes: 0

Related Questions