My own View Android development Maarten Pennings 2011 oct 14.

Slides:



Advertisements
Similar presentations
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Advertisements

Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
Crash Course in Android Development. 2 Content  Installing the ADT  Hardware and OS requirements  Java  ADT Bundle  Eclipse Project Setup  Drawing.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Introducing the Sudoku Example
Basic Drawing Techniques
2D Graphics: Part 2.
1 Mobile Computing Monetizing An App Copyright 2014 by Janson Industries.
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Android Dialog Boxes AlertDialog - Toast
User Interfaces: Part 1 (View Groups and Layouts).
Android Graphics Library. Color Android colors are represented with four numbers, one each for alpha, red, green, and blue (ARGB). Each component can.
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android Application Lifecycle and Menus
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
Android and s Ken Nguyen Clayton state University 2012.
Custom Widget 2 UNIT 27 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Custom Widget –Canvas 를 이용하여 Custom Widget 을 만든다. 2.
Mobile Computing Lecture#12 Graphics & Animation.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
David Sutton 2D GRAPHICS IN ANDROID. INTRODUCTION AND OUTLINE  In this week’s session we will create a simple Kaleidoscope application. Topics that will.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Android Programming.
Andrew(amwallis) Classes!
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Application Development 1 6 May 2018
GUI Programming Fundamentals
Android – Event Handling
Android Introduction Hello World.
2D Graphics: Part 2.
Android Layouts 24 July 2018 S.RENUKADEVI/AP/SCD/ANDROID LAYOUTS 1.
ITEC535 – Mobile Programming
Reactive Android Development
תכנות ב android אליהו חלסצ'י.
CIS 470 Mobile App Development
Android Programming Lecture 6
CIS 470 Mobile App Development
Android Lists and Fragments
Graphics with Canvas.
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CS 240 – Advanced Programming Concepts
Chapter 5 Your Second Activity.
CIS 470 Mobile App Development
Android Sensor Programming
Android Sensor Programming
User Interface Development
Presentation transcript:

My own View Android development Maarten Pennings 2011 oct 14

Views User Interface elements are known as Views – Button – TextView – … I have written my own view – NumGridView – That can be used from XML layout in activity

NumGridView My app contains a 5x5 grid with numbers – This is my NumGridView drawing itself When you press a grid cell it is incremented – This is activity specific (coded in a listener)

