SlideShare a Scribd company logo
Anko & Karamba
in Kotlin Bapusaheb Patil
Google-certified Android
Developer & OpenClassrooms
Android Mentor
www.bapspatil.com
My sample app for this talk:
Ankara
bit.do/baps-ankara
What is Anko?
What We Know:
Kotlin reduces boilerplate
(lengthy) code in Android
Development, when compared
with Java.
Anko, if used in Kotlin,
reduces boilerplate code
even further.
What We Need To Know:
+ A bunch of other super
useful stuff I’ll be
covering in this talk
4 Parts of Anko
• Anko Commons
• Anko Layouts
• Anko SQLite
• Anko Coroutines
Setting up Anko...
App-level build.gradle file:
kotlin {
experimental {
coroutines “enable”
}
}
dependencies {
implementation “org.jetbrains.anko:anko:0.10.4”
}
Anko Commons
Usage of Intents:
INTENTS WITHOUT ANKO
val intent = Intent(this, SomeActivity::class.java)
intent.putExtra("id", 13)
startActivity(intent)
Anko Commons
Usage of Intents:
INTENTS WITH ANKO
startActivity<SomeActivity>("id" to 13)
Anko Commons
Usage of Intents:
BROWSER INTENT WITHOUT ANKO
val uri =
Uri.parse(“https://www.bapspatil.com”)
val browserIntent = Intent(Intent.ACTION_VIEW,
uri)
startActivity(browserIntent)
Anko Commons
Usage of Intents:
BROWSER INTENT WITH ANKO
browse(“https://www.bapspatil.com”)
Anko Commons
Usage of Intents:
CALL INTENT WITHOUT ANKO
val uri = Uri.parse(“tel:+917788990099”)
val callIntent = Intent(Intent.ACTION_DIAL)
callIntent.data = uri
startActivity(callIntent)
Anko Commons
Usage of Intents:
CALL INTENT WITH ANKO
makeCall(“+917788990099”)
Anko Commons
Usage of Common Android Components:
TOAST WITHOUT ANKO
Toast.makeText(context,
“Hello, people of BlrKotlin!”,
Toast.LENGTH_SHORT)
.show()
Anko Commons
Usage of Common Android Components:
TOAST WITH ANKO
toast(“Hello, people of BlrKotlin!”)
// longToast(“Hello, people of BlrKotlin!”)
Anko Commons
Usage of Common Android Components:
SNACKBAR WITHOUT ANKO
val snackbar = Snackbar.make(view,
“Hello, people of BlrKotlin!”,
Snackbar.LENGTH_SHORT)
.setAction ( “HI!”,
{ v ->
doSomething()
})
snackbar.show()
Anko Commons
Usage of Common Android Components:
SNACKBAR WITH ANKO
snackbar(view, “Hello, people of BlrKotlin!”, “HI!”){
doSomething()
}
Anko Commons
Usage of Common Android Components:
ALERTDIALOG WITHOUT ANKO
var alertDialog = AlertDialog.Builder(this)
.setMessage("Hi, this is a message for the AlertDialog.")
.setTitle("Title of the AlertDialog")
.setPositiveButton("OK", DialogInterface.OnClickListener { dialog,
which ->
doSomethingPositive()
})
.setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, which ->
doSomethingNegative()
})
.create()
.show()
Anko Commons
Usage of Common Android Components:
ALERTDIALOG WITH ANKO
alert(“Hi, this is a message for the AlertDialog.”, “Title of
the AlertDialog”) {
yesButton { doSomethingPositive() }
noButton { doSomethingNegative() }
}.show()
Anko Commons
Usage of Logging:
LOGGING WITHOUT ANKO
Log.v(“MainActivity”, “This activity was
created successfully!”)
Anko Commons
Usage of Logging:
LOGGING WITH ANKO
class MainActivity : AppCompatActivity(),
AnkoLogger {
override fun onCreate(savedInstanceState:
Bundle?) {
verbose(“This activity was created
successfully!”)
}
}
Anko
Layouts
But what’s wrong with XML?
• It forces you to write the same code again.
• It is not typesafe.
• It is not null-safe.
• No code reuse.
• XML parsing is too much overhead. Takes too much CPU time,
and hence battery.
But what’s wrong with XML?
Why not create the UI programmatically?
Why not create the UI programmatically?
Because...it looks ugly. Here’s an example:
val activity = this
val layout = LinearLayout(activity)
layout.orientation = LinearLayout.VERTICAL
val nameEditText = EditText(activity)
val button = Button(activity)
button.text = “Hey There!"
button.setOnClickListener {
Toast.makeText(activity, "Hello there, ${nameEditText.text}!",
Toast.LENGTH_SHORT).show()
}
layout.addView(nameEditText)
layout.addView(button)
All that code...
...just for this?
Enter,
Anko Layouts.
Anko Layouts
MainActivity.kt
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
verticalLayout {
val nameEditText = editText()
button(“Hey There!”) {
onClick {
toast(“Hello there, ${nameEditText.text}!”)
}
}
}
}
This makes Virat
happy too.
Let’s take a
closer look at
Anko Layouts...
Attributes in Anko Layouts DSL
verticalLayout {
lparams(width = matchParent, height = matchParent)
textView {
id = R.id.helloTextView
text = “Hello, people of BlrKotlin!”
textSize = 32f
textColor = 0xffffff.opaque
padding = dip(16)
gravity = Gravity.START // gravity in XML
}.lparams(width = wrapContent, height = wrapContent) {
gravity = Gravity.CENTER // layout_gravity in XML
margin = dip(4)
}
}
AnkoComponent<T>
What is it & why use it?
- It’s a separate class where you can write your DSL code for your UI.
- It is reusable.
- It keeps the UI separate from logic.
- You can convert existing XML files to AnkoComponents with the Anko
Support plugin.
- You also get a layout preview if you install the Anko Support plugin.
Keep UI code in a separate file!
AnkoComponent<T>
MainView.kt
class MainView : AnkoComponent<MainActivity> {
override fun createView(ui: AnkoContext<MainActivity>):
View = with(ui) {
verticalLayout {
...
...
...
}
}
}
AnkoComponent<T>
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit val textView: TextView
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
MainView().setContentView(this)
textView = find(R.id.helloTextView)
toast(textView.text)
}
}
Still not convinced about Anko
Layouts?
• 1 parent RelativeLayout
▪ 17 ImageViews
▪ 1 SurfaceView
▪ 2 TextViews
▪ 1 View
Just visit the Wiki at https://bit.do/anko-constraint
Anko
Coroutines
•asReference()
•bg()
asReference()
MainActivity.kt
...
val ref: Ref<MainActivity> = this.asReference()
doAsync {
uiThread {
ref().updateUI()
}
}
...
bg()
MainActivity.kt
...
val ref: Ref<MainActivity> = this.asReference()
doAsync {
val someData = bg { getDataThatTakesLongTimeToFetch() }
uiThread {
ref().updateUI(someData.await())
}
}
...
Karamba
A small bunch of useful Kotlin extensions,
very similar to Anko Commons.
https://bit.do/karamba-kotlin
by matteocrippa
How would you convert a View to a
Bitmap?
val returnedBitmap = Bitmap.createBitmap(view.width,
view.height,Bitmap.Config.ARGB_8888)
val canvas = Canvas(returnedBitmap)
val bgDrawable = view.background
if (bgDrawable != null)
bgDrawable.draw(canvas)
else
canvas.drawColor(Color.WHITE)
view.draw(canvas)
return returnedBitmap
How would you convert a View to a
Bitmap?
val returnedBitmap = view.toBitmap()
How would you resize a Bitmap?
val scaleWidth = newWidth.toFloat() / oldWidth
val scaleHeight = newHeight.toFloat() / oldHeight
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
val resizedBitmap = Bitmap.createBitmap(
this, 0, 0, oldWidth, oldHeight, matrix, false)
resizedBitmap.recycle()
return resizedBitmap
How would you resize a Bitmap?
val resizedBitmap = oldBitmap.resize(newWidth, newHeight)
There’s a ton of stuff...
API VERSION:
• support(apiVersion)
• supportLollipop()
• supportKitkat()
BITMAP:
• base64()
• resize(width, height)
BOOLEAN:
• toggle()
• random()
DATE:
• convertTo(format)
[Eg:format=“dd-MM-yy”]
• toCalendar()
• isFuture()
• today()
DOUBLE:
• localCurrency(currency)
[Eg:currency=“USD”]
• celsiusToFahrenheit()
INTEGER:
• commaSeparatedId()
• readableDistanceFromMe
ters()
STRING:
• isValidEmail()
• isPhoneNumber()
• plainText()
• toBitmap()
VIEW:
• toBitmap()
+ more extension functions can be found at
https://bit.do/karamba-kotlin
Thank you.
Bapusaheb Patil
Google-certified Android
Developer & OpenClassrooms
Android Mentor
www.bapspatil.com
GitHub | github.com/bapspatil
LinkedIn | linkedin.com/in/bapspatil
Pinterest | pinterest.com/bapspatil
Twitter | twitter.com/baps_patil
(couldn’t get @bapspatil for this one! ☹)
Play Store | bit.do/bapsapps
Watchfaces | bit.do/bapswat

