Skip to main content

Posts

Showing posts from 2015

How to setup Kotlin in Android Studio

Brief about Kotlin What I am impressed in Kotlin is Android Extensions. It enables you to remove boiler plate code of setting views. You have bind your view object by calling findViewbyId() but Kotlin supports it just importing kotlinx.android.synthetic. .* package. Let's have a look at an example: res/layout/login.xml LoginActivity.kt package com.ispark.kotlin.login import kotlinx.android.synthetic.login.* import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.text.TextUtils import android.util.Log import android.widget.TextView import android.widget.Toast import com.ispark.kotlin.R import kotlin.String public class LoginActivity : AppCompatActivity() { private val LOG_TAG: kotlin.String = "LoginActivity" override fun onCreate(savedInstanceState : Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.login) setTitle(R.string.pleaseSignIn) log

How to test AsyncTask in Android

In Android, test is not as easy as any other platform. Because Android test cannot be run without emulator. Particulary when it comes to AsyncTask or Service, it is difficult to test because they are different type of thread and hard to check their result. Then, how can we ensure the result of AsyncTask valid? AsyncTask is a thread and an asynchnorous as the name means. So, we need to wait for it finishes its job and need to capture the event. Then, when it happens in AsyncTask. It can be one of onBackground() and onPostExecute() methods. It doesn't matter you use onBackground() or onPostExecute() but I prefer onPostExecute(). Anyway, we can test an AsyncTask if we can hook it. Then, how can we hook it? For that, we can use callback pattern. But we need to delay main thread to wait for the AsyncTask's job done because we want to check the result. So the structure for the test would be like: 1. Create AsyncTask A 2. Injection a callback into A 3. Wait until A finish 4.

Fragment LifeCycle

Normal setRetainInstance(true) Description onAttach onAttach Called once the fragment is associated with its activity. onCreate - Called to do initial creation of the fragment. onCreateView onCreateView Creates and returns the view hierarchy associated with the fragment. onActivityCreated onActivityCreated Tells the fragment that its activity has completed its own Activity.onCreate(). onViewStateRestored onViewStateRestored Tells the fragment that all of the saved state of its view hierarchy has been restored. onStart onStart Makes the fragment visible to the user (based on its containing activity being started). onResume onResume Makes the fragment begin interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series

Let's start Lambda in Android

In Android programming, I think Java & J2EE programming are not that different, we developers put many boiler plate code particularly when we use anonymous instances. Example1. Simple example button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view){ //Do something } }); In the above example, what we really need is inside of onClick() method. Other code is actually decorations and we don't want it. How about we can pass just onClick method body as a parameter of setOnClickListener method like below? button1.setOnClickListener(view->{ //Do something }); That's what we have exactly wanted. We can use the code style in Java 8. I think you already might know about Java 8 Lambda but there is no official support for Java 8 in Android 8. It would be awesome we can use it in Android and there is a way to be able to use Lambda in Android as well. You can refer my previous blog to setup RetroLambda in Andro

How to setup RetroLambda, Roboletric and RxAndroid together in Android Studio

Testing environment Before you proceed the process, Java 1.8 should be installed first in your system. Here is my testing system: OS: MacOSX Yosemite(Version 10.10.3) Android Studio: Version 1.2 Java SDK: Version 1.8.0_45 Project structure /Project /app /src /main /java AndroidManifest.xml /testDebug /java build.gradle /build /gradle build.gradle Configuration /Project/build.gradle Add dependencies for retrolambda and robolectric plugins under dependencies block. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.2' classpath 'me.tatarka:gradle-retrolambda:3.1.0' classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1' } } allprojects { repositories { jcenter() } } Configuration /Project/app/build.gradle apply plugin: 'co

Robolectric setup with Android Studio

Android Studio: 1.2 1. Add class path to project build. proejct/buiild.gradle classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1' 2. Add robolectric plugin to app build. proejct/app/build.gradle project/app/build.gradle apply plugin: 'org.robolectric' 3. Add dependencies to app build. proejct/app/build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' testCompile 'junit:junit:4.12' testCompile 'org.robolectric:robolectric:2.4' } 4. Add configurations for convenience to app build. proejct/app/build.gradle robolectric { // Configure includes / excludes include '**/*Test.class' exclude '**/espresso/**/*.class' // Configure max heap size of the test JVM maxHeapSize = '2048m' // Configure the test JVM arguments - Does not apply to Java 8 jvmArgs '-XX:MaxPermSiz

Retain AsyncTask when configuration change

One of the frequent facing situation in Android is how to handle configuration change such as device rotation, soft keyboard and so on. For example, what happens if you rotate the device when you download a file using AsyncTask? The Android would create your Activity again to load the proper configuration including layout. The lifecycle methods(onCreate - onStart - onResume) will be called again and you can get data onCreate() if you have saved any data onSavedInstance(). However, your AsyncTask lost pipeline to your Activity and the code of executing the AsyncTask will be called again. This is not definitely what we want. Let's have a look at code how to avoid it: I'm going to create a simple app to display weather using Open Weather API for demo. We need one Activity, one Fragment for UI ListView first. MainActivity.java import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bun

Support library AppCompat v21.1.0

Recently Android Support Library has been updated to v21.1.1. There were some changes and you can see the changed via the link: https://developer.android.com/tools/support-library/index.html#revisions One thing I have notice is new AppCompt* classes for appcompat library. Now you need to use AppCompatActivity instead of ActionBarActivity. It supports support library action bar features. ActionBarActivity now has been deprecated. I think this kind of too often changes in framework makes us developers annoying. Some people might just start to use ActionBarActivity.

Google's fragment example error

These days, I have been reading through Android Developer training document(http://developer.android.com/training/index.html) in detail and playing with samples. I am in the Fragment section now and I found out an example app has an error. Here is the link: http://developer.android.com/shareables/training/FragmentBasics.zip I give you a brief about the example. Entry point of the app is MainActivity and it uses different layouts depending on the screen sizes layout, one for normal and the other for large size devices. You can see only news article list when you run the app on handset devices but it will display two pane layout on large size device such as 7" tablets. /res/layout/news_article.xml(Normal size) /res/layout-large/new_article.xml(Large size) It will load news content when you click a item from the headlines. The MainActivity switches Fragment to display it in a handset but it displays news content in the second pane in a table device. Error c

Robolectric setup in Android Studio 1.0

These days I have been using Android Studio v1.0 and I want to do test using Robolectric, which give you more freedom for unit testing in Android. You can perform your test without emulator running. But I have faced setting problems even though the website looks like they're explaining well about how to setup the configuration. I blamed my brain but I found out there were missings for Android Studio gradle build and I had to google it a little bit more. I am not familiar with Gradle yet, but Android Studio uses Gradle as default build tool and Gradle seems to be a quite great build tool. There are two 'build.gradle's for Android Studio project. These are the main file for build configuration. One of them(a) is in your project folder is for global setting and the other one(b) in the 'app' folder is just for your app. myproject - build.gradle (a) - app - build.gradle (b) For the right setup, we need to touch a few things. 1) Installation 'Android

How to remove log completely in release version in Android

You can use  ProGuard  to remove completely any lines where a return value is not used, by telling ProGuard to assume that there will be no problems. The following proguard.cfg chunk instructs to remove Log.d, Log.v and Log.i calls. - assumenosideeffects class android . util . Log { public static *** d (...); public static *** w (...); public static *** v (...); public static *** i (...); } The end result is that these log lines are not in your release apk, and therefore any user with logcat won't see d/v/i logs. ref. http://stackoverflow.com/questions/5553146/disable-logcat-output-completely-in-release-android-app