0% found this document useful (0 votes)
34 views

Mobile Basics Java

The document provides information about layouts and managing resources in Android applications. It discusses the different types of layouts that can be used to arrange views, including linear, relative, frame, and table layouts. It also describes how to define and use different resource types like strings, styles, dimensions, colors, and drawables. Resources are reusable and help maintain consistency. The structure of an Android project is also outlined, including the main folders for Java code, resources, libraries, and the compiled build files.

Uploaded by

Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Mobile Basics Java

The document provides information about layouts and managing resources in Android applications. It discusses the different types of layouts that can be used to arrange views, including linear, relative, frame, and table layouts. It also describes how to define and use different resource types like strings, styles, dimensions, colors, and drawables. Resources are reusable and help maintain consistency. The structure of an Android project is also outlined, including the main folders for Java code, resources, libraries, and the compiled build files.

Uploaded by

Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Layouts

Sunday, 28 October 2018 11:19 AM

Layout stuff from screen

App -> Res -> layout ->Content_main

To change the code for positions of items - images, text etc, click on "text tab" - not design. It should
display the XML file for layout

Layout_marginEnd="0dp" //right side margin for item - 0 pixels - this can be changed
Layout_marginStart="0dp" // left side margin
Layout_marginTop="0dp" // top margin

• Layouts define the structure for a user interface in Android applications and app widgets
• XML is used to create a layout which is stored in the res/layout folder
• To add a new layout to the app, File -> New ->XML -> layoutXML
• The Root Tag Field shows the structure type for the layout. The following layout tags can be
used to arrange views:
○ Linear Layout - arrange views in single row with multiple columns or single column with
multiple rows. Each element comes either after or beside the previous element. This is
an easy layout type
○ Relative Layout - used to arrange views relative to another view's position. The layout is
a little harder to manage than the Linear Layout because it is the designer's task to
position elements relative to one another
○ Frame Layout - used as a placeholder layout for a single view. Makes it easy to arrange
a view's position in another layout. Video playback is a good Frame layout to use as it is
done in single view
○ Table Layout - creates table-like views with columns and rows
○ Grid Layout - multiple rows and columns. Good for things like photo thumbnails

layout stuff Page 1


Managing Resources
Friday, 2 November 2018 10:26 AM

• To make UI, you need to know how to use static resources , strings, color, definitions, style definitions, dimensional definitions and
drawables.
• Resources are the main building blocks for layouts. They are reusable and easy to manage, good for consistency

Using Strings
• Should add text elements as a string instead of just writing text to XML file or text property
• Create string resources in another XML file containing a list of elements.
• Example of XML file containing string content
<resources>
<string name="app_name">Chaprter5</string>
<string name="ok_button">OK</string>
<string name="next">Next</string>
</resources>
• To use a string resource in the UI layout, asign a property's text starting with @string in the XML file
"android:text="string/chapter_name"

• You can also do this using Properties window.

Using Styles
• Style resources including appearance of text, backgound color, text size, font, shadowing etc.
• Style resources are stored in the res/values/styles.xlm file. Some predefined are already in the file
• Example
<style name="NewStyle" parent="AppTheme">
<item name="android:textColor">#229933</item>
<item name="android:textSize">20sp</item>
<item name="android:padding">5sp</item>
</style>
• To apply a style to a view in the text mode of layout, add the style="@style/NewStyle" line or the style property to select
the NewStyle resource from the list.

Using Dimens
• Are used to change width, length, height, and margins of the assigned view in the layout for devices with different resolutions
• Using dimens gives flexibility to both design and layout for device sizes and to assign a dimen resource to change to a different
device size at runtime
• Dimen tags are stored in the res/values as per below which is the dimen.xml file generated right after the project was created.
• Example
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>
• To use the dimen resources right after the definition, you need to find the resource that is related with the dimensions of the view.
• Assignment of the dimen resource in the XML files is similar to assignment for strings and styles.
• For example to assign fab_margin to your checkbox, previously added, you need to define the layout:margin attribute and
assign the dimen with "@dimen/fab_margin", as below
<Checkbox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/chapter_name"
android:id="@+id/checkbox"
Style="@style/NewStyle"
android:layout_margin="@dimen/fab_margin" />

Using Colors
• Color resources are defined in the res/values/colors.xml file

Using Drawables
• Drawables are resources which contain graphics that can be drawn on screen, applied directly through the XML layout file with
android: drawable attribute
• You can add a color value as a drawable with android:drawable="@color/colorBlack"
• Can be used for bitmaps and nine patch states; state, level and layer lists; and transitions, clip, scale, shape and inset drawables.

layout stuff Page 2


• Can be used for bitmaps and nine patch states; state, level and layer lists; and transitions, clip, scale, shape and inset drawables.
• Drawables can be defined in XML format - example below - to draw a rectangle with rounded corners with gradient fill
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<gradient
android:endColor="#DD888888"
android:startColor="DD333333"
android:angle="45" />
</shape>
• Other drawables that can be declared in XML are vector, transition, state and animation drawables

layout stuff Page 3


Android Manifest File
Sunday, 28 October 2018 11:20 AM

• Manifest file is like an Android application's signature for the Android operation system. It
defines the starting activity and other activities the application includes in the package, such as
services, the applications name, the Android SDK version, and the required permissions
needed to access the services

Manifest Page 4
Activities
Sunday, 28 October 2018 1:10 PM

Screens are called activities

