Upgrade to Pro — share decks privately, control downloads, hide ads and more …

android resources

android resources

for the ADI Devfest.

source code: https://github.com/mlc/android-resources-talk

mike castleman

February 04, 2013
Tweet

More Decks by mike castleman

Other Decks in Technology

Transcript

  1. . . . . . . . . . .

    . . . Resources on Android Mike Castleman Meetup ADI DevFest, 2013-02-04 Mike Castleman (Meetup) Android Resources DevFest 2013 1 / 16
  2. . . . . . . Fragmentation “Developing for Android

    is hard, because there are lots of different devices, and you have to test your app on every single one of them.” — People who never develop for Android Mike Castleman (Meetup) Android Resources DevFest 2013 2 / 16
  3. . . . . . . Resources Provide different assets

    for different devices Let the platform choose the right one Set things up declaritively: don’t write any extra code! Mike Castleman (Meetup) Android Resources DevFest 2013 3 / 16
  4. . . . . . . Resource Types drawable: images

    etc. layout values: strings, styles, themes, integers, arrays anim, color, menu xml, raw Mike Castleman (Meetup) Android Resources DevFest 2013 4 / 16
  5. . . . . . . Qualifiers OS Version: v9

    etc. Screen size: small, medium (phones); large, xlarge (tablets) Language (and country): en, es, en-rCA Portrait/landscape: port, land Screen density: ldpi, mdpi, hdpi, xhdpi Many others, see SDK docs if interested. Mike Castleman (Meetup) Android Resources DevFest 2013 5 / 16
  6. . . . . . . Directory structure Mike Castleman

    (Meetup) Android Resources DevFest 2013 6 / 16
  7. . . . . . . Use cases res/layout res/layout-land

    Mike Castleman (Meetup) Android Resources DevFest 2013 7 / 16
  8. . . . . . . Use cases res/values/styles.xml res/values-v11/styles.xml

    Mike Castleman (Meetup) Android Resources DevFest 2013 8 / 16
  9. . . . . . . Use cases res/values/strings.xml <string

    name="nice_phone">Nice phone!</string> <string name="nice_tablet">Nice tablet!</string> <string name="nice_device">@string/nice_phone</string> res/values-large/strings.xml <string name="nice_device">@string/nice_tablet</string> res/values-it/strings.xml <string name="nice_phone">Bel telefono!</string> <string name="nice_tablet">Bel tablet!</string> res/layout/activity.xml <TextView android:id="@+id/praise_user_device" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/nice_device"/> Mike Castleman (Meetup) Android Resources DevFest 2013 9 / 16
  10. . . . . . . Nononono public class TurnBlueOnTouch

    implements View.OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getActionMasked() == MotionEvent.ACTION_DOWN) v.setColor(Color.argb(255, 0, 0, 127)); else if (event.getActionMasked() == MotionEvent.ACTION_UP) v.setColor(Color.BLACK); return false; } } Mike Castleman (Meetup) Android Resources DevFest 2013 10 / 16
  11. . . . . . . Yesyesyesyes <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item

    android:state_pressed="true" android:color="#ff00007f" /> <item android:color="@color/black" /> </selector> Mike Castleman (Meetup) Android Resources DevFest 2013 11 / 16
  12. . . . . . . Images: Four “Abstract Densities”

    ldpi: 75% of base mdpi: base image size hdpi: 150% of base xhdpi: 200% of base ldpi mdpi hdpi xhdpi 48×48 64×64 96×96 128×128 Mike Castleman (Meetup) Android Resources DevFest 2013 12 / 16
  13. . . . . . . Images: Measurement Units px

    — physical device pixels dp — “density-independent pixel” On mdpi, 1 px = 1 dp. Not so on other densities! Devices vary both in their density and in their size in dp. Mike Castleman (Meetup) Android Resources DevFest 2013 13 / 16
  14. . . . . . . But! Android will automatically

    scale if you don’t have appropriate files. ldpi maybe irrelevant. Maybe just xhdpi if you’re feeling brave. Mike Castleman (Meetup) Android Resources DevFest 2013 14 / 16
  15. . . . . . . 9-Patch Images Mike Castleman

    (Meetup) Android Resources DevFest 2013 15 / 16
  16. . . . . . . Thank You Questions? Also,

    we’re hiring, interns and full-time positions: [email protected] Mike Castleman (Meetup) Android Resources DevFest 2013 16 / 16