Skip to content

Commit a95e683

Browse files
author
David Weiser
committed
adds otto exercise
1 parent da8d0f0 commit a95e683

32 files changed

+595
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
defaultConfig {
7+
applicationId "com.vogella.android.otto"
8+
minSdkVersion 16
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support:appcompat-v7:25.3.1'
28+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
29+
compile 'com.squareup:otto:1.3.8'
30+
compile 'com.android.support:design:25.3.1'
31+
testCompile 'junit:junit:4.12'
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/david/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.vogella.android.otto;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.vogella.android.otto", appContext.getPackageName());
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.otto">
4+
5+
<application
6+
android:name=".MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vogella.android.otto;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.support.design.widget.FloatingActionButton;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.Button;
11+
import android.widget.TextView;
12+
13+
import com.squareup.otto.Subscribe;
14+
15+
public class BottomFragment extends Fragment {
16+
17+
private TextView bottomTextView;
18+
19+
@Override
20+
public void onCreate(@Nullable Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
((MyApplication)getActivity().getApplicationContext()).getOttoBus().register(this);
23+
}
24+
25+
@Nullable
26+
@Override
27+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
28+
View v = inflater.inflate(R.layout.fragment_bottom, container, false);
29+
bottomTextView = (TextView) v.findViewById(R.id.bottom_textview);
30+
return v;
31+
}
32+
33+
@Subscribe
34+
public void buttonClickAvailable(final View v){
35+
if(v instanceof FloatingActionButton) {
36+
bottomTextView.setVisibility(View.VISIBLE);
37+
bottomTextView.setText("Button pushed");
38+
bottomTextView.animate().alpha(0).setDuration(2000).withEndAction(new Runnable() {
39+
@Override public void run() {
40+
bottomTextView.setAlpha(1);
41+
bottomTextView.setVisibility(View.GONE);
42+
}
43+
});
44+
}
45+
}
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.otto;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
6+
public class MainActivity extends AppCompatActivity {
7+
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
setContentView(R.layout.activity_main);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.vogella.android.otto;
2+
3+
import android.app.Application;
4+
5+
import com.squareup.otto.Bus;
6+
7+
public class MyApplication extends Application{
8+
9+
Bus ottoBus;
10+
11+
@Override
12+
public void onCreate() {
13+
super.onCreate();
14+
ottoBus = new Bus();
15+
}
16+
17+
public Bus getOttoBus() {
18+
return ottoBus;
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vogella.android.otto;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.design.widget.FloatingActionButton;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
public class TopFragment extends android.app.Fragment {
11+
12+
private FloatingActionButton button;
13+
14+
@Nullable
15+
@Override
16+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
17+
View v = inflater.inflate(R.layout.fragment_top, container, false);
18+
button = (FloatingActionButton) v.findViewById(R.id.top_button);
19+
button.setOnClickListener(new View.OnClickListener() {
20+
@Override
21+
public void onClick(View v) {
22+
((MyApplication) getActivity().getApplicationContext()).getOttoBus().post(v);
23+
}
24+
});
25+
return v;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
tools:context="com.vogella.android.otto.MainActivity">
8+
9+
<fragment
10+
android:name="com.vogella.android.otto.TopFragment"
11+
android:layout_width="match_parent"
12+
android:layout_height="0dp"
13+
android:layout_weight="1"
14+
android:id="@+id/topFragment"/>
15+
16+
<fragment
17+
android:name="com.vogella.android.otto.BottomFragment"
18+
android:layout_width="match_parent"
19+
android:layout_height="0dp"
20+
android:layout_weight="1"
21+
android:id="@+id/bottomFragment"/>
22+
</LinearLayout>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/holo_blue_light">
6+
7+
8+
<TextView
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:id="@+id/bottom_textview"
12+
android:layout_centerInParent="true"
13+
android:textColor="@android:color/black"/>
14+
</RelativeLayout>

0 commit comments

Comments
 (0)