More Related Content

What's hot

Angular elements - embed your angular components EVERYWHERE
Angular elements - embed your angular components EVERYWHEREAngular elements - embed your angular components EVERYWHERE
Angular elements - embed your angular components EVERYWHERE
Nadav Mary
 
Design Patterns in XCUITest
Design Patterns in XCUITestDesign Patterns in XCUITest
Design Patterns in XCUITest
Vivian Liu
 
Wuff: Building Eclipse Applications and Plugins with Gradle
Wuff: Building Eclipse Applications and Plugins with GradleWuff: Building Eclipse Applications and Plugins with Gradle
Wuff: Building Eclipse Applications and Plugins with Gradle
Andrey Hihlovsky
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android development
GDGKuwaitGoogleDevel
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyond
Ramon Ribeiro Rabello
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
Yakov Fain
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
William Marques
 
React For Vikings
React For VikingsReact For Vikings
React For Vikings
FITC
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
Yakov Fain
 
Java Applets
Java AppletsJava Applets
Java Applets
Priyanshi Parashar
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
Katy Slemon
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
Commit University
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
Yakov Fain
 
HotPush with Ionic 2 and CodePush
HotPush with Ionic 2 and CodePushHotPush with Ionic 2 and CodePush
HotPush with Ionic 2 and CodePush
Evan Schultz
 
