Skip to content

Instantly share code, notes, and snippets.

@learningjs
Last active January 4, 2016 15:39
Show Gist options
  • Save learningjs/8641788 to your computer and use it in GitHub Desktop.
Save learningjs/8641788 to your computer and use it in GitHub Desktop.
activity Java files for the Jabberwocky and Roundball activities. AndroidManifest.xml and activity_jabberwocky.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Jabberwocky" >
<Button
android:id="@+id/WikipediaButton"
android:onClick="openWikipediaPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp"
android:background="#3b5998"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="@string/Wikipedia"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:id="@+id/loadPictureButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/WikipediaButton"
android:background="#CC8800"
android:onClick="openPicture"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="@string/loadPicture"
android:textColor="#ffffff"
android:textStyle="bold" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/WikipediaButton"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_alignParentLeft="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.to.webpage.webpagetoapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="app.to.webpage.webpagetoapp.Jabberwocky"
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
android:label="@string/title_activity_jabberwocky"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="app.to.webpage.webpagetoapp.Roundball"
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
android:label="@string/title_activity_roundball"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="app.to.webpage.webpagetoapp.Waroftheworlds"
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
android:label="@string/title_activity_waroftheworlds"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="app.to.webpage.webpagetoapp.UofIatNASA"
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
android:label="@string/title_activity_uof_iat_nas"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="app.to.webpage.webpagetoapp.MainActivity"
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package app.to.webpage.webpagetoapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
// activity Java file for the Jabberwocky activity
public class Jabberwocky extends Activity {
private WebView wV;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jabberwocky);
// Catch the webView
wV = (WebView) findViewById(R.id.webView1);
// Open assets/jabberwocky.html
wV.loadUrl("file:///android_asset/jabberwocky.html");
// Enable features
wV.getSettings().setBuiltInZoomControls(true);
wV.getSettings().setJavaScriptEnabled(true);
System.out.println(Math.random() > 0.5);
}
// When this Activity is ready (and the layout has been been created)
// Android will call
// our onResume method. The onResume method can be called again if we are
// paused for some reason.
@Override
protected void onResume() {
Log.e("Pickle", "onResume"); // Let's tell the World! Well anyone
// reading the logcat
// window...
// "MediaPlayer.create(...)" is the code that actually creates a media
// player object
// "player =" is the code that changes ('assigns') our pointer to
// point to the new media player object
// You need to add "import android.media.MediaPlayer;"
// import statement at the top of this file
player = MediaPlayer.create(this, R.raw.jabberwocky_carroll_wt);
// When 'create' returns the MediaPlayer is already in the prepared
// state and ready to go!
player.start();
super.onResume();
}
@Override
protected void onPause() {
Log.e("Pickle", "onPause");
// We're pausing. For this demo we will just stop the MediaPlayer and
// then ask it to release
// all of the valuable resources it's using.
// If the user comes back to this app then onResume() will be called
// again
// (and we'll make a new Media player; see above)
player.stop();
player.release();
super.onPause();
}
// Code to start the external WebBrowser app
// We ask Android to open a Wikipedia page for us.
// It does so by starting a web browser and giving it the intent information
// You'll need to add import statements to the top of this file-
// import android.content.Intent;
// import android.net.Uri;
// import android.view.View;
public void openWikipediaPage(View v) {
String url = "http://en.wikipedia.org/wiki/Jabberwocky";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
// Code to open the Jabberwocky image
public void openPicture(View v) {
wV.loadUrl("file:///android_asset/Jabberwocky.jpg");
}
// Code to enable the back button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the event was the Back key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && wV.canGoBack()) {
wV.goBack();
return true;
}
// Default behavior
// If it wasn't the Back key or there's no web history, bubble up to the
// default
// system behavior (probably exit the activity).
return super.onKeyDown(keyCode, event);
}
// http://stackoverflow.com/questions/19441979/how-to-disable-and-re-enable-touch-event
@Override
public boolean onTouchEvent(MotionEvent event) {
// Touch events should not be recorded if you're not editing anything.
// Check if the event was the Back button and if there's history
if (wV.canGoBack()) {
wV.goBack();
return true;
}
return false;
}
}
// activity Java file for the Roundball activity
package app.to.webpage.webpagetoapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class Roundball extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roundball);
// Catch the webView
WebView wV = (WebView) findViewById(R.id.webView1);
// Open assets/roundball/roundball.html
wV.loadUrl("file:///android_asset/roundball/roundball.html");
// Enable features
//wV.getSettings().setBuiltInZoomControls(true);
wV.getSettings().setJavaScriptEnabled(true);
WebSettings settings = wV.getSettings();
// https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
// http://developer.android.com/reference/android/webkit/WebSettings.html
settings.setDomStorageEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.roundball, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment