Commit 8c670a7e authored by Daniel Vogel's avatar Daniel Vogel

add android demos

parent 4fab7c2c
.idea
.idea/
# Created by https://www.gitignore.io/api/androidstudio
### AndroidStudio ###
# Covers files to be ignored for android development using Android Studio.
# Built application files
*.apk
*.ap_
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Gradle files
.gradle
.gradle/
build/
# Signing files
.signing/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio
/*/build/
/*/local.properties
/*/out
/*/*/build
/*/*/production
captures/
.navigation/
*.ipr
*~
*.swp
# Android Patch
gen-external-apklibs
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
# NDK
obj/
# IntelliJ IDEA
*.iml
*.iws
/out/
# User-specific configurations
.idea/libraries/
.idea/workspace.xml
.idea/tasks.xml
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
.idea/datasources.xml
.idea/dataSources.ids
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Legacy Eclipse project files
.classpath
.project
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.war
*.ear
# virtual machine crash logs (Reference: http://www.java.com/en/download/help/error_hotspot.xml)
hs_err_pid*
## Plugin-specific files:
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Mongo Explorer plugin
.idea/mongoSettings.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### AndroidStudio Patch ###
!/gradle/wrapper/gradle-wrapper.jar
# End of https://www.gitignore.io/api/androidstudio
\ No newline at end of file
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "com.example.mvc"
minSdkVersion 15
targetSdkVersion 26
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mvc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mvc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.mvc;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup;
public class MainActivity extends Activity {
Model model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("MVC", "onCreate");
// load the base UI (has places for the views)
setContentView(R.layout.mainactivity);
// Setup model
model = new Model();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Log.d("MVC", "onPostCreate");
// can only get widgets by id in onPostCreate for activity xml res
// create the views and add them to the main activity
View1 view1 = new View1(this, model);
ViewGroup v1 = (ViewGroup) findViewById(R.id.mainactivity_1);
v1.addView(view1);
View2 view2 = new View2(this, model);
ViewGroup v2 = (ViewGroup) findViewById(R.id.mainactivity_2);
v2.addView(view2);
// initialize views
model.initObservers();
}
// save and restore state (need to do this to support orientation change)
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d("MVC", "save state");
// save all stateful values in model
outState.putInt("Counter", model.getCounterValue());
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d("MVC", "restore state");
super.onRestoreInstanceState(savedInstanceState);
// get all stateful values in model
model.setCounterValue(savedInstanceState.getInt("Counter"));
}
}
package com.example.mvc;
import android.util.Log;
import java.util.Observable;
import java.util.Observer;
public class Model extends Observable {
private int counter;
Model() {
counter = 0;
}
// Data methods
public int getCounterValue() {
return counter;
}
public void setCounterValue(int i) {
counter = i;
Log.d("DEMO", "Model: set counter to " + counter);
setChanged();
notifyObservers();
}
public void incrementCounter() {
counter++;
Log.d("DEMO", "Model: increment counter to " + counter);
setChanged();
notifyObservers();
}
// Observer methods
// a helper to make it easier to initialize all observers
public void initObservers() {
setChanged();
notifyObservers();
}
@Override
public void addObserver(Observer observer) {
Log.d("DEMO", "Model: Observer added");
super.addObserver(observer);
}
@Override
public synchronized void deleteObserver(Observer observer) {
Log.d("DEMO", "Model: Observer deleted");
super.deleteObserver(observer);
}
@Override
public synchronized void deleteObservers() {
super.deleteObservers();
}
@Override
public void notifyObservers() {
Log.d("DEMO", "Model: Observers notified");
super.notifyObservers();
}
}
\ No newline at end of file
package com.example.mvc;
import java.util.Observable;
import java.util.Observer;
import android.content.Context;
import android.util.*;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class View1 extends LinearLayout implements Observer {
private Model model;
private Button button;
public View1(Context context, Model m) {
super(context);
Log.d("DEMO", "View1 constructor");
// get the xml description of the view and "inflate" it
// into the display (kind of like rendering it)
View.inflate(context, R.layout.view1, this);
// save the model reference
model = m;
// add this view to model's list of observers
model.addObserver(this);
// get a reference to widgets to manipulate on update
button = (Button) findViewById(R.id.view1_button);
// create a controller for the button
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// do this each time the button is clicked
model.incrementCounter();
}
});
}
// the model call this to update the view
public void update(Observable observable, Object data) {
Log.d("DEMO", "update View1");
// update button text with click count
// (convert to string, or else Android uses int as resource id!)
button.setText(String.valueOf(model.getCounterValue()));
}
}
package com.example.mvc;
import java.util.Observable;
import java.util.Observer;
import android.content.Context;
import android.util.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class View2 extends LinearLayout implements Observer {
private Model model;
private TextView textview;
public View2(Context context, Model m) {
super(context);
Log.d("DEMO", "View2 constructor");
// get the xml description of the view and "inflate" it
// into the display (kind of like rendering it)
View.inflate(context, R.layout.view2, this);
// save the model reference
model = m;
// add this view to model's list of observers
model.addObserver(this);
// get a reference to widgets to manipulate on update
textview = (TextView)findViewById(R.id.view2_textview);
// create a controller to increment counter when clicked
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// do this each time the button is clicked
model.incrementCounter();
}
});
}
// the model call this to update the view
public void update(Observable observable, Object data) {
Log.d("DEMO", "update View2");
int n = model.getCounterValue();
StringBuilder s = new StringBuilder(n);
for (int i = 0; i < n; i++) {
s.append("x");
}
// update button text with click count
// (convert to string, or else Android uses int as resource id!)
textview.setText(s);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/mainactivity_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/mainactivity_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/mainactivity_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/mainactivity_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true" >
<Button
android:id="@+id/view1_button"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="@string/button_text"
android:textSize="20pt" />
</RelativeLayout>
\ No newline at end of file
<?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:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/view2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20pt" />
</ScrollView>
</LinearLayout>
\ No newline at end of file
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw600dp devices (e.g. 7" tablets) here.
-->
</resources>
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
-->
<dimen name="activity_horizontal_margin">128dp</dimen>
</resources>
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MVC1</string>
<string name="action_settings">Settings</string>
<string name="button_text">\?</string>
<color name="bgcolour">#ff0000</color>
</resources>
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
allprojects {
repositories {