React JS
React JSReact JS
A journey beyond the page object pattern
A journey beyond the page object patternA journey beyond the page object pattern
A journey beyond the page object pattern
RiverGlide
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
Loiane Groner
 
Connect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with JavascriptConnect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with Javascript
joshcjensen
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Paul Jensen
 
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and PuppeteerE2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Paul Jensen
 

What's hot (20)

Angular elements - embed your angular components EVERYWHERE
Angular elements - embed your angular components EVERYWHEREAngular elements - embed your angular components EVERYWHERE
Angular elements - embed your angular components EVERYWHERE
 
Design Patterns in XCUITest
Design Patterns in XCUITestDesign Patterns in XCUITest
Design Patterns in XCUITest
 
Wuff: Building Eclipse Applications and Plugins with Gradle
Wuff: Building Eclipse Applications and Plugins with GradleWuff: Building Eclipse Applications and Plugins with Gradle
Wuff: Building Eclipse Applications and Plugins with Gradle
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android development
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyond
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
React For Vikings
React For VikingsReact For Vikings
React For Vikings
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
 
Java Applets
Java AppletsJava Applets
Java Applets
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
Angular 2 - The Next Framework
Angular 2 - The Next FrameworkAngular 2 - The Next Framework
Angular 2 - The Next Framework
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
HotPush with Ionic 2 and CodePush
HotPush with Ionic 2 and CodePushHotPush with Ionic 2 and CodePush
HotPush with Ionic 2 and CodePush
 
React JS
React JSReact JS
React JS
 
A journey beyond the page object pattern
A journey beyond the page object patternA journey beyond the page object pattern
A journey beyond the page object pattern
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
 
Connect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with JavascriptConnect.js 2015 - Building Native Mobile Applications with Javascript
Connect.js 2015 - Building Native Mobile Applications with Javascript
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
 
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and PuppeteerE2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
 

