Android Application Development Programming

DrAbiramiAnandhan 48 views 43 slides Oct 10, 2024
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

Android Programming


Slide Content

1
Android Programming
T.ABIRAMI/KEC
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College
[email protected]

XML in Android
XML stands for Extensible
 Markup Language.
XML is a markup language much like HTML
used to describe data
XML tags are not predefined
T.ABIRAMI/KEC 2

XML Syntax
//This line is called the XML prolog:
<?xml version="1.0" encoding="UTF-8 "?>
<root>
 
<child>
 
   <subchild>.....</subchild>
 
</child>
</root>
T.ABIRAMI/KEC 3

XML Elements
An XML element is everything from
(including) the element's start tag to
(including) the element's end tag.
An element can contain:
text
attributes
other elements
or a mix of the above
T.ABIRAMI/KEC 4

XML Naming Rules
XML elements must follow these naming rules:
Element names are case-sensitive
Element names must start with a letter or
underscore
Element names cannot start with the letters xml
(or XML, or Xml, etc)
Element names can contain letters, digits,
hyphens, underscores, and periods
Element names cannot contain spaces
Any name can be used, no words are reserved
T.ABIRAMI/KEC 5

xmlns:android Defines the Android namespace.
Namespaces are how you access all the Android
attributes and tags that are defined by Google
This attribute should always be set to
"http://schemas.android.com/apk/res/android".
In your example, the
 
Namespace Prefix 
is
"android" and the
 
Namespace URI 
is
"http://schemas.android.com/apk/res/android"
T.ABIRAMI/KEC 6
xmlns:android

xmlns:android
The xmlns attribute declares an XML
Namespace.

Namespaces are used to avoid conflicts between
element names when mixing XML languages.
Name Space in order to specify that the
attributes listed below, belongs to Android .
Thus they starts with "android:"
T.ABIRAMI/KEC 7

<Button
 
  android:id="@+id/button"
 
  android:layout_width="wrap_content"
 
  android:layout_height="wrap_content"
 
  android:text=“Submit" />
T.ABIRAMI/KEC 8

Syntax
<RootTag
xmlns:android="http://schemas.android.c
om/apk/res/android"
 
 
xmlns:tools="http://schemas.android
.com/tools" >
T.ABIRAMI/KEC 9

XML Example
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>

Understanding the default activity Code
MainActivity: It is the name of the class we are having. It is also
named as your activity name. If you remember the
 project creation we
have given this name.
ActionBarActivity : Our class MainActivity is extending this class.
 
onCreate(Bundle savedInstanceState): This is an overridden
method.
 It is inside the
ActionBarActivity class.
 
This method will be executed
 when the app is opened (the activity is
created). So basically you can think that it is the main method.
Bundle: It is another predefined class. It is basically used for passing
data between android activities.
setContentView(R.layout.activity_main): This method takes a
layout and it set the view as the layout
 to the activity.  
We created a layout named activity_main.xml. As I told you before
that all the ids for all the components are generated automatically and
stored inside R.java file. We are selecting the id of our layout from
R.java file (R.layout.activity_main).
T.ABIRAMI/KEC 11

View
 class
It
 is a superclass for all GUI components
in Android.
Commonly used Views are :
EditText
ImageView
TextView
Button
ImageButton
CheckBox etc…
T.ABIRAMI/KEC 12

Initializing a View
predefined class named View in android.
 
For initializing a View from XML layout file,
we have a method
 
findViewById().
 
T.ABIRAMI/KEC 13

findViewById(int id)
R is a Class in android that are having the
id’s of all the view’s.
findViewById() is a call to a function by a
reference of View class
findViewById is a method that finds the
view from the layout resource file that are
attached with current Activity.
to find xml id
T.ABIRAMI/KEC 14

Syntax
Declaration of UI
UIclassname objectname=(Uiclassname) findViewById(R.id.UniqueUI
_ID);
Button button = (Button) findViewById(R.id.button_send);
 

 Button
It represents a push
 button.
A Push buttons can be clicked, or pressed by the user to
perform an action.
There are different types of buttons used in android such as
CompoundButton,
 ToggleButton, 
RadioButton ,
ImageButton etc..
Android buttons are GUI components which are sensible to
taps (clicks) by the user.
It perform different actions or events like click event,
pressed event, touch event etc.
T.ABIRAMI/KEC 16

Button
View is the superclass for all widgets and
the OnClickListener interface belongs to
this class
Button
Create the button object
Use OnClickListner to make it listen to the
user's click
Use onClick to execute actions after the
user clicks the button
T.ABIRAMI/KEC 17

