Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 1 Part 2 Android Programming. Chapter objectives: Understand how to create a simple Android project Understand how to create a manifest file Understand.

Similar presentations


Presentation on theme: "CHAPTER 1 Part 2 Android Programming. Chapter objectives: Understand how to create a simple Android project Understand how to create a manifest file Understand."— Presentation transcript:

1 CHAPTER 1 Part 2 Android Programming

2 Chapter objectives: Understand how to create a simple Android project Understand how to create a manifest file Understand how to import resources Understand how to run an app on an Android device Prerequisite: must know Java!

3 Tutorial 1 Need resources from the book. http://www.jblearning.com/catalog/978128407 0705/

4 Creating a project Starting a project – File -> new -> New Project… – Must specify the minimum SDK. More devices vs more features – Choose template Creates a type of activity Note that the book asks for a different type of activity (Blank) that you may see (Empty) choose “Empty” – Name the activity

5 Creating a project There are default activity and layout names – You can change to whatever you want – The tutorials use the default Do not use a fragment yet. Click finish May run into various problems – Filesystem case-sensitivity problem (easy to fix) – Missing styles on layout (fix automatically) – Failed to find toolbarStyle can ignore

6 Tutorial 1: Hello Goodbye app Add and Edit Resources – Text values, graphic files, data values, color def Hello Goodbye app – Drawables: 3 graphic files (background image, button image, graphic image) May have to copy the launch icon in also! – String values: “hello”, “goodbye” – Color values: color for “hello” text and “goodbye” text.

7 Resources Image file names must be lowercase Drawable need different pixel densities (see previous pptx slides) Different folders for different densities – If a folder is not present, Android will create it when you add image – If it won’t create the folder, you can create it yourself. – See next slide.

8 Resources Different folders for different densities – If a folder is not present, Android will create it when you add image – If it won’t create the folder, you can create it yourself. – App/src/main/res/drawable-xxhdpi – do this in Android studio; could also do in finder – For xxhdpi image should be in folder “drawable-xxhdpi” at the same level as the “drawable” folder – If you do this correctly, you should see the structure shown in figure 1-25 – You can paste the xxhdpi background.png into both the xxhdpi folder and the xhdpi folder.

9 Resources

10 Other Resources The tutorial will have you create strings in the res/values/strings.xml file You will create a color in the res/values/color.xml file but must create the file first. #B530BD

11 Constructing the Interface Layout files in XML There is a default layout created when the project is created The tutorial uses the file activity_main.xml Two modes: – Design (visual). Similar to Java Swing – Text. XML.

12 Interface What to do if your interface won’t render: choose a different version of the API

13 Interface Construction Canvas – the screen Palette – elements that can be dragged to the canvas Android Version – that is used to render the interface Device – each device has it’s own look and feel Orientation – portrait or landscape or UI Mode such as TV AppTheme – configuration theme, e.g., all balck, full screen with no title

14 Interface Construction Outline– the hierarchical list of visual elements placed on the layout file. Root is a “container”; will explore these later. Properties – each visual object has a collection of context-sensitive properties that can be set, e.g., color, width, etc. Mode Tabs – can build interface via text or visually

15 Building the Interface Note that you might have a CustomView – this layout might have a line in the XML like – You should open the layout “content_main” This will have a RelativeLayout may have to edit the content_main.xml, e.g., to delete the “hello world!” text or to add text. – Instructions in the tutorial will still work

16 Changing Background Your resource dialog box may look like this! This is the “drawable drawer”

17 Building the Interface Note: use the resources (string, color) as described in the tutorial in your Java code. – Do NOT enter text directly, even though this works!

18 Sizing Elements Sizes of GUI components and text can be specified in dp or sp Notation dp: density-independent pixel – 1 dp = 1 pixel on a 160-dpi screen. – On a 240-dpi screen each dp pixel is scaled by a factor of 240/160 – The sp = scale independent pixel. – Scaled like dp but use the user’s preferred size (as specified in settings)

19 Where Code Goes Apps can have several screens – Each screen is coded separately – A screen is called an “activity” Every screen has two files associated with them: – A java file that contains code – A XML file that describes the look (components, background, etc.) of the page