Android Activities
• Android activities have a lifecycle
• The following list describes the Activity lifecycle states with Activity methods
○ onCreate() - activities load the static user interface elements and allocate system resources
○ onStart() - activity enters the started state, it becomes visible and ready to handle user interaction
○ onResume() - all actions are performed , such as user input, drawing new user interface elements according to user input etc
○ onPause() - application taken to background or another one brought forward
○ onStop() - the application is totally invisible and in a sleeping state
○ onDestroy() - all processes are killed and all resources released
• Activities handle drawing the UI , user inputs and responses to user inputs - UI defined in layout XML file
• User defined activities derive from Activity superclass
• Other derived classes from Activity include ListActivity and AppCompactActivity'

Intent Event Handler


• Need an "intent" event handler to start an activity created by: Intent intent = new Intent();
• Intent is a data structure that holds the description of action to be performed such as start a new activity. Eg
○ Intent intent = new Intent(this, NextActivityClass.class);

Adding New activities


• When adding a new activity "NextActivity", it is named " public class NextActivity extents AppCompatActivity {
• The original activity is the main launcher activity. You can set an intent in that activity to launch the new one.
• 2 XML files are created - one for UI elements (toolbars etc) and one for the blank area
○ activity_next.xml defines the main layout UI elements, including toolbar widget (AppBarLayout) and button
widget(FloatingActionButton)
○ content_next.xml defines the UI elements in the blank area of the activity
• All the elements are defined in the XML file inside another UI component called CoordinatorLayout.content_next.xml

Activities Page 5
Project Structure
Wednesday, 31 October 2018 10:18 AM

Project structure

• build/: a folder that contains the compiled resources after building a java application and
the classes generated by the android tools such as R.java file, which contains the
references to the application resources.

• libs/: A folder that contains the libraries referenced from our code.

• src/main/: a folder that contains the sources for your application. All the files you will
usually work with are in this folder. The main folder is subdivided as follows:

• java/: a folder that contains the Java classes organised as packages. Every class
we create will be in our project package namespace
(com.example.myapplication). When we created our first project we also created
its main activity so the activity class should also be in this package (src-> main ->
java -> MainActivity

• res/: a folder that contains project resources such as the XML files that specify the
layouts and menus
• drawable/: a folder that contains the images used in our application. There are
different drawable folders catagorised into the different screen densities.
• layout/: a folder that contains the XML definitions of the views and their elements.
• menu/: a folder that contains the XML definitions of the menus of the application
• values/: a folder that contains the XML files that define sets of name-value pairs.
These values can be colours, strings, or styles. There are different values folders
categorised into different screens options to adapt the interface to them. For example,
to enlarge the components or the fonts when the application is running on a tablet
• AndroidManifest.xml: this file is essential in an Android project and is generated
automatically when we create the project. This file declares basic information needed
by the Android system to run the application, package name, version, activities,
permissions, intents, or required hardware
• build.gradle: This file is the script used to build our application.

Proj Structure Page 6


Fragments
Wednesday, 31 October 2018 10:33 AM

• Fragments are like sub activities: they handle partial UI operations


• Using fragments decreases the number of activities used inside the application and provides a smooth transition between UI el ements
• Using fragments simply divides the UI tasks defined for activities and allow developers to design a more compact application without
the need to launch a large number of activities, which decreases the application stack
• Useful on big screen apps, as you make use of each part of the application without blocking the activity thread that handles the UI

Understanding the Fragment Lifecycle

• Fragments are like activities within activities so they have similar lifecycle with additional bindings to a root activity they run in.
• Lifecycle states can include:
○ onAttach - The fragment's associations with the root activity
○ onCreateView - Creates and returns the view hierarchy associated with the fragment
○ onActivityCreated - Method called when the activity is ready after the onCreate state
○ onDestroyView - Method called when the fragment frees resources and views
○ onDetach - Method called when the fragment is no longer associated with the activity
• Tabbed activity template includes fragments

Fragments and Services Page 7


Services
Thursday, 1 November 2018 1:05 PM

• Services can be defined as activities without a UI that run in the background to perform long running
tasks.
• Music player is an example - ie can run in the background while you do other things
• Android studio provides 2 basic service templates for developers
○ Standard Android Services
○ Intent Service
• Services usually handle tasks within the applications main thread - designed to be short running,
however , IntentServices handles long running tasks - they need to be designed as a separate
thread than the main application thread

Fragments and Services Page 8


Assets
Wednesday, 31 October 2018 11:37 AM

• Includes icons, pics etc


• Each asset type should be added to its own directory.
• All assets are stored in the res folder
• Switch to the traditional Project View to better see the res folder content

Assets Page 9
Images
Thursday, 1 November 2018 1:19 PM

• Image resources should be stored in the mipmap-xx folders


• If your application runs on multiple devices with different screen resolutions, you should add
images to the corresponding resolution folder in the project directory
○ mipmap-hdpi - high density images
○ mipmap-mdpi - medium density images
○ mipmap-xhdpi - Extra high density images
○ mipmap-xxhdpi - Extra Extra high density images
○ mipmap-xxxhdpi - Extra extra extra high density images

Assets Page 10
Sound
Thursday, 1 November 2018 1:44 PM

• Sound assets are placed in the raw directory - it is not auto generated by Android studio
• You need to make a subdirectory in the res folder called raw - you may drag and drop or paste to the Project View

Assets Page 11
Video
Thursday, 1 November 2018 1:53 PM

• Videos are stored in the raw folder like the audio

Assets Page 12

You might also like