Android Development Training – Building Your First App

Understand basic XML compontents of Android user interface

– AndroidManifest.xml – I think this is the first xml to run before the main_layout.xml files
– fragment_main.xml – main view layout, viewgroup that holds views
– strings.xml – xml document that holds layout string object
– R.id.something something – resource file that holds references to your apps resource object.

Is the viewgroup container that holds the elements you see on screen ie; buttons, text fields?

Yes the viewgroup is a container that holds the elements you see on screen like buttons and text fields. It appears that these elements are referred to as views.

initial ultimate goal – create a button that calls a java resourse.

After having no luck opening my app in the AVD (android virtual device) and on my tablet I decided to start from scratch. The app would install but would not open automatically nor display an icon anywhere in the launcher. I decided to go into the android SDK manager and install Android 4.4.2 API 19 Kitkat SDK rather than 4.4W (wear stupid) or L 20 api preview.

This time I created a new project and chose activity and fragment rather than blank activity. The tutorials directed me to choose blank activity however not fragment_main.xml was included when creating the project.

It appears that eclipse and Android need to be properly updated to fix the lack of frangment_main.xml and activity_main.xml

I tried with the windows version and got the same result. I think I will just have to accept the fact that I will be creating my own fragment and main xml layouts

After speaking with a developer at Bitter, I decided it best I downloaded the Android studio. He suggested that the tutorial written online are already geared for the new Android Studio. I did the initial tutorial and I was able to get the app running on my tablet and virtual device.

Saturday August 08 2015.

Activities are the java component of an android app. The XML layout could be considered the swing aspect of the application. Viewgroups can be considered the containers and views can be considered the components. Like swing, the view or component has to call the activity.

Q: Where do we define the activity or in other words; how do we point to the java file that we are going to use?

A: The newActivity.java is declared in the AndroidManifest.xml as an <activity> element within the <application> element

Q: How do we activate the new Activity?

A: android:onClick=”sendMessage”/> call the sendMessage() method in the MainActivity.java file.

Q: How does it know to call the sendMessage() method in the MainActivity.java?

A: I’m guessing because MainActivity is set as the context in the LinearLayout element at the line tools:context=”com.example.firstproject.MainActivity”

 

Posted in Uncategorized | Leave a comment

Log4j2 – Basic Logging For Java Development – Eclipse

Log4j2_Example

The purpose of this exercise is to implement Log4j2 as a console logger and file logger for Java development.

1. Download apache-log4j-2.3-bin.zip

2. In Windows explorer create a /lib/ folder in you project parent folder if one does not exist, typically the same folder that contains your /src/ package folder.

3. In Windows explorer move log4j-api-2.3. and log4j-core-2.3 jar files from the zip into the /lib/ folder you created

4. Right click and refresh your project in Eclipse Package Explorer to make the /lib/ folder visible

5. Right click log4j-api-2.3 and log4j-core-2.3 in the /lib/ folder and select Build Path -> Add to Build Path

  • /lib/ is not a package folder, /src/ is a package folder

6. Create log4j2.xml in your project /src/ folder so it is part of your class path – this is your configuration file.

 <?xml version="1.0" encoding="UTF-8"?>
 <Configuration status="DEBUG">
 <Appenders>
 <Console name="Console" target="SYSTEM_OUT">
 <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
 </Console>
 <File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
 <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
 </File>
 </Appenders>
 <Loggers>
 <Root level="debug">
 <AppenderRef ref="Console" />
 <AppenderRef ref="MyFile"/>
 </Root>
 </Loggers>
 </Configuration>

7. Create a text file in your project parent directory and save it as all.log

For some unknown reason the logger didn’t work correctly the first time and even though the files were in the right place and the setting were correct. The logger couldn’t find the configuration file initially. I tried moving log4j2.xml to /src/main/resources/ with no success. Only after moving it back to /src/ did it finally kick in. I found this to be the same case when I implemented it for the first time in another project.

 

Posted in java, object oriented programming, Programming | 1 Comment

Objects First With Java 5E – Chapter 02 Notes

Elements of class definitions : fields, constructor, and methods.

The class header is top part of the outer wrapping of the class that essentially names the class ie

public class Car
    {
        class body and definitions
    }

Public class Car is considered the class header. Unlike methods there is no parameter list.

By convention we always start class names with a capital letter to help distinguish them from field and methods.

“public” and “class” are Java keywords or reserved words. Keywords are always lowercase.

The inner part of the class is where we define the field, constructor, and methods.

Fields provide a place to store long-lasting data (ie, persistent data). Fields store data persistently within an object.

Though data is persistent it can still change. Fields values can change often, hence the name variable.

The constructors are responsible for initializing an object correctly when it is first created.

Once the object has been created, the constructor plays no further role in that objects life.

In Java fields are automatically initialized to a default value if they are not explicitly initialized. Interger fields default is zero. Boolean default value is false. The default value of any object reference is null.

** Understand constructor / method space **

Is the constructor space is used to store parameters during the object creation? Does the constructor / method space take up space in memory? Am I overthinking this?

public Car(int topSpeed)

formal parameter is the name given to the parameter and actual parameter is the value supplied for the parameter.

The methods implement the behavior of an object; they provide it’s functionality.

There are two types of methods: accessor and mutator method. Alternatively get and set methods respectively.

Best practice define all fields at the start of the class definition.

For now we will always define fields as private.

Parameters are variables that are defined in the header of a constructor or method.

** Java does allow void methods to have a special return statement. This takes the form

return;

and simple causes the method to exit without executing any further code.

Exercise 2.8 Check whether or not it is possible to leave out the public from the outer wrapper of the TicketMachine class.

Exercise 2.8 It is possible to leave out public from the class and it appears to compile correctly.

Exercise 2.9 It in not possible to leave out the class from the outer wrapper of the class header. Class, enum or interface expected.

Exercise 2.11 What are two features of the constructor that make it look signifigantly different from the methods of the class.

Exercise 2.11 The two main features that make the constructor different from the methods of the class is that no return type is specified in the constructor and method names do not start with capital letters.

Exercise 2.22 The following obect creation will result in the constructor  of the Dat class being called. Can you wirte the constructor’s  header?

new Date("March", 23, 1861)

Exercise 2.22

public Date(String month, int day, int year)

Exercise 2.28 Compare the method headers of getPrice and printTicket. Apart from their names what is the main difference between them?

Exercise 2.28 getPrice has a return type of int while printTicket has a void return type.

Exersise 2.29 insertMoney and printTicket do not have return types becuase they are not accesor methods.

Exercise 2.33 score = score + points;

Exercise 2.34 The increase method is a mutator method. We can demonstrate this using an accessor method that returns the points field to view the changes that the increase method made.

Exercise 2.35 price = price – amount;

 

Posted in Programming | Leave a comment