Slide 1

Slide 1 text

Efficient Android Layouts Dan Lew

Slide 2

Slide 2 text

“Give me the place to stand, and I shall move the earth.” ~Archimedes

Slide 3

Slide 3 text

“Give me a standing desk, and I shall write an Android app.” ~Me

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

But I <3 RelativeLayout • LinearLayout == sometimes slow • RelativeLayout == always slow • ConstraintLayout == savior • Profile!

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text


 
 
 
 


Slide 13

Slide 13 text


 
 
 
 


Slide 14

Slide 14 text


 
 
 
 


Slide 15

Slide 15 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 16

Slide 16 text


 
 
 
 
 


Slide 17

Slide 17 text

AvatarView (FrameLayout) FrameLayout ImageView TextView

Slide 18

Slide 18 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 19

Slide 19 text


 
 
 
 
 


Slide 20

Slide 20 text

AvatarView (Framelayout) ImageView TextView

Slide 21

Slide 21 text

Custom Drawing

Slide 22

Slide 22 text

Step #1: onMeasure() (…sometimes you can skip this…)

Slide 23

Slide 23 text

onMeasure() • onMeasure() signature void onMeasure(int widthMeasureSpec, int heightMeasureSpec) • measureSpec - packed integer int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec);

Slide 24

Slide 24 text

MeasureSpec • EXACTLY - Must be that size • AT_MOST - Maximum width • UNDEFINED - Ideal width

Slide 25

Slide 25 text

onMeasure() protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int width; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { int desiredWidth = 500; // Whatever calculation you want if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } } // ...to be continued... }

Slide 26

Slide 26 text

onMeasure() @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width; int height; // ...Calculate width and height... setMeasuredDimension(width, height); }

Slide 27

Slide 27 text

Step #2: onDraw() (this is up to you)

Slide 28

Slide 28 text

Custom Drawables • onMeasure() -> getIntrinsicHeight() / getIntrinsicWidth() • onDraw() -> draw() • https://speakerdeck.com/cyrilmottier/mastering-android-drawables

Slide 29

Slide 29 text

Styles

Slide 30

Slide 30 text

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


Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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


Slide 33

Slide 33 text

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


Slide 34

Slide 34 text

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


Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Themes

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

• Application • Activity • View

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text


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


Slide 42

Slide 42 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 43

Slide 43 text


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


Slide 44

Slide 44 text

Resources

Slide 45

Slide 45 text

v24 port xxxhdpi w411dp h731dp en_US

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

setContentView(R.layout.activity_main)

Slide 57

Slide 57 text


 
 
 
 
 
 
 


Slide 58

Slide 58 text


 
 
 
 
 
 
 


Slide 59

Slide 59 text

Slide 60

Slide 60 text

24sp 16sp

Slide 61

Slide 61 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 62

Slide 62 text


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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Drawables

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Assets as code

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Button Outline 
 
 
 
 
 
 
 
 


Slide 71

Slide 71 text

Button Selector 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Slide 72

Slide 72 text


 
 
 
 
 
 
 
 
 
 
 
 Button Selector (v21)

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

Vector drawables

Slide 75

Slide 75 text

VectorDrawable != SVG Design :(

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

vs

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 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 80

Slide 80 text

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