37
Android Effective UI Tips, Tricks and Patterns Adham Enaya adhamenaya.com 1

Android Effective UI: Tips, Tricks and Patterns

Embed Size (px)

Citation preview

Page 1: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 1

Android Effective UITips, Tricks and Patterns

Adham Enaya

Page 2: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 2

Topics

• Introduction• Resources and qualifiers• Using ButterKnife Library• Model View Presenter (MVP)• Some advices

Page 3: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 3

Motivation

• Provide and enjoyable user experience.• Change the UI depending on the device.• Improve layout performance.• Create Customized widgets.• Make the UI code maintainable and testable.

Page 4: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 4

Android Devices

• Almost 19,000 devices(60% growth yearly)

Page 5: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 5

Motivation (2)

How can we change the UI depending on the device ?

By using Android Resources

Page 6: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 6

Android Recourses

• Color: declares the app color pallet

• Drawable: images assets• Layout: xml file declares

the app layout.• Menu: xml file declares

the UI menu• Integer: integer values in xml and java codes.

Page 7: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 7

Resources & Qualifiers

• Screen Density: hdpi, mdpi … etc• Screen Size: large, small .. etc• Language: ar, en, ar-JO• Min Width: sw600dp, sw720dp• Screen Orientation: port and land • Android API level: v7, v8, v9 …etc

Page 8: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 8

Resources & Qualifiers/Screen Destiny

• Screen Destiny: quantity pixels(dots) per inch– ldpi: ~120dpi – mdpi: ~160dpi – hdpi: ~240dpi– xhdpi: ~360dpi– xxhdpi:480dpi– xxxhdpi: ~640dpi

Page 9: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 9

Resources & Qualifiers/Screen Density

Page 10: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 10

Resources & Qualifiers/Screen Destiny

Page 11: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 11

Resources & Qualifiers/Screen Size

• xlarge screens are at least 960dp x 720dp > 7 inch• large screens are at least 640dp x 480dp 5-7 inch• normal screens are at least 470dp x 320dp 3.0-4.7 inch• small screens are at least 426dp x 320dp 2.2-2.7 inch

Page 12: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 12

Resources & Qualifiers/Screen Size

• Android screen resolution distribution in China. (Umeng data)

Page 13: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 13

How to use Qualifiers?

• Create new directory in res/ folder• The name of the folder in this format:– <resources_name>-<qualifier>– Resource name: such drawable, layout …– Qualifier: such as hdpi, mdpi, land, port…

Page 14: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 14

Android resources example

• drawable/: for every thing • drawable-en/ : for English locale• drawable-en-port/ : for English and in portrait

orientation• drawable-en-notouch-12key/ : For English and

in notouch devices and 12key as primary input• drawable-port-ldpi/ : for low density screen and

in portrait orientation

Page 15: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 15

Android resources example(2)

• layout-v4: API level (android 1.6)• layout- h720dp: Available height 720• Layout-sw600dp: Smallest width 600

Same application with different layouts for each device.

Page 16: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 16

Using Butter Knife Library (1)

• Inject Views and view events in activities and fragments.

• Notation –based. By : Jake Wharton (Square).

Page 17: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 17

Using Butter Knife Library (2)

• Using findViewById

Page 18: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 18

Using the Butter Knife Library (3)

• Add dependency to build.gradle:– compile 'com.jakewharton:butterknife:6.1.0‘

• In the onCreate() method of the activity, before using any the views, call inject on the Butterknife object:– ButterKnife.inject(this);

Page 19: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 19

ButterKnife / Insatiate the view (4)

• Use: – @InjectView(R.id.sample_textview);– TextView textview;

• Rather than: – TextView textview;– textview =

(TextView)findViewById(R.id.sample_textview);

Page 20: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 20

Butter Knife / View Events(5)

• Use:@OnClick(R.id.sample_textview)public void showToastMessage(){

// DO SOTHING ….

}

Page 21: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 21

Android Design Pattern

Model-View-Presenter (MVP)

Page 22: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 22

What is MVP?

• The MVP pattern allows separate the presentation layer(UI) from the business logic

Page 23: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 23

Why use MVP?

• Android activities are closely coupled to both interface and data.– Such as: CursorAdapter (View)+Cursor(data)

• Application to be easily extensible and maintainable.– Such as: changing the data access (from local

database to web service).• Easier to test each interface alone.

Page 24: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 24

How to implement MVP ?

• MVP architecture consists of 3 layers:– View– Presenter– Model

View Presenter Model

User Event Update Model

Update view State Changed event

Page 25: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 25

MVP: VIEW

• Activity, Fragment, View …• Has a reference to the Presenter• Propagates evens from UI to the Presenter.

(such as onClick)• Exposes methods that controls the

presentation objects. (Such as Show/Hide progress bar)

Page 26: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 26

MVP: PRESENTER

• Middle-man Between the View and Presenter• Has reference to View and Model.• Introduce a level of abstraction to the data

coming from the model, and formats it before sending it to the view (this makes the View and Model independent).

• Updates the UI, the difference to the MVC.

Page 27: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 27

MVP: Model (Interactor)

• Gateway towards the business logic.• Contains methods to for data retrival.

Page 28: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 28

MVP Conventions

• MVP– Views: ExampleView– Listeners: ExampleListener– Presenters: ExamplePresenter• > impl: ExamplePresenterImpl

– Interactors: ExampleInteractor• > impl: ExmpleInteractorImpl

Page 29: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 29

MVP Example

Activity Presenter NetworkInteractor

Press Login Button valiadteAccount()

loginSuccess() networkSuccess()

loginFailue() networkFailure()

Page 30: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 30

MVP Example: Login

Page 31: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 31

Login Sequence LoginActivity PresenterImpl

(onLoginFinsishedListener)InteractorImpl

attempLogin(..)validateCredentials(..)

onSuccess()navigateToMainActivity()

onError()

loginFaild()

loginTapped()Web

Service

Page 32: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 32

MVP vs. MVC

• MVP Pattern– View is more loosely coupled to the model. The

presenter is responsible for binding the model to the view.

– Easier to unit test because interaction with the view is through an interface

– Usually view to presenter map one to one. Complex views may have multi presenters.

Page 33: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 33

MVP architecture

Page 34: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 34

MVP vs. MVC

• MVC Pattern– Controller are based on behaviors and can be

shared across views– Can be responsible for determining which view to

display

Page 35: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 35

MVC architecture

Page 36: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 36

Some Advices

• Avoid expensive tasks on the UI thread.• Avoid code duplicity• Use themes, styles and colors properly.• Think in UI layer as isolated domain.• Write testable code and test it.• Measure the UI performance using the

performance measure tools.

Page 37: Android Effective UI: Tips, Tricks and Patterns

adhamenaya.com 37

Any Questions?

Contact me: [email protected]