Handling Click events in Button | Android
There are 2 ways to handle the click event
in button
1.Onclick in xml layout
2.Using an OnClickListener interface
T.ABIRAMI/KEC 18

Onclick in XML layout
When the user clicks a button, the Button
object receives an on-click event.
To make click event work
add
 
android:onClick 
attribute to the
Button element in your XML layout.
The value for this attribute must be the
name of the method you want to call in
response to a click event.
 
T.ABIRAMI/KEC 19

Onclick in xml layout
<Button
 
   
android:id="@+id/button_send"
 
   
android:layout_width="wrap_content"
 
   
android:layout_height="wrap_content"
 
   
android:text="@string/button_send"
 
   
android:onClick="sendMessage"
 
/>
T.ABIRAMI/KEC 20

In MainActivity class : Onclick in
xml layout
/** Called when the user touches the
button */
public void sendMessage(View view)
{
 // Do something in response to
button click
}
T.ABIRAMI/KEC 21
Make sure that your sendMessage method should have the following :
•Be public
•Return void
•Define a View as its only parameter (this will be the View that was
clicked)

Using an OnClickListener : Operation on button
button.setOnClickListener ( new View.OnClickListener()
{
public void onClick(View v)
{
// Do something in response to button click
}
}
);
T.ABIRAMI/KEC 22

public class MainActivity extends ActionBarActivity
 {Button b;
 @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
 }
});
 }
 }
T.ABIRAMI/KEC 23

Handling EditText
getText(): This method returns an
Editable
 instance. And it contains the text
from the EditText.
So for storing the Text into a String
object,
use the toString() method to convert it
into String.
setText(String s):This method takes a
string and set the string to EditText.
T.ABIRAMI/KEC 24

Input From user
1. using setText()
tt.setText(Integer.toString(z));
2.using getText()

x=Integer.parseInt(amount1.getText().toString());

private void showMessage(){
 
     
String s = editText.getText().toString();
 
       
String greeting = "Hello "+s;
 
       
editText.setText(greeting);
 
   
}
T.ABIRAMI/KEC 26

Android Toast class
Toast can be used to display information for the short
period of time.
Toast class is used to show notification for a particular
interval of time. After sometime it disappears.
It doesn't block the user interaction.
A toast contains message to be displayed quickly and
disappears after sometime.
android.widget.Toast class is the subclass of
java.lang.Object class.
T.ABIRAMI/KEC 27

Constants of Toast class
Constant Description
public static final int
LENGTH_LONG
displays view for the long
duration of time.
public static final int
LENGTH_SHORT
displays view for the short
duration of time.
T.ABIRAMI/KEC 28

Methods of Toast class
Method Description
public static Toast makeText(Context
context, CharSequence text, int
duration)
makes the toast
containing text and
duration.
public void show() displays toast.
public void setMargin (float
horizontalMargin, float verticalMargin)
changes the horizontal
and vertical margin
difference.
T.ABIRAMI/KEC 29

Example
Toast.makeText(getApplicationC
ontext(),"Hello
 Javatpoint",Toast
.LENGTH_SHORT).show();
  
T.ABIRAMI/KEC 30

Toast
 toast=Toast.makeText(getApplicatio
nContext(),"Hello
 Javatpoint",Toast.LENGT
H_SHORT);
  
toast.setMargin(50,50);
  
toast.show();
  
T.ABIRAMI/KEC 31

DDMS (Dalvik Debug Monitor
Server)
It is integrated into Eclipse and is also
shipped in the
 tools/ directory of the SDK.
It works with both the emulator and a
connected device. If both are connected and
running simultaneously, DDMS defaults to
the emulator.
The service could include message
formation, call spoofing, capturing
screenshot, exploring internal threads and
file systems etc..
T.ABIRAMI/KEC 32

TextView UI control / Class
To display text on the screen
Class Heirarchy:
java.lang.Object


android.view.View


android.widget.TextView
 
T.ABIRAMI/KEC 33

Xml code
<TextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=“Test" />
T.ABIRAMI/KEC 34

Attributes
android:typeface="sans"
android:textColor="@color/colorPrimary“
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
textView.setTextColor(Color.RED);
T.ABIRAMI/KEC 35

