Reputation: 58
I am creating a app that shows webview on the click of a button. Ihave an activity A that has 5 buttons. When click any of the button, it opens Activity B that has webview in it . What I want is when I click first button it should take me on activity b that contains website 1, when i click second button it take me on activity b that contains website 2 and so on. I'm passing the website url as intent. But the problem is when the second activity opens it is totally blank. I can't seem to figure out the reason.
Code below:
Activity A : monsterB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Monster.class);
i.putExtra("monsterB", Monster);
//int x = 1;
startActivity(i);
}
});
naukrii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Monster.class);
i.putExtra("naukri", naukriaddress);
//x=2
startActivity(i);
}
});
Activity B :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monster);
WebView mons = (WebView) findViewById(R.id.monster);
WebView nauk = (WebView) findViewById(R.id.naukri);
String newM = "https://www.google.co.in/";
String newN = "https://www.google.co.in/";
String one = getIntent().getExtras().getString("monsterB");
String hell = getIntent().getExtras().getString("naukri");
if(one == newM) {
mons.setWebViewClient(new WebViewClient());
mons.loadUrl("https://www.naukri.com/");
}
else if(hell == newN) {
nauk.setWebViewClient(new WebViewClient());
nauk.loadUrl("https://www.google.co.in/");
}
}
Activity B.XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="nripesh.gob.Monster">
<WebView
android:id="@+id/monster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<WebView
android:id="@+id/naukri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>
I have made only 2 buttons so far. Thanks in advance.
Upvotes: 1
Views: 6235
Reputation: 2558
change code like below just use one webview and pass different url and load that url in web view
Activity A : monsterB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Monster.class);
i.putExtra("url", "https://www.google.co.in/");
//int x = 1;
startActivity(i);
}
});
naukrii.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), Monster.class);
i.putExtra("url", "https://www.naukri.com/");
//x=2
startActivity(i);
}
});
Activity B :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monster);
WebView webView = (WebView) findViewById(R.id.webView);
String url = getIntent().getExtras().getString("url");
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("url");
Activity B.XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="nripesh.gob.Monster">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>
Upvotes: 3