Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin for Android Developers

Kotlin for Android Developers

Talk about Kotlin for android developers, a introduction to Kotlin Language and some tips apply to Android develop

Yury Camacho

July 08, 2017
Tweet

More Decks by Yury Camacho

Other Decks in Technology

Transcript

  1. Java • Missing modern features • NPE • JDK 6-7

    • No lambdas • No streams • Too Much ceremony
  2. History • 2011, released as official project by Jetbrains •

    2012, Jetbrains Open Sourced the project, license Apache 2 • 2015, released first Kotlin beta version 1.0 • 2016, Kotlin 1.0 released • 2016 Gradle announces official support • 2017, released 1.1 • and…….
  3. What is Kotlin? • JVM • Seems Java • Object

    Oriented • Functional • Do more with less • Complete interop with Java • Null safety
  4. Variables val name: String = "Kylo Ren" var side =

    "Force" side = "Dark Side" name = "Kylo" //no compile!!! and mutability
  5. Null Safety var name: String = null name = "Juan"

    name?.trim() // does runs name!!.trim() // runs anyway ? val lastName: String = "Perez" lastName.trim() // no need for "?."
  6. No Lambdas in Java public List<Integer> evens(List<Integer> nums){ List<Integer> numsCopy

    = new ArrayList<>(nums); Iterator<Integer> numsItr = numsCopy . listIterator (); while (numsItr.hasNext()) { Integer num = numsItr . next (); if (num % 2 != 0) numsItr.remove(); } return numsCopy; }
  7. POJO JAVA public static class Person { private String mName;

    public Person(String name) { mName = name; } public String getName() { return mName; } public void setName(String name) { mName = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return mName != null ? mName.equals(person.mName) : person.mName == null; } @Override public int hashCode() { return mName != null ? mName.hashCode() : 0; } }
  8. Optional Arguments class Character(name: String, gun: Boolean = false ){

    val name = name; var gun = gun } var bruceLee = Character("Bruce Lee") var rambo = Character("Rambo", true)
  9. Named Arguments class Character(name: String, gun: Boolean = false ){

    val name = name; var gun = gun } var rambo = Character(gun = true, name = "Rambo")
  10. Extension functions in android //JAVA Toast.makeText(getBaseContext(),"#IOLima2017", Toast.LENGTH_SHORT).show(); //Kotlin fun Activity.toast(message:

    CharSequence, duration: Int = Toast.LENGTH_LONG){ Toast.makeText(this, message, duration).show() } toast("#IOLima2017")
  11. Extension functions in android //JAVA public View onCreateView(LayoutInflater inflater, ViewGroup

    container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.main, container, false); } //KOTLIN fun ViewGroup.inflate(layoutId: Int, attachRoot:Boolean = false): View { return LayoutInflater.from(context).inflate(layoutId, this, attachRoot) } return container.inflate(R.layout.main)
  12. how use it ? Add this line in Build.gradle apply

    plugin: 'kotlin-android-extensions' Import synthetic layout import kotlinx.android.synthetic.<layout>.*
  13. Example with Java <TextView android:id="@+id/hello" android:layout_width="fill_parent" android:layout_height="wrap_content"/> public class MyActivity

    : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) TextView text = (TextView) findViewById(R.id.hello); } }
  14. Example Android Extensions <TextView android:id="@+id/hello" android:layout_width="fill_parent" android:layout_height="wrap_content"/> import kotlinx.android.synthetic.main.* public

    class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) this.hello.setText("Hi!") } }
  15. Anko • Programmatic layouts • DSL instead of XML •

    Compact and readable • Ide Integration with previews