Learning Kotlin through Android Studio...

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
Just purchased this book. I need to develop an app for android phones, and it seemed like one of the best available out there. I'm going to log my progress here. Anyone who wants to join the discussion is welcome to crash the party.

1584581317068.png
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
I'm 1/4 through the book, and it's proving a bit of challenge for me, but I'm managing. It's written for people already familiar with Java (I'm not) and some chapters are a bit dense for me. So I'm taking things slowly.

I downloaded the IntelliJ Kotlin compiler and I'm running the example code snippets listed in the book. That approach has been extremely helpful.
 

wayneh

Joined Sep 9, 2010
17,498
... it's proving a bit of challenge for me, but I'm managing. It's written for people already familiar with Java (I'm not) and some chapters are a bit dense for me.
It's tough to get going. I feel for you. I went from zero to a finished app in the AppStore and I can tell you it was pulling teeth the whole way. That's partly because my app is quite complex for a first project but mostly the hurdle was ignorance. I'm working on another app now and it's a joy to just get stuff done instead of try to learn stuff. This time I'm spending far more time on the math and logic under the hood instead of interface issues and all the other little hurdles; how to change fonts, background color, position of an item when screen size changes between devices, and a million other details. The hardest concept for me to grasp was asynchronous processing, where you ask the system to do something and carry on while that gets done, and then intercept it when it comes back. Sounds simple enough but it's nothing like the linear execution in good old Fortran or Excel for that matter.

One thing I've just accomplished is to incorporate java code - a project pulled from the internet - into my latest iOS app project. This would normally be impossible but I discovered that Google had a big base of java they wanted to port to iOS, and so they built a tool to translate java code to Obj-C. They offer the tool for free. It wasn't easy getting going but a tech genius at Google answered my inquiry, had patience and took the time to drag me through it. So I've now got the chops to use C or java projects created by others, as long as they are self-contained. I tried the same thing with Python and C++ but the technical hurdles were too much for me.

So if you write some cool java code, it can also run on an iPhone. This is only true of the code you can't see. Someone would still have to build an interface. Apple's tools are pretty good at getting that done.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
Thanks for chiming in, W. Your observations are thoroughly appreciated.

Fortunately, I'm no stranger to OOP and event oriented programming. I've been programming in .NET for quite a few years now, and I'm familiar with structures and object properties and all that. It's kotlin's extremely dense structure that I find a little hard to learn. For instance, calling functions within functions and treating them as types, etc.

I chose Kotlin mainly because of Google's strong support and promotion, and also because of its ease of translation between Android and iOS... but at this point I'm in the baby steps phase.

Please stay tuned, I'm sure I'll come back here every once in a while not just to log my progress, but also to ask specific questions about syntax and type handling and such. I have a hunch you may be able to help me with that sort of things.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
You might also consider Ionic -- especially if you intend to write apps for both Android and iOS. The same Ionic code works for both.
Darn you, Joey... you made me doubt my decision ...

Per your suggestion, I looked up the Ionic framework, and was quite tempted to change course. Except that this kept me at the fence:


1584770425778.png

It were the previous points that made me stick to AS3's Kotlin, plus that a few years ago AS3 announced that during the course of this year a "translation package" of sorts was to be launched for iOS
 

bogosort

Joined Sep 24, 2011
696
Another alternative: make your application a web app. That would allow you to write the business logic on the server in whatever language you prefer. For the frontend UI, a responsive framework like Angular or React will handle the displays for all the different platforms. There's a learning curve, but -- unlike phone APIs -- these frameworks are universal: learn once, apply anywhere.

On the phone side, you'd write a very simple app that would just be a container for a built-in browser that loads your web page.
 

John P

Joined Oct 14, 2008
2,026
While the topic of writing code for Android is open--does anyone have an opinion on B4A (Basic for Android)?

https://www.b4x.com/b4a.html

Just totally casually, it would be fun to be able to write Android apps, and we've all got time to spare these days. But I want a method that has an easy learning curve!
 

wayneh

Joined Sep 9, 2010
17,498
But I want a method that has an easy learning curve!
Good luck with that!
All the computer companies try to make their tools easy for beginners to get started. They hold sessions for kids to pretend to create apps. I say pretend because it's all a bit of a lie, in my opinion. Yes, you can crank out "Hello World" in your first session. That's not what most kids dream of. The reality is that the cumulative amount of stuff you need to learn to go from zero to a commercial app is daunting and not something any school kid could do. Even with a completed app, you still face a tedious process to submit it, get it listed and all that. You need a lot of persistence at every stage.
 

atferrari

Joined Jan 6, 2004
4,770
While the topic of writing code for Android is open--does anyone have an opinion on B4A (Basic for Android)?