Similar to Anko & Karamba in Kotlin by Bapusaheb Patil

20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
Eric Deng
 
Eclipse 2011 Hot Topics
Eclipse 2011 Hot TopicsEclipse 2011 Hot Topics
Eclipse 2011 Hot Topics
Lars Vogel
 
Angular 2 Crash Course
Angular  2 Crash CourseAngular  2 Crash Course
Angular 2 Crash Course
Elisha Kramer
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
intelliyole
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
anshunjain
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)
Mikkel Flindt Heisterberg
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance Meetup
David Barreto
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 
Guides To Analyzing WebKit Performance
Guides To Analyzing WebKit PerformanceGuides To Analyzing WebKit Performance
Guides To Analyzing WebKit Performance
National Cheng Kung University
 
AOT(Ahead Of Time)
AOT(Ahead Of Time)AOT(Ahead Of Time)
AOT(Ahead Of Time)
Questpond
 
[143]Inside fuse deview 2016
[143]Inside fuse   deview 2016[143]Inside fuse   deview 2016
[143]Inside fuse deview 2016
NAVER D2
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
Carl Lu
 
Introduction to Groovy Monkey
Introduction to Groovy MonkeyIntroduction to Groovy Monkey
Introduction to Groovy Monkey
jervin
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
David Barreto
 
Dev Catchup @ARQGroup
Dev Catchup @ARQGroupDev Catchup @ARQGroup
Dev Catchup @ARQGroup
Karan Sharma
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
Fabrit Global
 
Compose Camp Day 3 PPT.pptx.pdf
Compose Camp Day 3 PPT.pptx.pdfCompose Camp Day 3 PPT.pptx.pdf
Compose Camp Day 3 PPT.pptx.pdf
METDSC
 
SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진
VMware Tanzu Korea
 
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
Paris Android User Group
 

Similar to Anko & Karamba in Kotlin by Bapusaheb Patil (20)

20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
Eclipse 2011 Hot Topics
Eclipse 2011 Hot TopicsEclipse 2011 Hot Topics
Eclipse 2011 Hot Topics
 
Angular 2 Crash Course
Angular  2 Crash CourseAngular  2 Crash Course
Angular 2 Crash Course
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance Meetup
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Guides To Analyzing WebKit Performance
Guides To Analyzing WebKit PerformanceGuides To Analyzing WebKit Performance
Guides To Analyzing WebKit Performance
 
AOT(Ahead Of Time)
AOT(Ahead Of Time)AOT(Ahead Of Time)
AOT(Ahead Of Time)
 
[143]Inside fuse deview 2016
[143]Inside fuse   deview 2016[143]Inside fuse   deview 2016
[143]Inside fuse deview 2016
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Introduction to Groovy Monkey
Introduction to Groovy MonkeyIntroduction to Groovy Monkey
Introduction to Groovy Monkey
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
 
Dev Catchup @ARQGroup
Dev Catchup @ARQGroupDev Catchup @ARQGroup
Dev Catchup @ARQGroup
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
 
Compose Camp Day 3 PPT.pptx.pdf
Compose Camp Day 3 PPT.pptx.pdfCompose Camp Day 3 PPT.pptx.pdf
Compose Camp Day 3 PPT.pptx.pdf
 
SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진
 
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
Introduction to Honeycomb APIs - Android Developer Lab 2011 Q3
 

Recently uploaded

Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 

Recently uploaded (20)

Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 

