Slide 1

Slide 1 text

Efficient Sad Puppy Layouts Dan Lew Image: https://commons.wikimedia.org/wiki/File:Sad-pug.jpg

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

$$$

Slide 4

Slide 4 text

ViewGroups

Slide 5

Slide 5 text

RelativeLayout ConstraintLayout LinearLayout FrameLayout Complex Simple

Slide 6

Slide 6 text

RelativeLayout / ConstraintLayout • Position views relative to each other • RelativeLayout: Slow • ConstraintLayout: Alpha

Slide 7

Slide 7 text

LinearLayout • Stack views vertically/horizontally • Weight distribution

Slide 8

Slide 8 text

FrameLayout • Positioning based on parent bounds • Overlapping Views • Clickable item backgrounds • Toggle container

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text


 
 
 
 


Slide 12

Slide 12 text


 
 
 
 


Slide 13

Slide 13 text


 
 
 
 


Slide 14

Slide 14 text

public class AvatarView extends FrameLayout {
 
 ImageView icon;
 TextView initials;
 
 public AvatarView(Context context, AttributeSet attrs) {
 super(context, attrs);
 
 LayoutInflater.from(context).inflate(R.layout.view_avatar, this);
 
 icon = (ImageView) findViewById(R.id.icon);
 initials = (TextView) findViewById(R.id.initials);
 }
 
 public void bind(Member member) {
 // ...Load icon into ImageView...
 // OR
 // ...Setup initials in TextView...
 }
 }

Slide 15

Slide 15 text


 
 
 
 
 


Slide 16

Slide 16 text

AvatarView (FrameLayout) FrameLayout ImageView TextView

Slide 17

Slide 17 text

public class AvatarView extends FrameLayout {
 
 ImageView icon;
 TextView initials;
 
 public AvatarView(Context context, AttributeSet attrs) {
 super(context, attrs);
 
 LayoutInflater.from(context).inflate(R.layout.view_avatar, this);
 
 icon = (TextView) findViewById(R.id.icon);
 initials = (TextView) findViewById(R.id.initials);
 }
 
 public void bind(Member member) {
 // ...Load icon into ImageView...
 // OR
 // ...Setup initials in TextView...
 }
 }

Slide 18

Slide 18 text


 
 
 
 
 


Slide 19

Slide 19 text

AvatarView (Framelayout) ImageView TextView

Slide 20

Slide 20 text

Styles

Slide 21

Slide 21 text

Styles • No style • Style 
 
 <item name="android:background">#FF0000</item>


Slide 22

Slide 22 text

Efficient • Semantically identical Views • All styled Views should change at once

Slide 23

Slide 23 text

Not Efficient • Single-use styles • Coincidentally using the same attributes 
 


Slide 24

Slide 24 text

Not Efficient • Single-use styles • Coincidentally using the same attributes 
 


Slide 25

Slide 25 text

Not Efficient • Single-use styles • Coincidentally using the same attributes 
 


Slide 26

Slide 26 text

static final int NUM_COLUMNS = 3;
 
 static final int NUM_RETRIES = 3; static final int NUM_THREE = 3;

Slide 27

Slide 27 text

// static final int NUM_COLUMNS = 3;
 
 // static final int NUM_RETRIES = 3;
 static final int NUM_THREE = 3;

Slide 28

Slide 28 text

Themes

Slide 29

Slide 29 text

Themes • Affect multiple Views at once • Default styles • Configure system-created Views

Slide 30

Slide 30 text

• Application • Activity • View

Slide 31

Slide 31 text

AppCompat • Material on all devices • Baseline themes/styles • Enables View theming pre-Lollipop

Slide 32

Slide 32 text


 <item name="colorPrimary">#F00</item>
 <item name="colorPrimaryDark">#0F0</item>
 <item name="colorControlNormal">#00F</item>


Slide 33

Slide 33 text


 <item name="buttonStyle">@style/MyButton</item>
 <item name="android:spinnerItemStyle">@style/MySpinnerItem</item>
 
 <item name="android:textAppearance">@style/MyText</item>
 <item name="android:textAppearanceInverse">@style/MyTextInverse</item>


