SlideShare a Scribd company logo
ANDROID BASICS
J M GITHEKO
AGENDA
• UI ELEMENTS
• View and ViewGroup
• Layouts
• Relative, Linear, Grid, ListView
• Frame
• Buttons
• TextView
•
•
•
•
• See: http://developer.android.com/guide/topics/ui/layout/listview.html
ELEMENTS OF A UI
VIEWGROUP AND VIEW
• ViewGroup can contain Views
• A View can contain widgets such as buttons, EditText,
TextView…
• They are grouped in a tree structure
CREATING VIEWS
Define a view/widget in the layout XML file and
assign it a unique ID:
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
VIEW CONT’D
Then create an instance of the view object and
capture it from the layout (typically in
the onCreate()method):
Button myButton = (Button) findViewById(R.id.my_button);
VIEWS CONT’D
• Defining IDs for view objects is important when creating
a RelativeLayout.
• In a relative layout, sibling views can define their layout relative to
another sibling view, which is referenced by the unique ID.
• An ID need not be unique throughout the entire tree, but it should be
unique within the part of the tree you are searching (which may often
be the entire tree, so it's best to be completely unique when possible).
LAYOUTS
NOTE THAT LAYOUTS ARE VIEWGROUPS
LINEAR LAYOUT EXAMPLE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button" />
</LinearLayout>
LAYOUTS: TRY TO BE SIMPLE AND ELEGANT
DECLARING LAYOUTS
• XML use is recommended due to its simplicity and easy visualization of the design
• You can create layouts in code but difficult to debug
• XML allows you to create layouts customization for different orientations
(landscape, portrait)
• XML allows you to create Internationalized layouts (different languages, same app)
LINEAR LAYOUT
• Vertical Alignment
• Horizontal Alignment
• Similar to same layout in App Inventor
LINEAR LAYOUT EXAMPLE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
• Widgets are positioned relative to each other
• Use layout parameters such as:
• android:layout_toRightOf=“@id/btnx”
• Android:layout_below=“@id/times”
RELATIVE LAYOUT
LIST VIEW
• Displays a list of scrollable items.
• The list items are automatically inserted to the list using
an Adapter that pulls content from a source such as an
array or database query and converts each item result
into a view that's placed into the list.
• See:
http://www.tutorialspoint.com/android/android_list_view.
htm
LIST VIEW 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"
android:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
GRID VIEW
• GridView is a ViewGroup that displays items in a two-
dimensional, scrollable grid.
• The grid items are automatically inserted to the layout
using a ListAdapter
EXAMPLE (SIMILAR TO JAVA GRID
LAYOUT)
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
INPUT CONTROLS/WIDGETS
• Buttons
• Text Fields
• Label
• Checkboxes
• Radio Buttons
• Spinners
• Pickers
BUTTONS
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
ONCLICK BUTTON LISTENER
• In OnCreate method:
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
Details: http://developer.android.com/guide/topics/ui/controls/button.html
TEXT FIELDS
<EditText
android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
android:inputType="textEmailAddress" />
LABEL (TEXTVIEW)
<TextView
android:id="@+id/text_id"
android:layout_width="300dp"
android:layout_height="200dp"
android:capitalize="characters"
android:text="hello_world"
android:textColor="@android:color/holo_blue_dark"
android:textColorHighlight="@android:color/primary_text_dark"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:textSize="50dp"/>
CHECKBOXES
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:id="@+id/checkbox_meat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/meat"
android:onClick="onCheckboxClicked"/>
<CheckBox android:id="@+id/checkbox_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
TOGGLE BUTTONS
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="@+id/toggleButton"
android:checked="true"
android:layout_below="@+id/imageButton"
android:layout_toLeftOf="@+id/imageButton"
android:layout_toStartOf="@+id/imageButton" />
SPINNERS<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
To populate the spinner with a list of choices, you
then need to specify a SpinnerAdapter in your
Activity or Fragment source code.
The choices you provide for the spinner can come
from any source, but must be provided through an
SpinnerAdapter, such as an ArrayAdapter if the
choices are available in an array or a CursorAdapter
if the choices are available from a database query.
See: http://developer.android.com/guide/topics/ui/controls/spinner.html
To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity orFragment source code.
PICKERS
• See: http://developer.android.com/guide/topics/ui/controls/pickers.html
EVENT HANDLERS
• Create an event listener
• Register event listener
• Create an event handler
• Event occurs
• Event listener fires
• Handler is called
• Example:
package com.example.myapplication;
public class MainActivity extends ActionBarActivity {
Button b1; //declare a button
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Select activity
b1=(Button)findViewById(R.id.button); //Instantiate button
b1.setOnClickListener( new View.OnClickListener() { //Create listener & register
@Override
public void onClick(View v) { //Event handler
TextView txtView = (TextView) findViewById(R.id.textView); //Create textview
txtView.setTextSize(25); //Set textview size
}
});
FRAGMENTS
• Is a part of an Activity; a sub-activity
• Can receive input events
• Has its own lifecycle but can not exist outside Activity
• A fragment can be added or removed while the owner Activity is running
• It can be re-used
CREATE FRAGMENT IN JAVA
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
LIFECYCLE METHODS
• Fragments like Activities have lifecycle methods
• The key one is onCreateView() which is mandatory
• In Activities, onCreate() is mandatory
• Others are:
• onStart()
• onResume() - app running
• onPause() - app visible in the background
• onStop() - app hidden in the background
• onDestry() – app removed from memory
• onRestart() – app restored from onStop() condition – made visible in foreground
• When app is launched, onCreate(), onStart() and onResume() are called()
OR ADD A FRAGMENT TO AN
ACTIVITY USING XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
APPLY THE LAYOUT TO YOUR ACTIVITY:
EXERCISE: TEMPERATURE CONVERTER –
FAHRENHEIT TO CELSIUS
• Requirements – Interface, language, platform
• Design – UI,…
• Construction –
• UI – XML
• Activity template
• UI elements
• Event listeners and handlers
• Testing
• Deployment
• Submit as Bitbucket link
VCS - BITBUCKET• Create free account
• Create repository (Git)
• Download and install Git on your
PC
• Create project in Android Studio
• Configure VCS (Git)
• Push project (Give Bitbucket
repository URL)
A/STUDIO AFTER VCS
CONFIGURATION
DEFINE REPOSITORY IN A/STUDIO
READY TO PUSH
PUSHING
PUSH COMPLETE
BITBUCKET POST-PUSH
BITBUCKET
REPOSITORY URL
Android programming basics

More Related Content

PDF
Android Basics
PPT
Android basics
PPTX
Android basics
PPTX
Android Development for Beginners with Sample Project - Day 1
PPTX
Get an Android tutorial for beginners
PPT
android-tutorial-for-beginner
PDF
Android - From Zero to Hero @ DEVit 2017
PPTX
Android application-component
Android Basics
Android basics
Android basics
Android Development for Beginners with Sample Project - Day 1
Get an Android tutorial for beginners
android-tutorial-for-beginner
Android - From Zero to Hero @ DEVit 2017
Android application-component

What's hot (20)

PPTX
Android basic principles
PPTX
Android architecture
PDF
Android tutorial
PPT
Google Android
PPTX
PDF
Android Development: Build Android App from Scratch
PPTX
What’s new in aNdroid [Google I/O Extended Bangkok 2016]
PPTX
Basic of Android App Development
PPTX
Android beginners David
PDF
Android studio
ODP
Intro To Android App Development
PDF
Android Development in a Nutshell
PPTX
Android apps development
PPT
Android Applications Development
PPTX
Introduction to Android Development
PPS
Getting Started With Android
PPT
Android application structure
PPTX
Introduction to Android development - Presentation
PPTX
Android Life Cycle
PPT
Day 3: Getting Active Through Activities
Android basic principles
Android architecture
Android tutorial
Google Android
Android Development: Build Android App from Scratch
What’s new in aNdroid [Google I/O Extended Bangkok 2016]
Basic of Android App Development
Android beginners David
Android studio
Intro To Android App Development
Android Development in a Nutshell
Android apps development
Android Applications Development
Introduction to Android Development
Getting Started With Android
Android application structure
Introduction to Android development - Presentation
Android Life Cycle
Day 3: Getting Active Through Activities
Ad

Similar to Android programming basics (20)

PPTX
PPTX
UNIT5newpart2pptx__2024_11_13_09_52_11 (1).pptx
PPTX
Introduction to android
PPTX
Android Study Jam 2
PPTX
03 layouts & ui design - Android
PPTX
layout and UI.pptx
DOCX
Android xml-based layouts-chapter5
PPTX
Android development session 3 - layout
PDF
Mobile Application Development -Lecture 07 & 08.pdf
PDF
Android ui layout
PPT
android layouts
PPTX
Android User Interface
PPT
View groups containers
PPTX
creating User interface in mobile and app dev
PPTX
08ui.pptx
PDF
01 08 - graphical user interface - layouts
PPTX
Lecture_On_AndroidApp_UserInterface.pptx
PPTX
Android Training (Android UI)
PPTX
Android Layout.pptx
DOCX
How to create ui using droid draw
UNIT5newpart2pptx__2024_11_13_09_52_11 (1).pptx
Introduction to android
Android Study Jam 2
03 layouts & ui design - Android
layout and UI.pptx
Android xml-based layouts-chapter5
Android development session 3 - layout
Mobile Application Development -Lecture 07 & 08.pdf
Android ui layout
android layouts
Android User Interface
View groups containers
creating User interface in mobile and app dev
08ui.pptx
01 08 - graphical user interface - layouts
Lecture_On_AndroidApp_UserInterface.pptx
Android Training (Android UI)
Android Layout.pptx
How to create ui using droid draw
Ad

More from Egerton University (7)

PPTX
COMP340 TOPIC 4 THREE.JS.pptx
PPTX
Android ui with xml
PPTX
Event handler example
PPT
javascript examples
PPTX
Php basics
PPTX
Website management
PPTX
My sql command line client
COMP340 TOPIC 4 THREE.JS.pptx
Android ui with xml
Event handler example
javascript examples
Php basics
Website management
My sql command line client

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PPTX
A Presentation on Artificial Intelligence
PDF
Approach and Philosophy of On baking technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Modernizing your data center with Dell and AMD
Review of recent advances in non-invasive hemoglobin estimation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
A Presentation on Artificial Intelligence
Approach and Philosophy of On baking technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Unlocking AI with Model Context Protocol (MCP)
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Chapter 3 Spatial Domain Image Processing.pdf
Modernizing your data center with Dell and AMD

Android programming basics