T.ABIRAMI/KEC 36
ATTRIBUTES DESCRIPTION
android:text Sets text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottom Sets images or other graphic assets to below of the Textview.
android:drawableEnd Sets images or other graphic assets to end of Textview.
android:drawableLeft Sets images or other graphic assets to left of Textview.
android:drawablePaddingSets padding to the drawable(images or other graphic assets) in the Textview.
android:autoLink This attribute is used to automatically detect url or emails and show it as clickable link.
android:autoText Automatically correct spelling errors in text of the Textview.
android:capitalize It automatically capitalize whatever the user types in the Textview.
android:drawableRight Sets drawables to right of text in the Textview.
android:drawableStart Sets drawables to start of text in the Textview.
android:drawableTop Sets drawables to top of text in the Textview.
android:ellipsize Use this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:ems Sets width of the Textview in ems.
android:gravity We can align text of the Textview vertically or horizontally or both.
android:height Use to set height of the Textview.
android:hint Use to show hint when there is no text.
android:inputType Use to set input type of the Textview. It can be Number, Password, Phone etc.
android:lines Use to set height of the Textview by number of lines.
android:maxHeight Sets maximum height of the Textview.
android:minHeight Sets minimum height of the Textview.
android:maxLength Sets maximum character length of the Textview.
android:maxLines Sets maximum lines Textview can have.
android:minLines Sets minimum lines Textview can have.
android:maxWidth Sets maximum width Textview can have.
android:minWidth Sets minimum lines Textview can have.
android:textAllCaps Show all texts of the Textview in capital letters.
android:textColor Sets color of the text.
android:textSize Sets font size of the text.
android:textStyle Sets style of the text. For example, bold, italic, bolditalic.
android:typeface Sets typeface or font of the text. For example, normal, sans, serif etc
android:width Sets width of the TextView.

Java Code
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Android"); //set text for text view
textView.setTextSize(20);
textView.setBackgroundColor(Color.BLACK);//set background
color
T.ABIRAMI/KEC 37

EditText
It is a user interface control
It is used to allow the user to enter or
modify the text.
Package Stru..
java.lang.Object
   
↳android.view.View
    
↳android.widget.TextView
     
↳android.widget.EditText
T.ABIRAMI/KEC 38

EditText  :Attributes Inherited from TextView Class
<EditText
 
       android:id="@+id/txtSub"
 
       android:layout_width="match_parent"
 
       android:layout_height="wrap_content"
 
       android:hint="Subject"
 
       android:inputType="text"/>
T.ABIRAMI/KEC 39

EditText Attributes
Attribute Description
android:id It is used to uniquely identify the control
android:gravity It is used to specify how to align the text like left, right, center, top,
etc.
android:text It is used to set the text.
android:hint It is used to display the hint text when text is empty
android:textColor It is used to change the color of the text.
android:textColorHint It is used to change the text color of hint text.
android:textSize It is used to specify the size of the text.
android:textStyle It is used to change the style (bold, italic, bolditalic) of text.
android:background It is used to set the background color for edit text control
android:ems It is used to make the textview be exactly this many ems wide.
android:width It makes the TextView be exactly this many pixels wide.
android:height It makes the TextView be exactly this many pixels tall.
android:maxWidth It is used to make the TextView be at most this many pixels wide.
android:minWidth It is used to make the TextView be at least this many pixels wide.
android:textAllCaps It is used to present the text in all CAPS
android:typeface It is used to specify the Typeface (normal, sans, serif, monospace)
for the text.
android:textColorHighlightIt is used to change the color of text selection highlight.
android:inputType It is used to specify the type of text being placed in text fields.
android:fontFamily It is used to specify the fontFamily for the text.
android:editable If we set false, EditText won't allow us to enter or modify the text
T.ABIRAMI/KEC 40

public class
 MainActivity extends AppCompatActivity {
 
   @Override
 
   protected void onCreate(Bundle savedInstanceState) {
 
       super.onCreate(savedInstanceState);
 
       setContentView(R.layout.activity_main);
 
       EditText et = (EditText) findViewById(R.id.txtSub);
 
       String name = et.getText().toString();
 
   }
}
T.ABIRAMI/KEC 41
EditText simpleEditText = (EditText) findViewById(R.id.et_simple);
String strValue = simpleEditText.getText().toString();

Validating your Text
if
 (name.getText().toString().isEmpty()|| password.getText().toString().isEmpty()
||
 email.getText().toString().isEmpty() || dob.getText().toString().isEmpty()
 
                       || phoneno.getText().toString().isEmpty())
{
 
 result.setText("Please Fill All the Details")
}
 else
 
{
result.setText(name.getText().toString() +
 password.getText().toString()
 
                           + email.getText().toString() + dob.getText().toString()
 
                           + phoneno.getText().toString())
}
T.ABIRAMI/KEC 42

EditText Example
T.ABIRAMI/KEC 43