Anko & Karamba in Kotlin by Bapusaheb Patil

  • 1. Anko & Karamba in Kotlin Bapusaheb Patil Google-certified Android Developer & OpenClassrooms Android Mentor www.bapspatil.com
  • 2. My sample app for this talk: Ankara bit.do/baps-ankara
  • 4. What We Know: Kotlin reduces boilerplate (lengthy) code in Android Development, when compared with Java.
  • 5. Anko, if used in Kotlin, reduces boilerplate code even further. What We Need To Know:
  • 6. + A bunch of other super useful stuff I’ll be covering in this talk
  • 7. 4 Parts of Anko • Anko Commons • Anko Layouts • Anko SQLite • Anko Coroutines
  • 8. Setting up Anko... App-level build.gradle file: kotlin { experimental { coroutines “enable” } } dependencies { implementation “org.jetbrains.anko:anko:0.10.4” }
  • 9. Anko Commons Usage of Intents: INTENTS WITHOUT ANKO val intent = Intent(this, SomeActivity::class.java) intent.putExtra("id", 13) startActivity(intent)
  • 10. Anko Commons Usage of Intents: INTENTS WITH ANKO startActivity<SomeActivity>("id" to 13)
  • 11. Anko Commons Usage of Intents: BROWSER INTENT WITHOUT ANKO val uri = Uri.parse(“https://www.bapspatil.com”) val browserIntent = Intent(Intent.ACTION_VIEW, uri) startActivity(browserIntent)
  • 12. Anko Commons Usage of Intents: BROWSER INTENT WITH ANKO browse(“https://www.bapspatil.com”)
  • 13. Anko Commons Usage of Intents: CALL INTENT WITHOUT ANKO val uri = Uri.parse(“tel:+917788990099”) val callIntent = Intent(Intent.ACTION_DIAL) callIntent.data = uri startActivity(callIntent)
  • 14. Anko Commons Usage of Intents: CALL INTENT WITH ANKO makeCall(“+917788990099”)
  • 15. Anko Commons Usage of Common Android Components: TOAST WITHOUT ANKO Toast.makeText(context, “Hello, people of BlrKotlin!”, Toast.LENGTH_SHORT) .show()
  • 16. Anko Commons Usage of Common Android Components: TOAST WITH ANKO toast(“Hello, people of BlrKotlin!”) // longToast(“Hello, people of BlrKotlin!”)
  • 17. Anko Commons Usage of Common Android Components: SNACKBAR WITHOUT ANKO val snackbar = Snackbar.make(view, “Hello, people of BlrKotlin!”, Snackbar.LENGTH_SHORT) .setAction ( “HI!”, { v -> doSomething() }) snackbar.show()
  • 18. Anko Commons Usage of Common Android Components: SNACKBAR WITH ANKO snackbar(view, “Hello, people of BlrKotlin!”, “HI!”){ doSomething() }
  • 19. Anko Commons Usage of Common Android Components: ALERTDIALOG WITHOUT ANKO var alertDialog = AlertDialog.Builder(this) .setMessage("Hi, this is a message for the AlertDialog.") .setTitle("Title of the AlertDialog") .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, which -> doSomethingPositive() }) .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which -> doSomethingNegative() }) .create() .show()
  • 20. Anko Commons Usage of Common Android Components: ALERTDIALOG WITH ANKO alert(“Hi, this is a message for the AlertDialog.”, “Title of the AlertDialog”) { yesButton { doSomethingPositive() } noButton { doSomethingNegative() } }.show()
  • 21. Anko Commons Usage of Logging: LOGGING WITHOUT ANKO Log.v(“MainActivity”, “This activity was created successfully!”)
  • 22. Anko Commons Usage of Logging: LOGGING WITH ANKO class MainActivity : AppCompatActivity(), AnkoLogger { override fun onCreate(savedInstanceState: Bundle?) { verbose(“This activity was created successfully!”) } }
  • 24. But what’s wrong with XML? • It forces you to write the same code again. • It is not typesafe. • It is not null-safe. • No code reuse. • XML parsing is too much overhead. Takes too much CPU time, and hence battery.
  • 25. But what’s wrong with XML?
  • 26. Why not create the UI programmatically?
  • 27. Why not create the UI programmatically? Because...it looks ugly. Here’s an example: val activity = this val layout = LinearLayout(activity) layout.orientation = LinearLayout.VERTICAL val nameEditText = EditText(activity) val button = Button(activity) button.text = “Hey There!" button.setOnClickListener { Toast.makeText(activity, "Hello there, ${nameEditText.text}!", Toast.LENGTH_SHORT).show() } layout.addView(nameEditText) layout.addView(button)
  • 30. Anko Layouts MainActivity.kt override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) verticalLayout { val nameEditText = editText() button(“Hey There!”) { onClick { toast(“Hello there, ${nameEditText.text}!”) } } } }
  • 32. Let’s take a closer look at Anko Layouts...
  • 33. Attributes in Anko Layouts DSL verticalLayout { lparams(width = matchParent, height = matchParent) textView { id = R.id.helloTextView text = “Hello, people of BlrKotlin!” textSize = 32f textColor = 0xffffff.opaque padding = dip(16) gravity = Gravity.START // gravity in XML }.lparams(width = wrapContent, height = wrapContent) { gravity = Gravity.CENTER // layout_gravity in XML margin = dip(4) } }
  • 34. AnkoComponent<T> What is it & why use it? - It’s a separate class where you can write your DSL code for your UI. - It is reusable. - It keeps the UI separate from logic. - You can convert existing XML files to AnkoComponents with the Anko Support plugin. - You also get a layout preview if you install the Anko Support plugin.
  • 35. Keep UI code in a separate file!
  • 36. AnkoComponent<T> MainView.kt class MainView : AnkoComponent<MainActivity> { override fun createView(ui: AnkoContext<MainActivity>): View = with(ui) { verticalLayout { ... ... ... } } }
  • 37. AnkoComponent<T> MainActivity.kt class MainActivity : AppCompatActivity() { lateinit val textView: TextView override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) MainView().setContentView(this) textView = find(R.id.helloTextView) toast(textView.text) } }
  • 38. Still not convinced about Anko Layouts? • 1 parent RelativeLayout ▪ 17 ImageViews ▪ 1 SurfaceView ▪ 2 TextViews ▪ 1 View
  • 39. Just visit the Wiki at https://bit.do/anko-constraint
  • 41. asReference() MainActivity.kt ... val ref: Ref<MainActivity> = this.asReference() doAsync { uiThread { ref().updateUI() } } ...
  • 42. bg() MainActivity.kt ... val ref: Ref<MainActivity> = this.asReference() doAsync { val someData = bg { getDataThatTakesLongTimeToFetch() } uiThread { ref().updateUI(someData.await()) } } ...
  • 43. Karamba A small bunch of useful Kotlin extensions, very similar to Anko Commons. https://bit.do/karamba-kotlin by matteocrippa
  • 44. How would you convert a View to a Bitmap? val returnedBitmap = Bitmap.createBitmap(view.width, view.height,Bitmap.Config.ARGB_8888) val canvas = Canvas(returnedBitmap) val bgDrawable = view.background if (bgDrawable != null) bgDrawable.draw(canvas) else canvas.drawColor(Color.WHITE) view.draw(canvas) return returnedBitmap
  • 45. How would you convert a View to a Bitmap? val returnedBitmap = view.toBitmap()
  • 46. How would you resize a Bitmap? val scaleWidth = newWidth.toFloat() / oldWidth val scaleHeight = newHeight.toFloat() / oldHeight val matrix = Matrix() matrix.postScale(scaleWidth, scaleHeight) val resizedBitmap = Bitmap.createBitmap( this, 0, 0, oldWidth, oldHeight, matrix, false) resizedBitmap.recycle() return resizedBitmap
  • 47. How would you resize a Bitmap? val resizedBitmap = oldBitmap.resize(newWidth, newHeight)
  • 48. There’s a ton of stuff... API VERSION: • support(apiVersion) • supportLollipop() • supportKitkat() BITMAP: • base64() • resize(width, height) BOOLEAN: • toggle() • random() DATE: • convertTo(format) [Eg:format=“dd-MM-yy”] • toCalendar() • isFuture() • today() DOUBLE: • localCurrency(currency) [Eg:currency=“USD”] • celsiusToFahrenheit() INTEGER: • commaSeparatedId() • readableDistanceFromMe ters() STRING: • isValidEmail() • isPhoneNumber() • plainText() • toBitmap() VIEW: • toBitmap() + more extension functions can be found at https://bit.do/karamba-kotlin
  • 49.
  • 50. Thank you. Bapusaheb Patil Google-certified Android Developer & OpenClassrooms Android Mentor www.bapspatil.com GitHub | github.com/bapspatil LinkedIn | linkedin.com/in/bapspatil Pinterest | pinterest.com/bapspatil Twitter | twitter.com/baps_patil (couldn’t get @bapspatil for this one! ☹) Play Store | bit.do/bapsapps Watchfaces | bit.do/bapswat