Reputation: 59
Hi I am new to Android development and StackOverflow.
I have an Android WebView application in which a 'starter activity' (just intro text and a button to start main intent) starts my WebView activity, which loads a single URL (this is in the onCreate method).
In this main activity, I have three buttons, one of which opens the same URL in the current activity (effectively refreshing the page). The other two buttons I would like to open two different URLs in the same WebView activity, and I was wondering if/how this is possible.
Here is the main ReadWebpageAsyncTask.java activity code:
public class ReadWebpageAsyncTask extends Activity {
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
}
@SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
browser.loadUrl(urlStrOne);
return true;
}
}
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(browser.canGoBack()) {
browser.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
And the corresponding XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black">
<Button
android:id="@+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/soon"
android:background="@+drawable/custom_button_one"
android:onClick="onClick"
android:text="@string/load_page" />
<Button
android:id="@+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/soon"
android:background="@+drawable/custom_button_one"
android:onClick="exitButton"
android:text="@string/exit_button" />
<Button
android:id="@+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toLeftOf="@+id/exitapp01"
android:layout_toRightOf="@+id/readWebpage"
android:layout_alignParentTop="true"
android:onClick="comingSoon"
android:text="@string/future_courses" />
</RelativeLayout>
<WebView
android:id="@+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/WebView01" >
</WebView>
</LinearLayout>
P.S. Please excuse the terrible naming of everything, I wrote this a while back and haven't gotten around to changing it all.
To clarify, the main URL which is loaded first is the same as the button "readWebpage", and the other two URLs which I would like to be loaded on click are buttons "exitapp01" (what can I say, I am a noob) and "soon".
Upvotes: 0
Views: 1321
Reputation: 1
If The buttons are on Activity You have to set the OnClickListener on all the buttons and within the onClick() do like this:
@Override public void onClick(View view) {
switch(view.getId()) {
case R.id.[Your first button Id]:
{
//Functionality
}
case R.id.[Your Second button Id]:
{
//Functionality
}
case R.id.[Your first button Id]:
{
//Functionality
}
//End Switch }
//You also need To Write implements OnClickListener at the Class Definition Of Your activity
Upvotes: 0
Reputation: 3045
You can add OnClickListener
s to your buttons and switch the url depending on the case:
public class ReadWebpageAsyncTask extends Activity implements OnClickListener{
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
Button reload, exit, soon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
reload = (Button)findViewById(R.id.readWebpage);
exit = (Button)findViewById(R.id.exitapp01);
soon = (Button)findViewById(R.id.soon);
reload.setOnClickListener(this);
exit.setOnClickListener(this);
soon.setOnClickListener(this);
}
@SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.readWebpage:
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
break;
case R.id.exitapp01:
//Do whatever you want with this button
break;
case R.id.soon:
//Do whatever you want with this button
break;
}
}
}
And remove all the android:onClick=""
from your xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black">
<Button
android:id="@+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<WebView
android:id="@+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Upvotes: 1