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

Facing the challenges of responsiveness aka Cus...

Facing the challenges of responsiveness aka Customize your View

Tired of writing multiple layout.xmls for different screen densities? There are alternatives.

You’ll learn how customizing Views can help easing development of responsive apps. Gain knowledge on when and how to write custom views. Learn how to get rid of includes, merges, code duplication and other nasty things.

Benjamin Weiss

October 24, 2013
Tweet

More Decks by Benjamin Weiss

Other Decks in Technology

Transcript

  1. Benjamin Weiss http://gplus.to/keyboardsurfer Twitter: @keyboardsurfer Senior Software Developer at ImmobilienScout24

    Organizer: GDG Android in Berlin Author of Crouton Co-Organizer of many Android events
  2. Agenda •  The challenge •  Basic approach •  Facing the

    issues •  Advancing o  values o  aliases o  entry points o  customization Benjamin Weiss
  3. The challenge Create a state of the art interface for

    an existing Android app. Benjamin Weiss
  4. The challenge Create a state of the art interface for

    an existing Android app. Oh, and it should also be look and feel good on different device categories. Benjamin Weiss
  5. Entry point based on device config final boolean isPhone =

    context.getResources() .getBoolean(R.bool.is_phone); final Class startClass; if (isPhone) { startClass = mypackage.phone.MyActivity.class; } else { startClass = mypackage.tablet.MyActivity.class; } Benjamin Weiss
  6. Change menus from code @Override public void onCreateOptionsMenu(Menu menu) {

    ... MenuItem myItem = menu.findItem(R.id.my_item); if (!isPhone) { myItem.setShowAsActionFlags(SHOW_AS_ACTION_WITH_TEXT); } else { myItem.setShowAsActionFlags(SHOW_AS_ACTION_IF_ROOM); } ... } Benjamin Weiss
  7. Adjusting orientation public class AttributeView extends LinearLayout { public AttributeView(...)

    { ... setOrientation(isPhone ? HORIZONTAL : VERTICAL); ... } } Benjamin Weiss
  8. And reference them public class MyFragment extends Fragment { ...

    @Override public View onCreateView(...) { return inflater.inflate(R.layout.my_layout, null); } Benjamin Weiss
  9. And reference them public class MyFragment extends Fragment { ...

    @Override public View onCreateView(...) { return inflater.inflate(R.layout.my_layout, null); } Benjamin Weiss
  10. Roll your own public final class Separator extends View {

    ... } Single point of concern Benjamin Weiss
  11. Roll your own public final class Separator extends View {

    ... } Can also be instantiated from code Benjamin Weiss
  12. Don’t be mindless Keep the complexity low Create base layouts

    Reuse layouts if possible Use common names Think of future generations Benjamin Weiss
  13. Links & Image Sources London from Space (http://goo.gl/E7BDHp) Tablet optimization

    (http://goo.gl/eMnoNb) Device art (http://goo.gl/jXjwrX) Device metrics(http://goo.gl/W7CDK9) First bug (http://goo.gl/NxuJKa) Underground (http://goo.gl/w7l6fb) Way out (http://goo.gl/g8kQsZ) This background (http://goo.gl/Q24R8a) Mind the Gap (http://goo.gl/dUpHbD) Benjamin Weiss