-
Notifications
You must be signed in to change notification settings - Fork 1
Android Basics
Go into Android Studio and choose, Start a New Android Studio Project.
- Use the
Create a New Project - Choose a basic API that runs on at least 85% of the phones. Currently, that is Lollipop.
- Look up Android Platform Version to see distribution.
There are two Gradle folders
- one is for the project as a whole, which is generally not touched.
- The second is the for the individual project, which is where dependencies are added as needed.
Class MainActivity
- Gets created automatically
- This is where you put your setup code to set up your page for the first time.
- After the first automatically created line, you add in code for: ** Event listeners ** Fetch you need in order to render the app ** Etc
Can add string values for things like buttons, labels, etc, in the string.xml file, found in app > res > values.
- "B" on the programmer's keyboard will toggle between blueprint mode and actual view
- Go to Palette, click container, and scroll views and other fancy options are available.
- Go to Palette, click layout, and there are ways to layout div-like sections within another element.
- Linear layout provides a flex-box-like organization to the app. Very commonly used.
- Change visibility to invisible. Just below layouts on the right hand side.
- Set up an event listener for a button that makes the section visible upon a button click
- See sample code from Code 401-Javad6: https://github.com/codefellows/seattle-java-401d6/blob/master/class-26/BuyCheapStuff/app/src/main/java/com/ferreirae/buycheapstuff/MainActivity.java
- Check that button has an id value (in this example, id = button), and that the section we want to make visible has an id (id=results in this example).
- Go to MainActivity.java
- Grab that button using its id and the generated R (resource). This makes sure there actually is an id. Then, set an onClickListener, which uses lambdas in place of callback functions. Click on the red bulb next to the red squiggles and click on "replace lambda with anonymous class." We need to define a class in line, where we need that class.
Approximately the same as a page / view
One activity is approximately a page
The main activity is the one that is started when the app is launched. This one is very important.
Other activities can occur as well.
go to folder that holds MainActivity class, and make a new empty activity (same was as making a Java class, but scroll all the way down to the android options).
How you, as the developer, state what you intend to happen next.
There are:
- Explicit intents - this exact activity in my app. "I intend to start up this other activity now"
- Implicit intents - android, do this sort of thing. Such as, when you hit the share button, and lots of apps appear to help you share. Or, when you take/pick photo, or get directions, or call this number. So, any app, can say, hey, do you want to make a phone call with one of these many apps? Implicit intent allows the app to reroute to an entirely different app to allow the user to complete a task. Allows for a very high level of flexibility, but it does make the code a little more complicated.
Share is also called ACTION_SEND, by the way. Very popular to register for the app. ACTION_CALL (helps user to call)
For demo code on making an implicit intent that lets a user buy something, see lecture on October 21 from Code401d6
Called Shared Preferences, aka Key-Value Data
Shared preferences is like local storage.
Resource: https://developer.android.com/training/data-storage/shared-preferences
- Default shared preference for current activity
- Limited to just one activity
- Default shared preference for current app
- Things cannot be open by other applications, so it's secure in that way.
- This one is usually the one to choose!
- Specific shared preference by filename
- Could type the wrong filename
- But, this means you can have a shared preference file that you can share across locations.
See Oct 22 Code401Javad6 class: SettingsActivity, build.gradle, and MainActivity
- Add something to the page, in the example, it's a little plus sign.
- Add method that allows an onclick method, in example, it's called onSaveButtonPressed(View view).
- Androidx is a class that has older options - have to add a couple things in the build.gradle file as a dependency.
- Must create the editor for the shared preference by calling .edit on the instance of SharedPreferences.
SharedPreferences.Editor editor = prefs.edit();
- Now, save the key, value pair
- We know we saved the key value pair, in this case, a username, by adding in a textview on the top of the page. Add in a static "Hi", and fetch the data from shared prefs.
- Note that the onCreate method runs upon startup of the app. If you put something to be updated with key-value pairs in this method, it will only update upon start of the app. Meanwhile, onResume() is called upon continuing activity, so adding a username to top of page better in this method.
See Resource: Activity-LifeCycle to determine what type of method may be needed to support lifecycle of the app, for android to call at the appropriate time. Figure 1 is gold.
Key value pairs that can be added into an intent. They are temporary. Approximately like query parameters.
They are useful for within your app, or for other apps. Allows you to add extras into an intent and make them accessible.
They are useful now, then gone. As soon as the user closes the app, those intent extras are gone.
Meanwhile, Shared Preferences are forever. This is perfect for a username that we want forever. It's overkill to use shared preferences for more transient data, such as showing up the title on a details page.
Shared preferences is expensive. For small key value pairs of data, it's more lightweight than a database, but it's still doing longterm reading/writing of data on the phone.
Shared preferences allow a username to be stored, so when the user returns to the app, the username is still available.
Example - We have a thing we type in that we want to buy, we go to the buying page, and we want that thing appear on the buying page. We have to pass the data regarding the item.
- Over in MainActivity, add some extra info about exactly what thing is being purchased, with .putExtra().
- Over in BuyItem.java, we need to add code that allows us to receive those extras - we want to grab data out of the intent. Can add in a:
String itemName = getIntent().getStringExtra("item");
- Note that there are parcelableExtra or SerializableExtra, so it can be serialized into the intent, and then deserialized, almost like JSON, where something is serialized and parced.
- Now, we grab the label from the page and set its text to be the item name BuyItem.java page, given this example.
- Head back to MainActivity, and check that it is grabbing text that was typed in. We can create a String variable to hold that text, so we can output it on the Buy page.
- Note that there are two lines of code added into class demo that allows the keyboard for the user to disappear once you hit submit - see within MainActivity.java.
- This all may be how we set up Task Titles for lab.