From c1e672c8ce718e7a42c5ba9c296a0c1942881eee Mon Sep 17 00:00:00 2001 From: Aditya Balwani Date: Sun, 14 Aug 2016 20:37:37 -0400 Subject: [PATCH 1/3] Updated android springboard --- springboard-projects/android.md | 286 +++++++------------------------- 1 file changed, 59 insertions(+), 227 deletions(-) diff --git a/springboard-projects/android.md b/springboard-projects/android.md index 619afa4..5e78b71 100644 --- a/springboard-projects/android.md +++ b/springboard-projects/android.md @@ -53,7 +53,7 @@ Development in android is done in Android Studio, a Google developed IDE that ma * Actionbar/Toolbar - The toolbar at the top is known as the ActionBar (Renamed to Toolbar in Lollipop) - It is optional, but useful - - Usually contains frequently needed actions and options menu + - Usually contains frequently needed actions and options menu and title * Fragment - Fragments are subsets of activities - Multiple fragments in one activity @@ -76,19 +76,25 @@ https://github.com/adibalwani03/ClickZilla ![New Project](img/startproject.PNG) +### Name your application + +On this screen you will enter your application name and your company domain. If you dont have a website then you can pick any domain but it should be unique because 2 applications with the same package name cannot exist. + +![Application Name](img/applicationname.png) + ### Support You can support as many systems as you want. I tend to prefer supporting anything Android 4.0 or above. -Android 2.3 is now dead, so no need to support that and android 3.0 is just plain weird so I like to pretend that never happened. +Android 2.3 is now dead, so no need to support that and Android 3.0 is just plain weird so I like to pretend that never happened. -You can also add support for Google glass, TV and Wear if you want. +You can also add support for Google glass, TV, Auto and Wear if you want. ![Android Support](img/choosesdk.png) ### Activity Templates -And now you’ll be taken to the templates screen which contain prebuild activities you can use. For this one, we will be sticking to a blank activity (or a blank with fragment) but you can choose anything you want (Except Login activity and preference acitivity, those are for specific purposes) +And now you’ll be taken to the templates screen which contain prebuild activities you can use. For this one, we will be sticking to a basic activity but you can also pick blank activity which is just a basic activity without some of the boilerplate code. The other templates are for more specific situations but if any of them fit your app then you can choose that. ![Choose Activity Type](img/chooseactivity.png) @@ -130,12 +136,15 @@ Over the next few slides I’ll go over the automatically generated code and the ### File Structure Here you can see the basic file structure of your app. Each folder here has a specific purpose which the following slides will explain. + ![File Structure](img/filestructure.png) #### Manifests The Manifests directory is where you declare your AndroidManifest.xml and any other manifests. + ![Manifests](img/manifests.png) + The purpose of the manifest is to specify things for the device including declarations of all activities, all permissions required (eg. Internet, Wifi, I/O), special hardware requirements (camera, flash, gyro etc) and certain google APIs (eg. maps) and other things that can possibly affect compatibility. NOTE : Some of the tasks a manifest does is now also being done by the build.gradle such as declaring minimum android version @@ -145,7 +154,8 @@ NOTE : Some of the tasks a manifest does is now also being done by the build.gra The java folder is where all your packages are declared, and inside those are all your java files. Most of your code would go in the main package. -I personally have never used the AndroidTest package, so I can’t say much on that, but i’m guessing its for making test cases for the app (If someone knows, do tell me and will update the slides). +I personally have never used the AndroidTest package, so I can’t say much on that, but i’m guessing its for making test cases for the app (If someone knows, do tell me and will update the project). + ![Java](img/javafolders.png) #### Res @@ -169,21 +179,37 @@ The res folder is the real deal. This folder contains all the graphics+ui stuff This is where all the gradle files reside. Usually you’ll only be making small changes to the build.gradle in the App Module Other than that, its mostly left untouched + ![Gradle](img/gradlefolder.png) ### Layouts -Now lets head over the activity_main.xml and you’ll probably be brought the design GUI. From the left pane you can drag and drop objects onto the screen and the layout xml will automatically change to add the new item. +Now lets head over the activity_main.xml content_main.xml and you’ll probably be brought the design GUI. From the left pane you can drag and drop objects onto the screen and the layout xml will automatically change to add the new item. Easy peasy stuff, but often annoying. And so we move onto the actual XML -> #### activity_main.xml (text view) -So this XML is where your layout is defined. +So this XML is where your layout is defined. This file is used to define the outer structure of the activity. Going through it line by line we have : -* <RelativeLayout - - This is the declaration of the layout, which in this case is Relative layout which means all objects are located in relation to a parent which is declared using android:layout_<position>=”” +* <?xml + - This is the xml version and encoding of the file. +* <CoordinatorLayout + - This is the declaration of the layout, which in this case is the Coordinator Layout. As the name suggests, this type of layout is to coordinate the movement of various views inside and moving the views in relation to each other. For example, you can make the toolbar shorter as you scroll down etc. + +* <AppBarLayout + - This is the declration of the app bar of your app. The app bar contains the toolbar and anything else you want to keep inside it. +* <include + - This line tells android to include this other layout file in here. This is used to seperate the layout and keep the code clean and reusable. +* <FloatingActionBar + - The floating action bar, also known as a FAB is a button which is used to trigger the primary action of your app. For example, in Gmail its for composing an email, in a music app it can be to play a song etc. + + +#### content_main.xml (text view) + +* <Relative Layout + - Another layout declaration, which in this case is Relative layout which means all objects are located in relation to a parent which is declared using android:layout_<position>=”” - I find this to be one of the easiest ways to declare a layout. - Other popular layouts include LinearLayout, Frame, Grid and Table. You could also use ListView and ScrollView - ignore the xmlns:android line, this is essentially the include of your layout, declaring the format the xml will follow and things you can place inside. You only need to do this for the Root and is often auto-generated @@ -196,13 +222,19 @@ Going through it line by line we have : The autogenerated java code. It contains the following things : -* `public class MainActivity extends ActionBarActivity` - - Declaration of your Activity and it inherits from ActionBarAcitivity (which in turn inherits from FragmentActivity which inhertis from Activity), which means you will have an Actionbar in your app and you can put Fragments in it too. +* `public class MainActivity extends AppCompatActivity` + - Declaration of your Activity and it inherits from AppCompatActivity (which in turn inherits from FragmentActivity which inhertis from Activity), which means you will have an Actionbar in your app and you can put Fragments in it too. - You can change it to just Activity in which case it won’t have an actionbar or fragment support (which you don’t need for this one) * `protected void onCreate(Bundle savedInstanceState)` - This method is automatically executed when this Activity is created. It is essentially your constructor. - setContentView(R.layout.activity_main); + This line of code is what “inflates” your activity, which essentially means it takes the layout you specified (in this case, activity_main.xml) and creates your activity to look like the specified layout + - `Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);` + + Once the layout is inflated, you can acquire handles to the various objects in it. In this line we get an object of the Toolbar, and we can then programmatically affect it. + - `FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);` + + Same as above, we get an object of the FAB + - `fab.setOnClickListener(new View.OnClickListener() {` + + Set the action that should be performed when the FAB is clicked * `public boolean onCreateOptionsMenu(Menu menu)` - Similar to the previous method, this one inflates the OptionsMenu using the specified menu layout and is also called automatically * `public boolean onOptionsItemSelected(MenuItem item) {` @@ -222,7 +254,7 @@ Adding a button and Showing messages to users ### Modifying TextView -So lets start with adding a button to the activity_main.xml. You can remove the existing textview if you want, or you can keep it, i’ll be keeping it for now, but changing the text to “Welcome To ClickZilla”. Also, if you’re keeping it, add the following lines of code to the textview declaration : +So lets start with adding a button to the content_main.xml. You can remove the existing textview if you want, or you can keep it, i’ll be keeping it for now, but changing the text to “Welcome To ClickZilla”. Also, if you’re keeping it, add the following lines of code to the textview declaration : android:id="@+id/welcome" android:layout_alignParentTop="true" @@ -234,7 +266,7 @@ IDs are given to views to refer to them in the program or for layout purposes an Now, to add the button, add the following block of code inside the RelativeLayout : -{% highlight xml %} +{% highlight xml %}