20 Adding Code Will code the MainActivity class – The MainActivity.java class is created with the project – Represents the main Java class that will be executed when the app is launched. – This class is a subset of the Activity class Inherits method callbacks when activity is created, paused, stopped, resumed, destroyed

21 Adding Code Two objectives – Inflate the user interface layout – Control and respond to the UI elements

22 Class Variables public class MainActivity extends AppCompatActivity { private TextView greetingTextView; private boolean isHello; private void initializeGreeting(){ isHello = true; }

23 Inflating onCreate() – Callback method, called when activity is created – Used to initialize the actviity, e.g., set up user interface, initialize state

24 onCreate() protected void onCreate(Bundle savedInstanceState) { // Task 1 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Task 2 greetingTextView = (TextView) findViewById(R.id.textView); // Task 3 initializeGreeting(); // Task 4 Button exclaimBtn = (Button) findViewById(R.id.button); exclaimBtn.setOnClickListener(toggleGreeting); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } Initialize the app, associate this code with It’s layout. These are always done. Task 2: get a pointer to the text box. Task 3: set the value of the global variable “isHello”

25 Accessing Resources Can refer to resources with the constant “R”, e.g., R.id.textView // refers to the resource with // id of “textView” Recall that the text box in the interface had name “textView” in the strings.xml file: <TextView android:id="@+id/textView” …more stuff

26 onCreate() protected void onCreate(Bundle savedInstanceState) { // Task 1 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Task 2 greetingTextView = (TextView) findViewById(R.id.textView); // Task 3 initializeGreeting(); // Task 4 Button exclaimBtn = (Button) findViewById(R.id.button); exclaimBtn.setOnClickListener(toggleGreeting); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } This is the listener. The actual variable “toggleGreeting” is an instance of a View.OnClickListener

27 Adding Code Registering an onClick listener: – We need to associate code with a click on the button – Do this by “registering” a “listener” method to the “onClick” event for the button: Button exclaimBtn = (Button) findViewById(R.id.button); exclaimBtn.setOnClickListener(toggleGreeting); This is the name of the method (defined by us in this class) that will be called when the event happens

28 Adding Code This is the event handler (a method that is called when the event is triggered). – Uses an inner class, i.e., a class that is defined “on the fly” in the code. private final View.OnClickListener toggleGreeting = new View.onClickListener(){ public void onClick(View btn){ // Task: construct the toggle greeting if (isHello){ isHello = false; greetingTextView.setText(R.string.goodbye); } else { isHello = true; greetingTextView.setText(R.string.hello); } } }; This variable will hold an instance of an OnClicListener class that is defined here

29 Adding Code new View.onClickListener(){ public void onClick(View btn){ // Task: construct the toggle greeting if (isHello){ isHello = false; greetingTextView.setText(R.string.goodbye); } else { isHello = true; greetingTextView.setText(R.string.hello); } } }; Creates an instance of the class View.onClickListener The class has no variables and one method: onClick(View btn) One class variable is tested: isHello greetingTextView points to the text box. We set its value using a resource from string.xml

30 building Uses scripts in the Gradle Scripts directory – Build.gradle – Provides locations of the app’s manifest, source, resources, assets. – The Android Asset Packaging Tool (aapt) collects all XML files and compiles into a java file: R.java – R.java provides reference to resources – Java compiler produces.class files – The dex tool compiles.class to Dalvik bytecode – Apkbuilder tool packages.dex, resources, etc. to.apk

31 Running on a device Build->Rebuild Project – Compiled then packaged into.apk – Includes.dex files (Dalvik byte code), binary version of manifest, resources, etc. – App must be signed In debug mode Android Studio signs for you To release you need a private key

32 Running on a device Can use any Android device – Must configure the device for development use – See textbook – Note that developer options may not be visible on your device Settings->about tablet/phone and press “Build” 7 times – When developer options are visible, toggle to enable Connect to your computer via USB

33 running Plug in device Run->Run ‘app’ Choose device to run on Note that a device has to have developer mode enabled. Go to settings  About phone and tap the Build number 7 times


Download ppt "CHAPTER 1 Part 2 Android Programming. Chapter objectives: Understand how to create a simple Android project Understand how to create a manifest file Understand."

Similar presentations


Ads by Google