Coding Android Applications Simplified (And Why Many Give Up).
In this tutorial we go over the basics of getting over the pitfalls of learning the Android application environment.
RTTEANS - Return Time To Enjoy A New Skill. Is defined as the amount of effort required to gain some type of emotional satisfaction from an endeavor. If what one is trying to learn is constantly painful, mundane, unrewarding, confusing, and or rewardless (or simply doesn't work) almost nobody will want to know it - even if driven by a real curiosity. Android presents this issue consider:
- There are real issues with learning to code android applications because it requires one not only master the Java langauge, it also requires learning the XML layout set, ConstraintLayouts, FrameLayouts, Fragments, Fragment Navigators, and the Activity Lifecycle to boot. Because all of these knowledge sets are pushed against a new learner basically at the same time - it is typically quite daunting.
- Of the people that can overcome these hurdles will then typically find out that nobody uses their programs, downloads them or even looks at them. That is a lot of toil to find out there is no return. It's like difficult blogging everyday in a second obscure language - only to put it into a hole in the ground that you bury and nobody sees. People usually want some kind of recognition for their work.
- If all of this is not enough a second language 'Kotlin' was added that is more obscure and abstract (but attempts to simplify it all).
- If all that is also not enough the entire Android EcoSystem is fiddled and manipulated by the cadre of Google Engineers who add some confusing feature, then embed it all into the basic example sets.
Code Footholding - The point a developer can modify the code in a understandable manner and understand the related result.
UI Choking - Where an experienced developer has worked in more efficient systems 'downgrades' to a more convoluted process of defining the UI.
A fellow developer who is experienced in numerous languages expresses the same disdain:
Goal : To Really Make This Simple - So Simple you Can get the RTTEANS down.
Advice: Do not get frustrated if you do not get this the first time, Sometimes it can take some days until it becomes intuitive and you hit the 'it clicked' together moment.
- Do you know Java at all, it is suggested to work through one of the basic primers.
2. Understand what is an Activity is - and its lifecycle.
- An activity is an application.
- A lifecycle is the phases of that application from when the user opens it until it is finally closed.
- Lifecycles are different than what you see on standard PC's in that the phone triages it's limited RAM to the open application, and causes the background applications to actually shutdown and save their settings. This is the onPause() action.
- When you rotate a phone from landscape to potrait it actually completely closes the open application onPause(), stores all its variables, and then reopens and rebuilds the application onResume(). Wild eh!?
- You do not always need to know all the running portions of a applications lifecyle when you are first learning - as most of the basic applications for learning can exist inside your oncreate().
Good? Learn this and then study it again until it is intuitive.
3. Understanding basic Layouts.
Layout - is a XML formatting specification on how a object or entity will be displayed. For instance this is the XML text layout for a button.
To See the XML text layout we select the following button in the Editor.
If we Select the design (top right button) it shows what the button will graphically look like:
In the palette we can see the types of Layouts that can contain the buttons themselves:
Each type of layout takes some practise, and is under constant evolution:
I recommend taking some time to study Philipp Lackner's videos. He does a very good job. He does not race his videos (many tutorials are rambling junk) and you can pick up a lot.
Have patience...
It cannot be stressed that Layouts can be very frustrating as it was for me coming from static layout engines that were very simple and explicit to the Android system that was trying to develop a layout system that could run on a billion disparate Android phones, tablets, and other devices. In older systems such as Lazarus this was extremely easy to do - but here it is a real learning curve, riddled with issues and a lot of misguidance.
3. Learn Code to XML context and onClick
Now that the basic concept of various Layouts is starting to be understood - we will go over event handlers. Android documentation IMHO does a very poor job of this. We will try to make this very simple.
- Context defines the contextual code block that is married to the XML
- onClick is the code class inside the context block of code for handling a Button or Single Event Handler.
Two Important XML Layout Points (A) and (B)
- (A) defines the associate code block (.MainActivity) for the entire XML Layout.
- (B) defines the event handler for the single button '@+id/button5'
Inside our main TreeView we have the following (MainActivity)
Inside MainActivity we have the declaration of the associate class that ties to the XML:
Which when you open shows as:
In essence we want to connect an event (user button press to start) to a block of code.
So reviewing it again - tools:context=".MainActivity" hooks the code block above to the XML definition of the ScrollView.
Now the Event Handler Hook.
- If you take any random XML object say the button and simply add an event handler as in:
If you hold you mouse over it it will make the suggestion to write the code.
It will show inside your MainActivity class as:
4. Learn How A View is Referenced And Displayed.
Next we want to know how the code knows which resource XML to 'inflate for view'
- A View is the programmatic object that will display one of the above XML layouts. It is inside your Java code.
- The code that sets the appropriate XML for reference is as C.
- R.layout.activity_main is referencing the resources as:
- R - Res
- layout - Layout Folder
- activity_main - activity_main.xml
setContentView(R.layout.activity_main);
Understand FindViewById is also where you need to find a single object.
- You must match the object type TextView to TextView, View to View etc.
TextView myTextView = findViewById(R.id.id_text_view);
5. Finally Learn How Add a Second View (Easily)
- After a while clearly one would want more than one screen on these small displays - here is the most simple way to do it.
- Make a new Layout.
- Set both to use the same .MainActivity as in:
- Give both a 'onClick' handler
- Because they are sharing the same .MainActivity - each one can simply 'setView' as in:
In your virtual phone you will be able to click navigate between the two:
That was a LOT of learning to get to 'Hello World' but from there one can quickly add to this knowledge set building much more complex and feature rich applications.
Mometum. Finally momentum is key. Writing one application is not going to change you or anyones world, and 97% of application downloads are free.