Activity code is simple public classextends public class NumGridActivity extends Activity { NumGridView mNumGridView; new OnCellTouchListener mNumGridView_OnCellTouchListener= new OnCellTouchListener() public void onCellTouch( NumGridView v, int x, int y ) { v.setCell(x, y, v.getCell(x,y)+1 ); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNumGridView= (NumGridView)findViewById(R.id.numgridview); mNumGridView.setOnCellTouchListener(mNumGridView_OnCellTouchListener); }

Activity layout has minor twists LinearLayout <LinearLayout xmlns:android=" xmlns:numgrid=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > TextView <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="10mm" /> nl.fampennings.numgrid.NumGridView <nl.fampennings.numgrid.NumGridView android:layout_width="fill_parent" android:layout_height="fill_parent" numgrid:cellCountX="5" numgrid:cellCountY="5" /> LinearLayout

Required: file attrs.xml resources declare-styleable attr /declare-styleable /resources

Required: file NumGridView.java package package nl.fampennings.numgrid; import import android.view.View;... publicclassextends public class NumGridView extends View { // Member variables... public public NumGridView (Context context, AttributeSet attrs) {...} public public void setCell (int x, int y, int v) {...} public public int getCell (int x, int y ) protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) protected void onDraw (Canvas canvas) {...} // OnCellTouchListener... } Resides in same package Extends basic View class Lazy: only implemented constructor for XML Setter and Getter for cell value “Mandatory” overrides Cell touch call-back

Constructor public public NumGridView(Context context, AttributeSet attrs) { super(context, attrs);... // Get the xml attributes TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumGridView); mStretch = a.getBoolean(R.styleable.NumGridView_stretch, false); mCellCountX = a.getInt(R.styleable.NumGridView_cellCountX, 8); mCellCountY = a.getInt(R.styleable.NumGridView_cellCountY, 8); a.recycle(); // Setup the grid cells new mCells= new int[mCellCountX][mCellCountY]; forfor for(int y=0; y<mCellCountY; y++) for(int x=0; x<mCellCountX; x++) mCells[x][y]= 0; } Call parent constructor Retrieve styled attribute information Lookup the attributes (pass default in case attribute is not set) “free”! Create the actual grid data array, and fill with zero’s

Setter and Getter public public void setCell(int x, int y, int v) { if if( ! (0<=x && x<mCellCountX) ) thrownew throw new IllegalArgumentException("setCell: x coordinate out of range"); if if( ! (0<=y && y<mCellCountY) ) thrownew throw new IllegalArgumentException("setCell: y coordinate out of range"); mCells[x][y]= v; invalidate(); } public public int getCell(int x, int y ) { if if( ! (0<=x && x<mCellCountX) ) thrownew throw new IllegalArgumentException("setCell: x coordinate out of range"); if if( ! (0<=y && y<mCellCountY) ) thrownew throw new IllegalArgumentException("setCell: y coordinate out of range"); return return mCells[x][y]; } Sort of assert (don’t know if it’s wise) Set the value Get the value Force a re-draw

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Extract the Ms (MesaureSpec) parameters int widthMsMode = MeasureSpec.getMode(widthMeasureSpec); int widthMsSize = MeasureSpec.getSize(widthMeasureSpec); int heightMsMode = MeasureSpec.getMode(heightMeasureSpec); int heightMsSize = MeasureSpec.getSize(heightMeasureSpec); // Determine preferred size mCellWidth = (int)Math.floor(widthMsSize / mCellCountX ); mCellHeight= (int)Math.floor(heightMsSize / mCellCountY ); mOffsetX= ( widthMsSize - mCellWidth*mCellCountX ) / 2; mOffsetY= ( heightMsSize - mCellHeight*mCellCountY ) / 2; // Must declare my size setMeasuredDimension( mOffsetX + mCellCountX*mCellWidth + mOffsetX, mOffsetY + mCellCountY*mCellHeight + mOffsetY ); } The renderer calls us with onMeasure to ask how big we want to be (sometimes passing max sizes). We must respond by calling setMeasuredDimension. Just a fragment; real code is harder

protected void onDraw(Canvas canvas) { super.onDraw(canvas);... // Draw all cells for for(int y=0; y<mCellCountY; y++) { for for(int x=0; x<mCellCountX; x++) { // Get cell data int v = mCells[x][y]; int dx= x*mCellWidth+mOffsetX; int dy= y*mCellHeight+mOffsetY; // Draw a rectangle in the cell canvas.drawRect( new Rect(dx+1,dy+1,dx+mCellWidth-2,dy+mCellHeight-2), mPaintBg); // Draw value canvas.drawText( ""+v, dx+cx, dy+cy, mPaintFg); } The renderer passes us the canvas to draw ourselves on We loop over all cells, … … draw a box on the canvas, … … and draw the cell value centered in the box The ‘paint’ (created in onCreate) is our pencil (color, font style, typeface, …)

onCellTouchListener public interface public interface OnCellTouchListener { void onCellTouch(NumGridView v, int x, int y); } protected protected OnCellTouchListener mOnCellTouchListener; public public void setOnCellTouchListener(OnCellTouchListener listener) { mOnCellTouchListener = listener; public boolean dispatchTouchEvent(MotionEvent event) { // First dispatch calls to our cell touch listener... if if( mOnCellTouchListener!=null ) { int x=(int)(event.getX()) - mOffsetX; int y=(int)(event.getY()) - mOffsetY; if if( 0<=x && x<mCellWidth*mCellCountX && 0<=y && y<mCellHeight*mCellCountY ) { // Touch was on cell (not on padding area) mOnCellTouchListener.onCellTouch(this,x/mCellWidth,y/mCellHeight); } //... next dispatch calls from the super class return return super.dispatchTouchEvent(event); } A class local interface definition for our listener One function: touch on x,y The member variable to store the listener The Setter for the listener The NumGridView does not register itself as listener, it overrides the dispatcher. This allows our customers to register an event listener for raw coordinates. map raw (x,y) to cell (x,y) Call listener (if set, if touch was on cell) Let super class do rest of dispatching

THE END