2

I have a linear layout carrying webview. I have set the id of the LinearLayout to 'lay001'. My intention is to set the onClickListener of the layout so that onclick, the intent will send data to webview and then to the url i specified.

All my efforts to make it work failed me. Please help me set the java code. Thanks

activity_main.xml

<LinearLayout
        android:id="@+id/lay001"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:orientation="vertical"
        android:background="@drawable/layout_bg1">

                <WebView
                    android:id="@+id/webview001"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:accessibilityPaneTitle="The Liturgy"
                    android:layout_marginTop="2dp">
                </WebView>

                <TextView
                    android:id="@+id/textView001"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="top"
                    android:text="Anglican Hymns Tunes"
                    android:textSize="10sp"
                    android:textStyle="italic|bold"
                    android:textAlignment="center"
                    android:textColor="#DD1A1A"/>
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private WebView webview;
    private LinearLayout myLayout;

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

        myLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                     webview = (WebView001) findViewById(R.id.webview001);
                     webview.setWebViewClient(new WebViewClient());
                     webview.loadUrl("http://www.google.com");
                }
            }
        });
    }
} 
Mojtaba Haddadi
  • 1,346
  • 16
  • 26
Joseph
  • 400
  • 4
  • 19

1 Answers1

1

You are missing findViewById() of linearlayout.

Try this out :

   private LinearLayout myLayout;
   myLayout= (LinearLayout) findViewById(R.id.lay001);

Also change this :

    webview = (WebView) findViewById(R.id.webview001);

To load url :

 myLayout.setOnClickListener(new OnClickListener() 
     {

        @Override
        public void onClick(View v)
        {
          mWebview.loadUrl("your url");
        }
    }); 
haresh
  • 1,424
  • 2
  • 12
  • 18
  • Onclick it loads url within App Is there a way i can make the webview launch using external playstore app url or browser App? – Joseph Jan 02 '20 at 10:52
  • @Joseph look for this :https://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl – haresh Jan 02 '20 at 11:03
  • Your answer is still correct. Shortening the playstore URL makes it possible to launch Playstore App onClick. I used bitly.com – Joseph Jan 02 '20 at 13:05