https://www.b4x.com/b4a.html

Just totally casually, it would be fun to be able to write Android apps, and we've all got time to spare these days. But I want a method that has an easy learning curve!
Not about B4A but RFO BASIC. I took the pain to learn it (not too hard) and managed to create a simple app that did what I wanted. After being out of the game for 3 years, when I was about to resume my practice, found out that development was discontinued and two spinoffs are now in the wake. Frustrating.
 

John P

Joined Oct 14, 2008
2,026
I wasn't really thinking of a commercial-quality app, more something that I'd use myself. One idea I had would be a customizable control panel and display, interfaced to some microcontroller-based device via Bluetooth. (With a UART-to-Bluetooth module at the processor side.)

I got RFO Basic too, and was disappointed when it basically became an orphan product. What was attractive about that was that the author had done his best to think of everything, and give you a simple routine to control it. Graphics, Bluetooth, camera interface, it all seemed to be there.

Here's another one. A precision position-control device, where a target would be placed in front of the camera, it would be recognized and commands sent out to operate a positioning system to place it exactly. The target could be a barcode, so you could have lots of them, individually identified. It might be worth buying a cheap tablet just to build into an application like this.
 

atferrari

Joined Jan 6, 2004
4,770
I got RFO Basic too, and was disappointed when it basically became an orphan product. What was attractive about that was that the author had done his best to think of everything, and give you a simple routine to control it. Graphics, Bluetooth, camera interface, it all seemed to be there.
individually identified. It might be worth buying a cheap tablet just to build into an application like this.
In spite I asked in their forum about what happened nobody answered my question. I felt really angry for the effort thrown overboard and how good it was. And easy to use. :mad::(
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
Ok. I've just finished the book I bought, and I must say that I'm almost as lost as when I started.

First, an extremely simple code. What it does is it displays a "12345" text in a textbox, and then it deletes the rightmost character when a button is pressed until it becomes an empty string. It then starts over.
Code:
package com.example.prueba02

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textHello.text="12345"
    }

    fun trimNumber(v:View){
        if(textHello.text.toString().isNotEmpty()) {   //instead of: textHello.text.toString().length != 0
            textHello.text =
                textHello.text.toString().substring(0, textHello.text.toString().length - 1)
        } else {
            textHello.text="12345"
        }
    }
}
1585509135902.png
 

wayneh

Joined Sep 9, 2010
17,498
Ok. I've just finished the book I bought, and I must say that I'm almost as lost as when I started.
The world is your oyster now!

Getting that first app up and running really is a big step. As you know, it means you've met the hardware and software requirements, got through all the necessary installations and such, and now can actually get down to work. Not to diminish what still lies ahead but it's good to get moving.
 

joeyd999

Joined Jun 6, 2011
5,283
Ok. I've just finished the book I bought, and I must say that I'm almost as lost as when I started.

First, an extremely simple code. What it does is it displays a "12345" text in a textbox, and then it deletes the rightmost character when a button is pressed until it becomes an empty string. It then starts over.
Code:
package com.example.prueba02

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textHello.text="12345"
    }

    fun trimNumber(v:View){
        if(textHello.text.toString().isNotEmpty()) {   //instead of: textHello.text.toString().length != 0
            textHello.text =
                textHello.text.toString().substring(0, textHello.text.toString().length - 1)
        } else {
            textHello.text="12345"
        }
    }
}
Make sure you test every possible Android version, mobile device, and screen size...
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,253
Here's my first attempt at serious coding. I want to scan available WiFi access points, and then display them in a textbox. So far, it's clear that I don't yet have full understanding of the structure in which Kotlin works:

Code:
package com.example.prueba02

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // findViewById<TextView>(R.id.textHello).text="12345"
        textHello.text="12345"
    }

    fun addNumber(v:View){
        ScanForNetworks()
    }

    fun ScanForNetworks(){
        val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wifiScanReceiver = object : BroadcastReceiver() {

            fun scanSuccess() {
                val results = wifiManager.scanResults
                textHello.text="Success"
                // ... use new scan results ...
            }

            fun scanFailure() {
                // handle failure: new scan did NOT succeed
                // consider using old scan results: these are the OLD results!
                val results = wifiManager.scanResults
                textHello.text="Failure"
                // ... potentially use older scan results ...
            }

            override fun onReceive(context: Context, intent: Intent) {
                val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
                if (success) {
                    scanSuccess()
                } else {
                    scanFailure()
                }
            }
        }

        wifiManager.startScan()
        textHello.text = wifiManager.toString()

    }
}
What the previous code attempts to do, is to list the available hotspots in the textHello textbox when the button is pressed... but all I get is a couple of empty brackets. :(

1585519911778.png
 
Top