Friendy Tut
Friendy Tut

Reputation: 1

how to show a website within the same activity?

I have created an Application where on Button click the Website should open within the same Activity.

Button 1 -> should load website = >"google"

Button 2 -> should load website = >"google play"

Button 3 -> should load website = >"you tube"

This is How i want it to look: https://i.sstatic.net/yzDF9.png

Sample code of activity_main.xml

<?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">


    <LinearLayout
         android:id="@+id/linear1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_alignParentTop="true"
         android:orientation="horizontal">

         <ScrollView
             android:id="@+id/Srcollview1"
             android:layout_width="100dp"
             android:layout_height="match_parent">

             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="vertical">
                 <Button
                     android:id="@+id/btn1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="program 1"
                     ></Button>
                 <Button
                     android:id="@+id/btn2"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="program 2"
                     ></Button>
                 <Button
                     android:id="@+id/btn3"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="program 3"
                     ></Button>
             </LinearLayout>
         </ScrollView>

         <LinearLayout
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

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

Sample code for MainActivity.java

 Button b1,b2,b3;
 WebView webView;
 LinearLayout l1;

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

     b1 = (Button)findViewById(R.id.btn1);
     b2 = (Button)findViewById(R.id.btn2);
     b3 = (Button)findViewById(R.id.btn3);


     b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             webView = (WebView) findViewById(R.id.webviewing);
             webView.getSettings().setJavaScriptEnabled(true);
             webView.loadUrl("https://www.google.com");
         }
     });

     b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             webView = (WebView) findViewById(R.id.webviewing);
             webView.getSettings().setJavaScriptEnabled(true);
             webView.loadUrl("https://play.google.com/");

         }
     });

     b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             webView = (WebView) findViewById(R.id.webviewing);
             webView.getSettings().setJavaScriptEnabled(true);
             webView.loadUrl("https://www.youtube.com/");
         }
     });
 }

Upvotes: 0

Views: 82

Answers (1)

blessthefry
blessthefry

Reputation: 121

Your code works fine, except you set the listener for b2 twice. Typo maybe?

If you are facing connection errors, just add the Internet permission to AndroidManifest.xml.

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

Upvotes: 1

Related Questions