Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Kenneth Mix
NoteSet
2016
>> ECLIPSE ANDROID
ECLIPSE_Android_NoteSet_2016
_________________________________________________________________________________________________
Android Development Notes and Vocabulary 2015
Android
NoteSet >>>> Android_000
Open Eclipse
File
New
Android Applicat ion Project
Applicat ion Name :
Package Name :
Activit y Name :
Hello1
z. x.c.hello1
NEXT
activit y_main
FINISH
NEXT
NEXT
NEXT
Eclipse Envir onment Opens!
Close :
res / layout / fragment.xml
Use X in Eclipse
Open activit y_main [double -click]
GO TO Hello1.java :
The code, for MainActivty.java, should look like the following :
package z. x.c.hello1;
import andr oid.app. Activit y
import andr oid.os.Bundle;
public class MainAct ivit y extends Acti vit y
{
@Override
protected void onCreate(Bundle s)
{
super.onCreat e( s);
setCont entView(R. layout. activity_main );
}
}
1
Change UI layout :
GO TO UI ==> Select activity_main.xml
GO TO ==>
Graphical Layout
[right-click]
Change Layout
Linear_Layout (Vertical)
OK
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
The Virtual Device / Emulator should open!
If there are syntax errors, the Virtual Device will NOT open.
This will test both the HelloWorld Application and the Virtual Device.
GO TO :
activity_main.xml
Graphical Layout
[click-n-drag] Large Text Box into the UI.
Open ==> activity_main.xml
<text_view>
code : change Android:text="Large Text" to
Android:Text="your name"
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
The Virtual Device / Emulator should open!
You should see your name in the UI.
2
Android Development Notes and Vocabulary 2015
Android Hello World
NoteSet >>>> Android_001
Open Eclipse
File
New
Android Applicat ion Project
Applicat ion Name :
Package Name :
Activit y Name :
Hello1
z. x.c.hello1
NEXT
activit y_main
FINISH
NEXT
NEXT
NEXT
Eclipse Envir onment Opens!
Close :
res / layout / fragment.xml
Use X in Eclipse
Open activit y_main [double -click]
GO TO Hello1.java :
The code, for MainActivty.java, should look like the following :
package z. x.c.hello1;
import andr oid.app. Activit y
import andr oid.os.Bundle;
public class MainAct ivit y extends Acti vit y
{
@Override
protected void onCreate(Bundle s)
{
super.onCreat e( s);
setCont entView(R. layout. activity_main );
}
}
Change UI layout :
GO TO UI ==> Select activity_main.xml
GO TO ==>
Graphical Layout
[right-click]
Change Layout
Linear_Layout (Vertical)
OK
3
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
The Virtual Device / Emulator should open!
If there are syntax errors, the Virtual Device will NOT open.
This will test both the HelloWorld Application and the Virtual Device.
GO TO :
activity_main.xml
Graphical Layout
[click-n-drag] Large Text Box into the UI.
Open ==> activity_main.xml
<text_view>
code : change Android:text="Large Text" to
Android:Text="your name"
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
The Virtual Device / Emulator should open!
You should see your name in the UI.
4
Android Development Notes and Vocabulary 2015
Android Background Color
NoteSet >>>> Android_001a
Open Eclipse
File
New
Android Application Project
Application Name :
Bcolor
Package Name :
z.x.c.buttcolor
NEXT
NEXT
Activity Name :
activity_main
FINISH
NEXT
NEXT
Eclipse Environment Opens!
Close :
res / layout / fragment.xml
Use X in Eclipse
Open activity_main [double -click]
GO TO Bcolor.java :
Code, at this time, should look like this :
package z.x.c.bcolor;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate( s );
setContentView(R.layout.activity_main);
}
}
Change UI layout :
GO TO UI ==> Select activity_main.xml
GO TO ==>
Graphical Layout
[right-click]
Change Layout
Linear_Layout (Vertical)
OK
5
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application **
OK
** Save Resources, if necessary.
The Virtual Device / Emulator should open!
If there are syntax errors, the Virtual Device wiill NOT open.
Insert a new Button in the UI :
GO TO --
activity_main.xml
Graphical Layout
Form Widgets (in the Palette)
[click-n-drag] Button to the UI
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
Set-up Button :
GO TO :
activity_main.xml (top of Eclipse)
activity_main.xml (view - bottom of Eclipse)
Code :
android:id="@+id/b1"
android:text="BLUE"
6
Connect the Button :
GO TO --
MainActivity.java
Code : Button b1 = (Button)findViewById(R.id.b1);
[Hover over "Button"]
Insert == import android.widget.Button;
Code : b1setOnClickListener(new OnClickListener()
{
}
[Hover over "OnClickListner"]
Insert ==
import android.view.View.OnClickListener;
[Hover over "OnClickListner"]
[click] Add unimplemented methods **
**
This will add the shell code for the onClick( ) method
Do Not add it manually!
Code :
add ");" next to the last "}" == This closes the
setOnClickListener
DO NOT close the entire class !
Connect the .xml to the .java :
GO TO -Code :
MainActivity.java
final LinearLayout LL =
LinearLayout(findViewById(R.id.LinearLayout);
[Hover over "LinearLayout"]
Insert == import android.widget.LinearLayout;
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK
7
Make the Button change the UI Background color :
In the onClick( ) method :
Code : LL.setBackgroundColor(Color.BLUE);
[Hover over "Color"]
Insert == import android.graphics.Color;
TEST The Program :
Select
Main-Activity.java
Run
Run
Android Application
OK **
**
The background color should change !
8
Android Development Notes and Vocabulary 2015
Hello World Vocabulary
NoteSet >>>> Android_001v
Android Platform Processor Operating System BIOS Linux Linux Kernel JDK Java Eclipse -
Java File Activity Intent Layout View -
Google open-source and Java based platform for mobile development.
Processor and Operating System
Circuits that process the basic computer instructions
OS - manages the operations of a computer
Basic Input / Output System - Used at starts up to load the OS.
An OS (subset of UNIX)
Free and Open-Source
Java Development Kit
An application programming language
Development environment | IDE - Integrated Development Environment
(used for compiling, debugging and testing)
It is a self-contained executable file
Software Development Kit (used to download all other SDK components.)
Android Development Tools | Plug-in - added files for to Eclipse
Tells Eclipse where to store project files
Android Virtual Device - represents a specific device (for PC / Android development)
Android Virtual Device Manager - set-up and define virtual devices
Android SDK Manager - Used to install Android Version Packages
Creates the required files for an Android Application
Defines the UI ||
stored in res/layout file
User Interface - The screen where the user communicates with the program.
The connection between the app and the user!
A UI is a set of View Objects.
.java file that defines the UI controls || Package/src file
Represents 1 Android Screen (Window), and each additional screen.
Opens a new Activity and processes user actions (Transition from 1 Activity to another.)
A container - holds and arranges screen controls
Screen entities placed in a layout container known as a ViewGroup.
Virtual Device -
Not an actual device. Known as an emulator.
Android Life Cycle AndroidManifest -
The states that an Activity goes through.
An XML file that defines overall structure and other information about an application.
SDK ADT Workplace Launcher AVD Android VDM Android SDK Manager Android Project Wizard XML File UI -
Activity Code :
import super.onCreate(s) setCOntentView(R.layout.activity_main) onCreateOptionsMenu ( ) -
Imports necessary class files.
Invoked when activity is started.
Defines the screen of the Activity.
Used for menus (Not used for basic applications.)
9
Your first project Code:
Project Name : Helloiii
iii = your initials
lower case - except first letter
MainActivity.java
package z.x.c.layouttest;
import android.app.Activity;
import android.os.Bundle;
This is what your .java should look like!!!
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate( s );
setContentView(R.layout.activity_main);
}
}
The onCreate( ) method is the only method
implemented in this application! onCreate( ) is
part of the Application Life Cycle.
This statement is the connection to the UI.
activity_main is the .xml file containing the
UI code.
Android Project Folder Structure :
src/ -
res/ -
Contains java source code
src/z.x.c.Projectname/ -
.java code
Contains resources, layout .xml, images, strings, etc.
res/lauout/activity_main.xml (No Upper Case)
Resources are anything that is not java code.
gen/ - Auto-generated. R.java
AndroidManifest/ -
(Do NOT edit)
Essential information about the application.
Activities, Permissions
Holds the Android API level.
android:minSDKVersion -
The minimum API level required by your application
(Lower versions will not install on device.)
android:targetSDKVersion -
The version that you are building for.
Defines features of your application.
android:maxSDKVersion -
The maximum SDK that your application will support.
Prevents installation on newer devices not able to run.
10
Android Development Notes and Vocabulary 2015
Layouts
NoteSet >>>> Android_002
To change the default layout :
Select - activity_main.xml
Graphical Layout
[right-click] Change Layout
New Layout Type ==> LinearLayout (Vertical)
Verify the change in the .xml source code !
Home Screen :
The first screen a user sees.
Allows user to start an application.
Notification Tray -
Lists notifications (e.g. - new mail, notifications, currently playing music, system status, etc.)
.java connection to a UI :
setContentView(R.layout.activity_main);
Name of the .xml UI file.
(acitivty_main)
Add a New View :
Select - activity_main.xml
Graphical Layout
Palette
Form Widgets
[click-n-drag)
TextView onto screen.
Resulting in a new TextView View being placed on the screen.
A Layout is known as a container. A view is an object placed into a screen that is
used to display text or handle user actions. Views are placed into a container.
11
Current Layout code :
<LinearLayout xmlns:android =
xmlns:tools=
android:id=
android:layout_width=
android:layout_height=
android:orientation=
"http://schemas.android.com/apk/res/android"
"http://schemas.android.com/tools"
"@+id/container"
"match_parent"
"match_parent"
"vertical"
>
<TextView
android:id=
"@+id/textView1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"TextView"
/>
</LinearLayout>
The TextView View displays text for the user to see.
Each element in the layout is called a "View".
A View is anything that can be drawn on the screen.
Activities are short-lived. This means that they exist for a short period of time. When the use rotates their device, the
Activity is destroyed and re-created in a new position.
12
Basic Eclipse UI Development Environment ==> Graphical Layout Editor :
6&7
1
4
3
2
5
1234567-
Configuration drop-down.
Lets you change the way the layout is viewed.
The Canvas The current layout --or -- the .xml source code.
The Palette The basic Objects for a screen.
The Outline Displays an overview of the layout
Tabs to switch between Graphical Layout and .xml
Run button To run an application (Must be pointing to .java file)
Windows For SDK Manager and Virtual Device Manager
13
To change the text/literal in a TextView View :
Select activity_main.xml
Select activity_main.xml code!
<TextView
android:id=
"@+id/textView1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
" New Text for user ! " />
This is where you can change the text literal.
View :
Every View is required to have a height and width.
Dimensions :
exact size DP - eg = 48px will be exactly 48 pixels.
wrap_content - View takes only enough space as needed
match_parent - View will expand to fill entire parent view.
MarginPadding -
Create space on outside of a view
Creates space inside of a view.
Gravity -
Controls position of a view within a container. (To right justify a view.)
android:layout_gravity = "right"
Table Layout :
Insert a Table :
Select activity_main.xml
Select Graphical Layout
Palette
Layouts
[click-n-drag] Table Layout ==> to screen
By default, 4 rows are inserted into the table.
To delete one row :
Select activity_main.xml
Select <Table Row.... to </tableRow>
Delect
This will delete 1 row.
14
To Insert view(s) into table rows :
Select Graphical Layout
Palette
Form Widgets
[click-n-drag] Button into screen
Do this two additional times.
This should insert 3 New Buttons into the top row of the table.
Form Widgets
[click-n-drag] Medium into screen
Do this two additional times.
This should insert 3 New Mediums into the top row of the table.
_________________________________________________________________________________________________
ViewGroups :
Linear Layout -
A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar
if the length of the window exceeds the length of the screen.
Relative Layout -
Enables you to specify the location of child objects relative to each other (child A to the left of
child B) or to the parent (aligned to the top of the parent).
Absolute Layout -
Each view given specific position. It is Depreciated.
Table Layout -
Rows and columns
Grid Layout -
Views are arranged in grid format (rows and columns).
Frame Layout -
Displays 1 View. The next View replaces the previous View and thus, the layout is changed.
_________________________________________________________________________________________________
Common Views :
TextView EditText ListView Spinner Button CheckBox RadioButton -
Read-only. Places a string in screen.
An editable text box.
A ViewGroup that creates and manages a vertical list of views
A TextView and a related listofitems for the user to select.
Standard Button.
Standard Checkbox
Standard Radiobutton
_________________________________________________________________________________________________
15
Common Attributes for views :
android:width
setWidth( )
android:width = "125dp"
android:height
setHeight( )
android:height = "200dp"
android:textColor
setTextColor( )
android:textColor = "#F4F"
android:textSize
setTextSize( )
android:textSize = "25dp"
android:ellipsize
setEllipsize( )
android:ellipsize = "end"
android:singleLine
etTransformationMethod( )
android:singleLine = "true"
android:background
setBackgroundResearch( )
android:background = "#00FF00"
android:textStyle
setTypeface( )
android:textStyle = "italic"
android:typeface
setTypeface( )
android:typeface = "serif"
_________________________________________________________________________________________________
LinearLayout :
android:orientation android:layout_width -
android:layout_height android:padding -
android:Layout weight -
horizontal or vertical
The width of control
specific dimension ==> 25dp
wrap_content - this resizes a control to expand to fit the contents.
(TextView - all text is visible)
match_parent - forces size of a control to expand and fill up all available
space ofa container.
The height of control
The whitespace between boundaries of the control and the actual content.
paddingLeft
paddingRight
paddingTop
padding Bottom
android:padding="20dp" ==> all 4 sides are 25dp.
Used to change size of the control to use extra space relative to other controls
in a container.
android:gravity Used to align content in a control.
android:layout_gravity - TO align the control within a container.
16
Android Development Notes and Vocabulary 2015
Android Activity Life Cycle
NoteSet >>>> Android_003
Android Activity Life Cycle : The events that an activity goes through from start to end.
onCreate( ) onStart( ) onResume( ) onPause( ) onStop( ) onDestroy( ) -
Called when Activity is first created.
Called just before Activity becomes visible on the screen.
Called when Activity becomes the foreground Activity.
Called when the Activity is stopped or no longer visible.
Called when Activity is no longer visible.
Used to release resources back to the system. May not be called.
onRestart( ) _________________________________________________________________________________________________
Event Handling :
Event -
Something that happens.
Situation : You want to have user enter data !
The reaction to handling an event is called event handling.
To handle events, you use a listener. Listeners wait for an event to occur. Once an event occurs, the Listener
detects it then instructs the program to handle the event.
The event listener is an interface in the View class. It contains 1 callback method called an event occurrence.
The onClick(View v ) method is called when a user clicks a Button.
setOnClickListener method
OnClickListener interface
17
Declaring the Event Handler (Handling a Button click, for example.) :
activity_main.xml file ( partial code )
<Button
android:id = "@+id/b1 "
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:text = "Click Here"
androi d:onClick = "dis playmessage" />
18
Android Development Notes and Vocabulary 2015
Android User Input
NoteSet >>>> Android_004
Application : Convert User Input to Uppercase !
Vocabulary :
User Input -
User Input is one of the most important aspects of programming. Most programs have some sort of
user interaction, from getting a character's name for a game to asking for a password to log into a
database.
_________________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Text = "Convert User Input to All uppercase"
Insert a new EditText
Select activity_main.xml
Graphical Layout
Palette
Text Fields
[click-n-drag]
Plain Text = identified as "abc" to screen
Change name :
Select activity_main.xml
Locate EditText tag
<EditText
android:id="@+id/et1"
==> make change as shown
This gives this EditText field a name to be used as a reference.
This will be for User Input
Insert a new TextView
Select activity_main.xml
Graphical Layout
Palette
Form Widgets
[click-n-drag] TextView to screen
Change name :
Select activity_main.xml
Locate TextView tag
<TextView
android:id="@+id/tv1" ==> make change as shown
This gives this TextView field a name to be used as a reference.
This will be for the result.
19
_________________________________________________________________________________________________
Insert a new Button :
Code :
<Button
android:id=
"@+id/b1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Convert to Upper" />
Insert a new Button :
Code :
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
________________________________________________________________________________________________
Java Code to this point in time:
package z.x.c.userinput1;
Comments - created with //
// This is a comment line !
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
}
}
20
activity_main.xml Code to this point in time:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="z.x.c.userinput1.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert User Input to all uppercase" />
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert to Upper" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
_________________________________________________________________________________________________
21
The Java Programming logic is to be inserted next !
'Exit' Button Code : Note: the Exit Button has a name of "b2".
Connect the button to
the .java code.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b2 = (Button)findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener( )
{
@Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
.java code to handle
the 'exit' process.
}
}
Note - Do not forget to insert following statements as necessary :
import
import
import
import
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
22
'Convert to Upper' Button Code : Note: this Button has a name of "b1".
This code will return the result to BOTH the EditText and TextView views.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
Button b2 = (Button)findViewById(R.id.b2);
final EditText et1 = (EditText)findViewById(R.id.et1);
final TextView tv1 = (TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener()
{
@Override
Declare a variable to hold the string
public void onClick(View v)
entered.
{
String s1 = new String();
s1 =
et1.getText().toString();
2 statements to convert the input to
s1 =
s1.toUpperCase();
the new value.
et1.setText(s1);
tv1.setText(s1);
Send the output to both output
}
});
views!
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
}
}
_________________________________________________________________________________________________
23
Android Development Notes and Vocabulary 2015
Android User Input 1.1
NoteSet >>>> Android_004_1
Application : Convert - First Character - of User Input to Uppercase !
Vocabulary :
append -
add onto, add onto existing data
_________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Text = "Convert User Input to All uppercase"
Insert a new EditText
Select activity_main.xml
Graphical Layout
Palette
Text Fields
[click-n-drag]
Plain Text = identified as "abc" to screen
Change name :
Select activity_main.xml
Locate EditText tag
<EditText
android:id="@+id/et1"
==> make change as shown
This gives this EditText field a name to be used as a reference.
This will be for User Input
Insert a new TextView
Select activity_main.xml
Graphical Layout
Palette
Form Widgets
[click-n-drag]
TextView to screen
Change name :
Select activity_main.xml
Locate TextView tag
<TextView
android:id="@+id/tv1"
==> make change as shown
This gives this TextView field a name to be used as a reference.
This will be for the result.
24
_________________________________________________________________________________________________
Insert a new Button :
Code :
<Button
android:id=
"@+id/b1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Convert to Upper" />
Insert a new Button :
Code :
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
________________________________________________________________________________________________
Java Code to this point in time:
package z.x.c.userinput1;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
}
}
25
activity_main.xml Code to this point in time:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="z.x.c.userinput1.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert User Input to all uppercase" />
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert First Character to Upper" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
_________________________________________________________________________________________________
26
The Java Programming logic is to be inserted next !
'Exit' Button Code : Note: the Exit Button has a name of "b2".
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b2 = (Button)findViewById(R.id.b2);
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
}
}
Note - Do not forget to insert following statements as necessary :
import
import
import
import
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
27
'Convert to Upper' Button Code : Note: this Button has a name of "b1".
This code will return the result to BOTH the EditText and TextView views.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
Button b2 = (Button)findViewById(R.id.b2);
Connect the button to
the .java code.
Button b2 = (Button)findViewById(R.id.b2);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s1 = new String();
s1 = et1.getText().toString();
s1 = s1.substring(0, 1).toUpperCase() + s1.substring(1);
et1.setText(s1);
tv1.setText(s1);
}
});
substring(start position, length)
+ substring(start position to add)
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
}
}
_________________________________________________________________________________________________
28
Android Development Notes and Vocabulary 2015
Android User Input 1.2
NoteSet >>>> Android_004_2
Application : subString( ) Method
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Text = "Convert User Input to All uppercase"
Insert a new EditText
Select activity_main.xml
Graphical Layout
Palette
Text Fields
[click-n-drag]
Plain Text = identified as "abc" to screen
Change name :
Select activity_main.xml
Locate EditText tag
<EditText
android:id="@+id/et1"
==> make change as shown
This gives this EditText field a name to be used as a reference.
This will be for User Input
Insert a new TextView
Select activity_main.xml
Graphical Layout
Palette
Form Widgets
[click-n-drag]
TextView to screen
Change name :
Select activity_main.xml
Locate TextView tag
<TextView
android:id="@+id/tv1"
==> make change as shown
This gives this TextView field a name to be used as a reference.
This will be for the result.
29
_________________________________________________________________________________________________
Insert a new Button :
Code :
<Button
android:id=
"@+id/b1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Convert to Upper" />
Insert a new Button :
Code :
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
________________________________________________________________________________________________
Java Code to this point in time:
package z.x.c.userinput1;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
}
}
The substring( ) method:
Substring (X, Y);
(S, N)
X ==>
Y ==>
position SUBSCRIPT - start and include
position NUMBER - start and include
_________________________________________________________________________________________________
30
Sample Results :
q w e r t y u i o p
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
==>
==>
==>
setstring(2, 8) ==>
ertyui
setstring(1, 4) ==>
wer
setstring(2, 6) + setstring(0, 6); ==>
User Input
SUBSCRIPT
NUMBER
ertyqwerty
s1 = s1.substring(0,1).toUpperCase() + s1.substring(2,6) + s1.substring(0,6); ==>
** Note -
Qertyqwerty
Clear out the User Input firld before reprocessing!
______________________________________________________________________________________________
31
.java code :
package z.x.c.userinput1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
Button b2 = (Button)findViewById(R.id.b2);
Button b3 = (Button)findViewById(R.id.b3);
final EditText et1 = (EditText)findViewById(R.id.et1);
final TextView tv1 = (TextView)findViewById(R.id.tv1);
Test the user input.
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s1 = new String();
String s2 = new String();
String s3 = new String();
s3 =
"q";
s1 =
et1.getText( ).toString( );
s2 =
s1.substring(0,1);
if (s2.equals(s3))
{
s1 = s2;
}
else
{
s1 = s1.substring(0,1).toUpperCase() + s1.substring(2,6) + s1.substring(0,6);
}
et1.setText(s1);
tv1.setText(s1);
}
});
32
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
b3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
tv1.setText(" ");
et1.setText(" ");
}
});
Reset Button code. This
moves insertion point to
position 1 in the input field.
Any literal within the quotes
will be set in the input field.
}
}
________________________________________________________________________________________________
33
Sample code to test a field :
Test for a lower case "q" in the first (left) position. If TRUE, then print "q" else pring the entire string with the
first position converted to upper case.
.java code :
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
Button b2 = (Button)findViewById(R.id.b2);
Button b3 = (Button)findViewById(R.id.b3);
final EditText et1 = (EditText)findViewById(R.id.et1);
final TextView tv1 = (TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s1 = new String();
String s2 = new String();
Declare a String variable (s3)
String s3 = new String();
and initialize it to a "q"
s3 = "q";
s1 = et1.getText().toString();
s2 = s1.substring(0,1);
Convert position 1 to upper case.
Then, concatenate the rest of the
string to the upper case letter.
if(s2.equals( s3 ))
{
s1 = s2;
}
else
{
s1 = s1.substring(0,1).toUpperCase() + s1.substring(1);
}
Print only a "q" if TRUE.
Print entire string with first
position converted to
upper case.
et1.setText(s1);
tv1.setText(s1);
}
});
Send the output to 2 output
views !
34
Android Development Notes and Vocabulary 2015
Android User Input 1.3
NoteSet >>>> Android_004_3
Application : Numeric Data
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Name =
Text =
tb 1
"Numeric Data"
Insert a New Plain Text Box
Name =
et1
Insert a New Button
Name =
Text =
b1
"Do It"
Insert a New TextView
Name =
Text =
tv2
"Numeric !"
Insert a New Button
Name =
Text =
b2
"E X I T"
35
xml Code to this point in time :
<LinearLayout xmlns:android=
xmlns:tools=
android:id=
android:layout_width=
android:layout_height=
android:orientation=
tools:context=
tools:ignore=
"http://schemas.android.com/apk/res/android"
"http://schemas.android.com/tools"
"@+id/container"
"match_parent"
"match_parent"
"vertical"
"z.x.c.userinput2.MainActivity"
"MergeRootFrame" >
<TextView
android:id=
android:layout_width=
android:layout_height=
android:text=
"@+id/tb1"
"wrap_content"
"wrap_content"
"Numeric Data" />
<EditText
android:id=
android:layout_width=
android:layout_height=
android:ems=
"@+id/et1"
"match_parent"
"wrap_content"
"10" >
<requestFocus />
</EditText>
<Button
android:id=
android:layout_width=
android:layout_height=
android:text=
<TextView
android:id=
android:layout_width=
android:layout_height=
android:text=
android:textAppearance=
"@+id/b1"
"wrap_content"
"wrap_content"
"Do It" />
"@+id/tb2"
"wrap_content"
"wrap_content"
"Numeric !"
"?android:attr/textAppearanceLarge" />
<Button
android:id=
android:layout_width=
android:layout_height=
android:text=
"@+id/b2"
"wrap_content"
"wrap_content"
"E X I T" />
</LinearLayout>
36
Java Code to this point in time:
package z.x.c.userinput2;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
}
}
________________________________________________________________________________________________
Control User Input :
To allow User to only enter numeric data :
<EditText
android:id=
android:layout_width=
android:layout_height=
"@+id/et1"
"match_parent"
"wrap_content"
android:inputType=
"numberDecimal"
android:ems=
"10" >
_________________________________________________________________________________________________
Set the focus / Insertion point :
Sample placed in a Button [b3] b3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
et1.setText(" ");
et1.requestFocus();
}
});
Note - multiple fields can be reset to blank spaces, as shown.
This clears out the field.
This sends the insertion point to
the left side of the view field.
But, only one field can have the focus!
37
Code to accept User Input (et1) and return exact result to a TextBox(tv2) :
protected void onCreate(Bundle ss)
{
super.onCreate(ss);
setContentView(R.layout.activity_main);
final EditText
Button
Button
final TextView
et1 = (EditText)findViewById(R.id.et1);
b1 = (Button)findViewById(R.id.b1);
b2 = (Button)findViewById(R.id.b2);
tv2 = (TextView)findViewById(R.id.tv2);
b1.setOnClickListener(new OnClickListener()
{
@Override
Declare variables for User
Input.
public void onClick(View v)
{
String s, t;
double d1;
s = et1.getText().toString();
d1 = Double.valueOf(s).doubleValue();
t = Double.toString(d1);
tv2.setText( t );
}
});
}
Get the User Input.
Convert the input
string to data-type
Double.
Assign the double
value (d1) back to
string data-type.
Print the result on
the screen.
_________________________________________________________________________________________________
38
Android Development Notes and Vocabulary 2015
Android User Input - Part 2 - 1.4
NoteSet >>>> Android_004_4
Application : Numeric Data Process User Input :
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Name =
Text =
tb 1
"Numeric Data"
Insert a New Plain Text Box
Name =
et1
Insert a New Button
Name =
Text =
b1
"Do It"
Insert a New TextView
Name =
Text =
tv2
"Numeric !"
Insert a New Button
Name =
Text =
b2
"E X I T"
Basic Process :
123456-
Get user input
Convert input to Double
Calculate result
Convert result to string
Format result (if necessary)
produce output
39
Code to add 12 to User Input :
public void onClick(View v)
{
String s, t;
double d1;
s = et1.getText( ).toString( );
d1 = Double.valueOf(s).doubleValue( );
d1 = d1 + 12;
t = Double.toString(d1);
tv2.setText(t);
}
Add 12 to the value of
the User Input.
_________________________________________________________________________________________________
Code to add 12 to values greater than 15, else just print a zero :
public void onClick(View v)
{
String s, t;
double d1;
s = et1.getText( ).toString( );
d1 = Double.valueOf(s).doubleValue( );
if(d1 > 15)
{
d1 = d1 + 12;
}
else
{
d1 = 0;
}
TRUE result
Add 12 .
FALSE result.
Just print a zero.
t = Double.toString(d1);
tv2.setText(t);
}
_________________________________________________________________________________________________
Code to add 5% if User Input > 15 else, add 7% to User Input :
if(d1 > 15)
{
d1 = d1 * 1.05;
}
else
{
Adds either 5% or 7%.
d1 = d1 * 1.07;
}
40
Android Development Notes and Vocabulary 2015
Android User Input - Part 2 - 1.4
NoteSet >>>> Android_004_4.1
Application : Numeric Data Process User Input :
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Basic Process :
123456-
Get user input
Convert input to Double
Calculate result
Convert result to string
Format result (if necessary)
produce output
_________________________________________________________________________________________________
41
Code to handle 1 input box -- NUMERIC :
package z.x.c.testnumio;
import
import
import
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.view.View;
android.view.View.OnClickL istener;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle sa)
{
super.onCreate(sa);
setContentView(R.layout.activ ity_main);
Button b1 =
final EditText e1 =
final TextView t1 =
(Button)findVi ewById(R.id.b1);
(EditText)fi ndViewById(R.id.e1);
(TextVi ew)findViewById(R.id.t1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s, t;
doubl e x;
s = e1.getText( ).toString( );
x = Doubl e.valueOf(s).doubleVal ue( );
x = x + 5;
t = Double.toString(x);
t1.setText(t);
}
});
}
}
42
.xml file :
<LinearLayout xmlns:android="http://schem as.android.com/apk/res/android"
xmlns:tools="http://schemas.andro id.com/tools"
android: id="@+id/container"
android: layout_width="match_parent"
android: layout_height="match_parent"
android:orientation="vertical "
tools:context="z.x.c.testnumio.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id=
android:layout_width=
android:layout_height=
android:text=
"@+id/t2"
"wrap_content"
"wrap_content"
"Add 5 to input v alue :" />
<EditText
android:id=
android:layout_width=
android:layout_height=
android:ems=
"@+id/e1"
"match_parent"
"wrap_content"
"10" >
<requestFocus />
</EditText>
<TextView
android:id=
android:layout_width=
android:layout_height=
android:text=
"@+id/t1"
"wrap_content"
"wrap_content"
"TextView" />
<Button
android:id=
android:layout_width=
android:layout_height=
android:text=
"@+id/ b1"
"wrap_content"
"wrap_content"
"ADD 5" />
</LinearLayout>
43
Code to handle two editText views :
public void onClick(View v)
{
String s, t, u;
double x, y, z;
s = e1.getText().toString();
x = Double.valueOf(s).doubleValue();
t = e2.getText().toString();
y = Double.valueOf(t).doubleValue();
z = x + y;
u = Double.toString(z);
t1.setText(u);
}
44
Android Development Notes and Vocabulary 2015
Android User Input - Part 2 - 1.5
NoteSet >>>> Android_004_5
Application : Numeric Date Edit Input / Sample Future Value of Money / Currency format :
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Insert a TextView
Name =
Text =
tb 1
"Numeric Data"
Insert a New Plain Text Box
Name =
et1
For the Present Value
Insert a New Plain Text Box
Name =
et2
For the Interest Rate
Insert a New Plain Text Box
Name =
et3
For the number of years
Insert a New Button
Name =
Text =
b1
"Do It"
Insert a New TextView
Name =
Text =
tv2
"Future Value !"
Insert a New Button
Name =
Text =
b2
"E X I T"
_________________________________________________________________________________________________
45
Code to prevent run-time errors for no-data entered condition :
public void onClick(View v)
{
String s;
String t;
double d1, d2, d3, d4;
s = et1.getText( ).toString();
This code traps a TextView view
that is empty.
try
{
d4 = Double.parseDouble(s.toString( ));
}
catch(NumberFormatException e)
{
d4 = 0;
}
d2 = d4;
t = Double.toString(d2);
tv2.setText(t);
}
Code to find the power of a number :
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s, t;
double d1, d2, d3;
s = et1.getText().toString();
d1 = Double.valueOf(s).doubleValue();
d2 = 2;
d3 = Math.pow(d1, d2);
t = Double.toString(d3);
tv2.setText(t);
}
});
d3 is assigned the value of x to the
power of y. ==> (x, y)
(Additional variables d2 and d3
are used!)
________________________________________________________________________________________________
46
Code for Future Value of money :
xm l file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="z.x.c.userinput2.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id=
"@+id/tv1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Numeric Data" />
<EditText
android:id=
android:layout_width=
android:layout_height=
android:inputType=
android:ems=
android:text=
"@+id/et1"
"match_parent"
"wrap_content"
"numberDecimal"
"10"
"0" >
<requestFocus />
<requestFocus> will set the
insertion point at this position
when program starts.
</EditText>
<EditText
android:id=
android:layout_width=
android:layout_height=
android:ems=
"@+id/et2"
"match_parent"
"wrap_content"
"10" />
<EditText
android:id=
"@+id/et3"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:ems=
"10" />
<Button
android:id=
"@+id/b1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Do It" />
47
<TextView
android:id=
"@+id/tv2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"Numeric !"
android:textAppearance= "?android:attr/textAppearanceLarge" />
<Button
android:id=
"@+id/b2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text=
"E X I T" />
</LinearLayout>
________________________________________________________________________________________________
.java code :
public void onClick(View v)
{
String s, t, u;
double d1, d2, d3, d4;
s = et1.getText( ).toString();
try
{
d1 = Double.valueOf(s).doubleValue( );
}
catch(NumberFormatException e)
{
d1 = 0;
}
t = et2.getText( ).toString( );
try
{
d2 = Double.valueOf(t).doubleValue( );
}
catch(NumberFormatException e)
{
d2 = 0;
}
u = et3.getText( ).toString( );
try
{
d3 = Double.valueOf(u).doubleValue();
}
catch(NumberFormatException e)
{
d3 = 0;
}
For each value
received from the
user.
d4 = d1 * Math.pow((1 + d2/100), d3);
tv2.setText(String.format("%.02f", d4));
}
48
_________________________________________________________________________________________________
Formulas :
Math.pow( ) is the method to find the
power of a number. The format is :
Math.pow(X, Y);
x = number
y =power
d4 = d1 * Math.pow((1 + d2/100), d3);
The variable to be passed into the method.
Format the result to US
currency.
tv2.setText(String.format("%.02f", d4));
Number of decimal
positions.
____________________________________________________________________________
Notes ** - Most common formulas can be found on Google.
If a formula cannot be found, it is up to you to develop the formula!
___________________________________________________________________________
49
Android Development Notes and Vocabulary 2015
Android Multi_Activity
NoteSet >>>> Android_005
Open Eclipse
File
New
Android Application Project
Application Name :
Package Name :
Activity Name :
Actkpm1
z.x.c.actkpm1
NEXT NEXT
activity_main
FINISH
NEXT
NEXT
Eclipse Environment Opens!
Close : res / layout / fragment.xml
Code :
public class MainActivity extends Activity
FIX .java CODE !!
/ ORGANIZE CODE
Open activity_main.xml
Change activity_main to LinearLayout
Open activity.main.xml
Select Graphical Layout
Window
==>> [right-click]
Change Layout
Linear Layout (Vertical)
OK
TEST :
Select MainActivity.java
Run
Run
Android Application
OK
[Virtual Device opens!]
Activity_main
A
Activity_two
B
50
Activity_main.java Completed code :
package z.x.c.mact;
import
import
import
import
import
import
android.app.Activity;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle s)
{
super.onCreate(s);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = (new Intent(MainActivity. this, Macttwo.class));
startActivity(intent);
}
});
}
}
Copy .java TO CREATE A SECOND ACTIVITY :
Select
Activity
src
z.x.c.Package_name
Main_Actovoty.java
Select all code
Edit
Copy
Select
z.x.c.Activity
Edit
Paste
51
Copy xml layout to create a second UI :
Select
res / layout / activity_main.xml
[right-click]
New
Other
[New]
xml
xml File
NEXT
File Name :
activity_main_two
FINISH
Copy :
.xml code from:
activity_main.xml
TO
activity_main_two.xml
Insert a Button in each Activity :
Select activity_main.xml
Graphical Layout
[click-n-drag] Button into UI
Code :
android:id="@+id/b1"
android:text="Go To Activity 2"
TEST*
* Note - only activity_main will work!
Connect Button to code :
Main_Activity.java
Button b1 = (Button)findViewById(R.id.b1);
[hover over Button]
Import android.widget.Button
b1.setOnClickListener(new OnClickListener()
{
}
[hover over OnClickListener]
Import androif.view.View.OnClickListener
[hover over onClickListener]
Add unimplemented methods
Code is added to program !
==>
Insert : ");" following the } the end of
b1.setOnClickListener
[This closes the opened "(" ]
Organize Code
TEST
52
Make Button open another UI :
In the onClick() body :
Code : --> in the .java / in the onClick() method() :
Intent intent = (new Intent(Main_Activity.this, Main_Activity2.class));
startActivity(intent);
[hover over Intent]
select :
Import Intent
Modify Manifest :
Open AndroidManifest =>
AndroidManifest.xml
Select entire <activity> through </activity>
EDIT
COPY
Insert cursor after </activity> then,
EDIT
PASTE
In the new code, remove all of the <intent-filter> code !
android:name="Activity #2 .xml"
Open MainActivity2.java
( Indentify the new .xml)
setContent(R.Layout.activity-main_two);
Add code to return to Activity_main :
Select
Activity #2.java / MainActivity.java
Select >> Button b1 = (Button) through the end of onClick() code
EDIT
COPY
Select Main_Activity2.java
EDIT
PASTE
Code :
Intent intent = (new Intent(Main_Act #2.this, Main_Activity_#1.class));
**
**
This returns to the Activity #1 Window ( From the Activity #2 Window! )
TEST
53
54
Android Development Notes and Vocabulary 2015
Android Drawing Static Shapes - 6
NoteSet >>>> Android_006.1
Drawing Simple Static Shapes :
Vocabulary :
_________________________________________________________________________________________
Start a new Android Application !!
Draw Red Circle at location x = 200, y = 200 radius = 70 :
.java code !
** There is NO activity_main.xml
package z.x.c.an2;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle ss)
{
super.onCreate(ss);
The View class is extended
MyView myView = new MyView(this);
(Overridden) by a custom
setContentView(myView);
class called MyView.
}
public class MyView extends View
{
The onDraw( ) method is
public MyView(Context context)
overridden
{
super(context);
}
A Paint object called paint is
@Override
created
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
The color of the graphic will
Paint paint = new Paint( );
be RED
paint.setColor(Color.RED);
canvas.drawCircle(200, 200, 70, paint);
The drawCircle( ) method is called
}
at X = 200, Y = 200, Radius = 70
}
}
________________________________________________________________________________________________
55
Adding a second circle :
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint( );
paint.setColor(Color.RED);
canvas.drawCircle(200, 200, 70, paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(300, 300, 50, paint);
Second circle is BLUE ==> (300, 300, 50)
The BLUE circle is drawn on top of the RED
circle.
}
_________________________________________________________________________________________________
Method :
drawCircle(float X, float Y, float R, Paint p)
==>
Format for this method.
** Note the data-types preceding each position.
________________________________________________________________________________________________
Code to draw a circle in the exact center of screen :
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
int w = canvas.getWidth();
int h = canvas.getHeight();
float cx = (float) w/2;
float cy = (float) h/2;
canvas.drawCircle(cx, cy, 40, paint);
}
Find the center of
the screen.
Divide the width and
height by 2 (to identify the
center)
Print the circle on
the screen.
________________________________________________________________________________________________
56
To round off a circle, change the following code :
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Add this argument in the statement.
________________________________________________________________________________________________
To change the position of a circle :
Add 100 to the X
value, then print the
circle.
cx = cx + 100;
canvas.drawCircle(cx, cy, 40, paint);
_________________________________________________________________________________________________
Draw 1 pixel on screen :
float x = 30;
float y = 30;
canvas.drawPoint(x, y, paint);
_________________________________________________________________________________________________
Draw a line from one point to another point :
float x = 30;
float y = 30;
canvas.drawLine(x + 55, y + 46, x + 20, y + 20, paint);
Start X, Y position
End X, Y position
Represents the Paint object
_________________________________________________________________________________________________
** Note -
The X and Y coordinates are variables.
They must be declared before use.
They must be initialized to a numeric value before being referenced.
57
Entire sample code :
package z.x.c.an2;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle ss)
{
super.onCreate(ss);
MyView myView = new MyView(this);
setContentView(myView);
}
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawCircle(200, 200, 70, paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(250, 250, 50, paint);
int w =canvas.getWidth();
int h = canvas.getHeight();
float cx = (float) w/2;
float cy = (float) h/2;
canvas.drawCircle(cx, cy, 40, paint);
cx = cx + 100;
canvas.drawCircle(cx, cy, 40, paint);
float x = 30;
float y = 30;
canvas.drawPoint(x, y, paint);
canvas.drawLine(x + 55, y + 46, x + 20, y + 20, paint);
}
}
}
_________________________________________________________________________________________________
58
Android Development Notes and Vocabulary 2015
Android Drawing Modified Static Shapes - 6
NoteSet >>>> Android_006.2
Drawing on the Screen:
Vocabulary :
_________________________________________________________________________________________
Draw a square box :
protected void onDraw(Canvas canvas)
Graphic and Outline are drawn BLUE.
{
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(40);
canvas.drawPoint(50, 50 , paint);
Width = 10 pixals
}
Draws a solid BLUE square at X=50, Y=50
________________________________________________________________________________________________
Draw a circle :
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
Draw a circle
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(50);
canvas.drawPoint(50, 50 , paint);
}
_________________________________________________________________________________________________
59
Insert a picture on a screen :
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ibmpic);
canvas.drawBitmap(myBitmap, 10, 10, null);
}
Places the picture on
the screen.
The imported Image.
How to import an image into Eclipse :
Locate image to use. **
Rename the picture (lowercase letters only !
Eclipse
[click-n-drag] image into res / drawable-hdpi
OK
[click-n-drag] image into res / drawable-ldpi
OK
[click-n-drag] image into res / drawable-mdpi
OK
[click-n-drag] image into res / drawable-xhdpi
OK
[click-n-drag] image into res / drawable-xxhdpi
OK *
* Note - At this time, the image can be identified by the .java
** Note - Use Photoshop to size image, if necessary.
canvas.drawBitmap(myBitmap, 10, 10, null); >>> This code prints two pictures, one on top, one on bottom.
canvas.drawBitmap(myBitmap, 10, 410, null);
Note the difference in the Y coordinates.
_________________________________________________________________________________________________
60
Insert text on a screen:
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ibmpic);
canvas.drawBitmap(myBitmap, 10, 10, null);
canvas.drawBitmap(myBitmap, 10, 410, null);
The string will be printer
String ss = "Ken Mix";
between the pictures in this
canvas.drawText(ss, 10, 375, paint);
example using the default font
}
and font size, color, etc.
Sample Text code :
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
String ss = "Ken Mix";
canvas.drawText(ss, 15, 30, paint);
paint.setTextSize(32);
paint.setTypeface(Typeface.DEFAULT_BOLD);
ss = "Kenneth Mix";
canvas.drawText(ss, 15, 70, paint);
paint.setTextSize(42);
paint.setTypeface(Typeface.SANS_SERIF);
ss = "KP Mix Sans Serif font";
canvas.drawText(ss, 15, 125, paint);
paint.setTextSize(42);
paint.setTypeface(Typeface.SERIF);
ss = "KP Mix Serif Font";
canvas.drawText(ss, 15, 175, paint);
}
Serif AND bold :
paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD));
_________________________________________________________________________________________________
61
Android Development Notes and Vocabulary 2015
Android Animation - 6
NoteSet >>>> Android_006.3
Animation:
Vocabulary :
_________________________________________________________________________________________
Using PhotoShop, draw 4 square boxs :
Draw 4 images, all different. Pixel size 80 x 80.
Save as face1.png, face2.png face3.png face4 png.
Start a new project!
activity_main.xml:
Change layout to RelativeLayout.
Code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id=
android:layout_width=
android:layout_height=
android:layout_centerInParent =
"@+id/imgview"
"100dip"
"120dip"
"true" />
<ToggleButton
android:id=
android:layout_width=
android:layout_height=
android:textOn=
android:textOff=
android:layout_centerHorizontal=
"@+id/startstop_button"
"wrap_content"
"wrap_content"
"Stop"
"Start"
"true" />
</RelativeLayout>
62
Copy the defined images into Eclipse.
Select the first image (face1.png)
[click-n-drag] image to eact res/drawable-hdpi and the other drawable sizes.
Add an .xml file to the res / anim folder :
** You need to define this folder!
Package Explorer
[right-click] res folder
New
Folder
name =
anim
FINISH
Add a New .xml file :
Package Explorer
Select res/anim folder
New
Android XML File
Name =
Select =
frame_anim
root element
Resource Type =
Project =
Drawable
An3
Root Element
animation-list
=
(name of project)
FINISH
frame_anim.xml
Code :
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot = "false">
<item android:drawable="@drawable/face1"
<item android:drawable="@drawable/face2"
<item android:drawable="@drawable/face3"
<item android:drawable="@drawable/face4"
android:duration = "100" />
android:duration = "100" />
android:duration = "100" />
android:duration = "100" />
</animation-list>
63
Main_Activity.java
Code :
package z.x.c.an3;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends Activity
{
AnimationDrawable animation;
@Override
protected void onCreate(Bundle sa)
{
super.onCreate(sa);
setContentView(R.layout.activity_main);
final ToggleButton startStopButton =
(ToggleButton)findViewById(R.id.startstop_button);
final ImageView imgView = (ImageView)findViewById(R.id.imgview);
startStopButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (startStopButton.isChecked())
{
imgView.setBackgroundResource(R.drawable.frame_anim);
animation = (AnimationDrawable) imgView.getBackground();
animation.start( );
}
else
{
animation.stop( );
}
}
});
}
}
** Note - TEST program The ToggleButton should start and stop the animation!
64