Slide 34

Slide 34 text


 <item name="selectableItemBackground">@drawable/bg</item>
 


Slide 35

Slide 35 text

Resources Image: https://www.flickr.com/photos/mcerasoli/6261957970

Slide 36

Slide 36 text

v24 port xxxhdpi w411dp h731dp en_US

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Resource Qualifier System • Define alternative resources for device configurations • Android automatically picks correct resource

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

values-sw600dp-port values-port values-sw600dp values not sw600dp or portrait not sw600dp not portrait

Slide 44

Slide 44 text

Resources as code • Resource == parameter • Parameter <-- device configuration

Slide 45

Slide 45 text

int square() {
 return 8 * 8;
 }
 int squareLarge() {
 return 16 * 16;
 } int square(int num) {
 return num * num;
 } VS

Slide 46

Slide 46 text

getResources().getBoolean(R.bool.is_portrait) false true

Slide 47

Slide 47 text

setContentView(R.layout.activity_main)

Slide 48

Slide 48 text


 
 
 
 
 
 
 


Slide 49

Slide 49 text


 
 
 
 
 
 
 


Slide 50

Slide 50 text

Slide 51

Slide 51 text

24sp 16sp

Slide 52

Slide 52 text


 <item name="android:textSize">24sp</item> <item name="android:textColor">#FF00FF</item>
 
 <item name="android:textSize">16sp</item> <item name="android:textColor">#FF00FF</item>


Slide 53

Slide 53 text


 <item name="android:textSize">@dimen/welcome_text_size</item>
 <item name="android:textColor">#FF00FF</item>
 24sp 16sp

Slide 54

Slide 54 text

activity_main.xml TextView style=@style/WelcomeText some_include.xml some_include.xml portrait default textSize=16sp textSize=24sp sw600dp default

Slide 55

Slide 55 text

Drawables Image: https://www.flickr.com/photos/suckamc/3047183157

Slide 56

Slide 56 text

Image: https://www.flickr.com/photos/ufv/8042499199 Design “I need this”

Slide 57

Slide 57 text

Design “Not enough” “I tweaked the color,
 here’s those assets again.”

Slide 58

Slide 58 text

Assets as code

Slide 59

Slide 59 text

Drawable XML • Built into Android • Simple shapes • State selectors • Layer lists

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 62

Slide 62 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 63

Slide 63 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 64

Slide 64 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 65

Slide 65 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 66

Slide 66 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 67

Slide 67 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 68

Slide 68 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 69

Slide 69 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 70

Slide 70 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 71

Slide 71 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 72

Slide 72 text


 
 
 
 
 
 
 
 
 
 
 
 Button Selector (v21)

Slide 73

Slide 73 text


 
 
 
 
 
 
 
 
 
 
 
 Button Selector (v21)

Slide 74

Slide 74 text


 
 
 
 
 
 
 
 
 
 
 
 Button Selector (v21)

Slide 75

Slide 75 text


 
 
 
 
 
 
 
 
 
 
 
 Button Selector (v21)

Slide 76

Slide 76 text

Button Versions button_welcome_outline.xml button_welcome.xml button_welcome.xml (with ripple)

Slide 77

Slide 77 text

Vector drawables

Slide 78

Slide 78 text

VectorDrawable != SVG Design :(

Slide 79

Slide 79 text

SVG -> VectorDrawable • Android Studio: New Vector Asset • Victor: github.com/trello/victor android {
 sourceSets {
 main {
 svg.srcDir 'src/main/svg'
 }
 }
 }

Slide 80

Slide 80 text

vs

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Tinting Images • XML • Simple drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); • Comprehensive Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
 DrawableCompat.setTint(wrappedDrawable, color); Not backwards compatible

Slide 83

Slide 83 text

Thank You! • @danlew42 • danlew.net • speakerdeck.com/dlew • github.com/dlew