├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── atownsend │ │ └── gettingstartedrxandroid │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── atownsend │ │ │ └── gettingstartedrxandroid │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── atownsend │ └── gettingstartedrxandroid │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | build/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | subProjects/facebook/build 20 | 21 | # Intellij project files 22 | .idea/ 23 | .idea/workspace.xml 24 | gen-external-apklibs/ 25 | *.iml 26 | 27 | # Intellij 28 | .idea/ 29 | *.iml 30 | *.iws 31 | 32 | # Local configuration file (sdk path, etc) 33 | local.properties 34 | 35 | # Mac-specific stuff 36 | .DS_Store 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GettingStartedRxAndroid 2 | 3 | Sample library for getting started with RxJava on Android. Giving a sample of how to convert an AsyncTask over to RxJava. 4 | Source code follows along with the blog at https://www.captechconsulting.com/blogs/getting-started-with-rxjava-and-android 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | .iml -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "atownsend.gettingstartedrxandroid" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | testCompile 'junit:junit:4.12' 26 | compile 'com.android.support:appcompat-v7:23.0.1' 27 | compile 'com.android.support:design:23.0.1' 28 | compile 'io.reactivex:rxandroid:1.0.1' 29 | compile 'io.reactivex:rxjava:1.0.14' 30 | } 31 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Local/Users/atownsend/Development/android-sdk-macosx/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/atownsend/gettingstartedrxandroid/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package atownsend.gettingstartedrxandroid; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/atownsend/gettingstartedrxandroid/MainActivity.java: -------------------------------------------------------------------------------- 1 | package atownsend.gettingstartedrxandroid; 2 | 3 | import android.os.AsyncTask; 4 | import android.os.Bundle; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.LinearLayout; 11 | 12 | import rx.Observable; 13 | import rx.Subscriber; 14 | import rx.android.schedulers.AndroidSchedulers; 15 | import rx.schedulers.Schedulers; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | private LinearLayout rootView; 20 | private Button startAsyncTaskButton; 21 | private Button startRxOperationButton; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 28 | setSupportActionBar(toolbar); 29 | rootView = (LinearLayout) findViewById(R.id.root_view); 30 | 31 | // AsyncTask operation setup 32 | startAsyncTaskButton = (Button) findViewById(R.id.start_async_task_btn); 33 | startAsyncTaskButton.setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | v.setEnabled(false); 37 | new SampleAsyncTask().execute(); 38 | } 39 | }); 40 | 41 | // RxJava operation setup 42 | final Observable operationObservable = Observable.create(new Observable.OnSubscribe() { 43 | @Override 44 | public void call(Subscriber subscriber) { 45 | subscriber.onNext(longRunningOperation()); 46 | subscriber.onCompleted(); 47 | } 48 | }) 49 | .subscribeOn(Schedulers.io()) 50 | .observeOn(AndroidSchedulers.mainThread()); 51 | 52 | startRxOperationButton = (Button) findViewById(R.id.start_rxjava_operation_btn); 53 | startRxOperationButton.setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(final View v) { 56 | v.setEnabled(false); 57 | operationObservable.subscribe(new Subscriber() { 58 | @Override 59 | public void onCompleted() { 60 | v.setEnabled(true); 61 | } 62 | 63 | @Override 64 | public void onError(Throwable e) { 65 | } 66 | 67 | @Override 68 | public void onNext(String value) { 69 | Snackbar.make(rootView, value, Snackbar.LENGTH_LONG).show(); 70 | } 71 | }); 72 | } 73 | }); 74 | } 75 | 76 | public String longRunningOperation() { 77 | try { 78 | Thread.sleep(2000); 79 | } catch (InterruptedException e) { 80 | // error 81 | } 82 | return "Complete!"; 83 | } 84 | 85 | /** 86 | * Sample Async Task that does a long running operation, and then posts something to the UI 87 | */ 88 | private class SampleAsyncTask extends AsyncTask { 89 | 90 | @Override 91 | protected String doInBackground(Void... params) { 92 | return longRunningOperation(); 93 | } 94 | 95 | @Override 96 | protected void onPostExecute(String result) { 97 | Snackbar.make(rootView, result, Snackbar.LENGTH_LONG).show(); 98 | startAsyncTaskButton.setEnabled(true); 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 |