33
Coding for Different Android Screen sizes and Density By Robin Srivastava

Coding for different resolutions

Embed Size (px)

Citation preview

Page 1: Coding for different resolutions

Coding  for  Different  Android  Screen  sizes  and  Density  

By  Robin  Srivastava  

Page 2: Coding for different resolutions

Intended  Audience  

•  Developers  of  Android  apps  •  Any  technological  enthusiast  •  Any  enthusiast  

Page 3: Coding for different resolutions

Ques?ons  Answered    

•  What  to  ask  from  a  graphic  designer?  •  What  to  do  once  the  graphic  designer  gives  the  assets?  

•  How  to  create  screen  layouts  which  are  independent  of  screen-­‐size  and  screen-­‐density?  

•  What  screen-­‐sizes  and  densi?es  are  available?  •  Your  ques?ons  

Page 4: Coding for different resolutions

Why  is  this  so  important?  

Some  stats  first  

Page 5: Coding for different resolutions

Stat  1  –  Android  OS  Distribu?on  

Page 6: Coding for different resolutions

Stat  2  –  Screen  Size  and  Density  

Page 7: Coding for different resolutions

Stat  2  –  Screen  Size  and  Density  (contd..)  

Page 8: Coding for different resolutions

Back  to  the  previous  ques?on  

Why  is  it  so  important?  

•  Simple  mathema?cs  would  suggest  that  we  have  4  *  4  =  16  different  varia?ons  of  android  screen  size  and  density.    

•  Each  of  these  sizes  and  density  comes  in  a  range,  so  theore?cally  speaking  its  infinite  

•  Prac?cally,  out  of  16  there  are  only  12  varia?ons  to  consider  with  a  very  reasonable  devia?on  

Page 9: Coding for different resolutions

12  screen  varia?ons?  

•  Is  it  low  or  high?  •  Android  gives  an  op?on  to  have  different  folders  for  different  varia?ons.  

•  If  we  s?ck  to  it  religiously,  we  are  talking  about  having  12  set  of  graphics,  each  with  different  specs  

Page 10: Coding for different resolutions

12  screen  varia?ons?  

•  Is  it  low  or  high?  •  Android  gives  an  op?on  to  have  different  folders  for  different  varia?ons.  

•  If  we  s?ck  to  it  religiously,  we  are  talking  about  having  12  set  of  graphics,  each  with  different  specs  

A  small  change  of  direc?on  or  shadow  in  an  arrow  icon  will  require  changing/crea?ng  11  more  assets  

Page 11: Coding for different resolutions

What  we  didn’t  consider  in  the  previous  slides  

•  Large  TVs  •  Small  watches  

Page 12: Coding for different resolutions

What  to  ask  from  a  graphic  designer?  

•  One  word  answer  –  graphical  assets  J  

Page 13: Coding for different resolutions

What  to  ask  from  a  graphic  designer?  

•  One  word  answer  –  graphical  assets  J  •  But  the  real  ques?on  is,  do  we  really  need  that  asset  we  are  asking  for?  

Page 14: Coding for different resolutions

Some  ques?ons  to  ask  yourself  before  asking  for  assets  

•  Do  I  really  need  the  assets  to  display  a  kind  dialog  box?  

•  Those  fancy  texts,  do  I  really  need  an  image  for  it?  

•  Those  colored  lines  being  used,  do  I  really  need  them  as  a  separate  image?  

•  So  on  and  so  forth  

Page 15: Coding for different resolutions

Giving  Rich  Look  Using  Views  

Color  code  #33B5E5  Font  Name  –  Roboto  Color  Code  -­‐  #33B5E5  

Page 16: Coding for different resolutions

If  you  really  need  an  asset  

•  Is  it  some  basic  shape  or  intricate  image?  –  If  its  shape  try  to  use  ShapeDrawable  (demo),  else  

•  Take  the  PNG  •  Convert  it  into  9-­‐patch  

• hfp://goo.gl/hVr6  

Page 17: Coding for different resolutions

9-­‐patch  Image  

•  Stretchable  bitmap  •  Define  the  area  in  which  the  text  appears  

Page 18: Coding for different resolutions

9-­‐patch  

•  How  to  use  – Tool  demo  – App  demo  

Page 19: Coding for different resolutions

When  9-­‐patch  might  not  work  

•  Images  with  text  in  it  and  the  requirement  is  stretching  of  text  too  (without  making  it  look  bad)  

•  Images  which  requires  very  fine  shadowing  and  gradients  

•  Characters  and  cartoons  with  intricate  designs  

Page 20: Coding for different resolutions

When  9-­‐patch  might  not  work  (contd…)  

•  Need  different  images  for  different  resolu?on  

Page 21: Coding for different resolutions

Providing  Alternate  Resources  •  <resource-­‐name>  -­‐  <qualifier>  

•  Famous  ones  –  drawable-­‐hdpi  –  drawable-­‐mdpi  

•  Some  more    –  drawable-­‐fr-­‐hdpi  –  drawable-­‐sw600dp-­‐hdpi  –  drawable-­‐land  –  drawable-­‐long  –  drawable-­‐land-­‐v8  –  etc  

Page 22: Coding for different resolutions

For  the  en?re  list  

Open  developers’  manual  for  providing  resources  

Page 23: Coding for different resolutions

How  to  create  screen  size  and  density  independent  layout?  

•  First,  let’s  check  out  various  common  screen  sizes  and  densi?es  

Page 24: Coding for different resolutions

Some  terminologies  we  will  use  •  Screen  density  – Number  of  dots  per  inch  of  the  screen.  A  medium  density  device  has  an  average  of  160  dots-­‐per-­‐inch  

•  Resolu?on  –  Total  number  of  physical  pixels  

•  Density  independent  pixel  (dp  or  dip)  –  The  density-­‐independent  pixel  is  equivalent  to  one  physical  pixel  on  a  160  dpi  screen,  which  is  the  baseline  density  assumed  by  the  system  for  a  “medium”  density  screen  

–  px  =  dp  *  (dpi  /  160)  

Page 25: Coding for different resolutions

Screen  Sizes  and  Density  Ranges  

Page 26: Coding for different resolutions

Resolu?ons  in  the  form  of  DP  

•  Xlarge  –  960dp  x  720dp  •  Large  –  640dp  x  480dp  •  Normal  –  470dp  x  320dp  •  Small  –  426dp  x  320dp  

Page 27: Coding for different resolutions

Back  to  the  ques?on  

•  How  to  create  layouts  which  are  independent  of  screen  sizes  and  densi?es?  

 

DEMO  

Page 28: Coding for different resolutions

Some  Quick  Tips  

•  DO  NOT  use  px  as  the  unit  of  text  sizes  and  views  

•  Use  dp  while  giving  the  margins,  padding  or  the  dimension  of  a  view  

•  Give  the  text  size  is  sp  

Page 29: Coding for different resolutions

Some  Quick  Tips  

Header  

Footer  

Content   Use    Linear  Layout  

Page 30: Coding for different resolutions

Some  Quick  Tips  

When  alignment  rela?ve  to  other  components  mafer  –  use  Rela?ve  Layout    It  is  the  most  general  layout  which  can  be  used  for  laying  out  components  with  great  flexibility  

Page 31: Coding for different resolutions

Some  Quick  Tips  

Layers    Frame  Layout    

Page 32: Coding for different resolutions
Page 33: Coding for different resolutions

E-­‐mail  –  robin@paradigmcrea?ves.com