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

Deflating the LayoutInflater

Deflating the LayoutInflater

Saket Narayan

August 28, 2016
Tweet

More Decks by Saket Narayan

Other Decks in Programming

Transcript

  1. Converts View hierarchies written in XML into Java objects XML

    <TextView android:layout_width="wrap_content" android:layout_height=“wrap_content" /> Java public class TextView extends View { ... }
  2. LayoutInflater#inflate() B. AttributeSet Figures precedence of attributes <style name="Button.Save"> <item

    name="android:layout_width">200dp</item> <item name="android:layout_height">200dp</item> </style>
  3. Finding a View’s source Creating the View object using Reflection


    LayoutInflater#inflate() C. Creating View Objects
  4. Finding a View’s source Creating the View object using Reflection


    LayoutInflater#inflate() C. Creating View Objects
  5. Delegate creation to “Factories” Instantiate View by itself (if #1

    fails) context.getClassLoader().loadClass(); Creating View Objects
  6. Delegate creation to “Factories” Instantiate View by itself a) Custom

    View 
 b) Framework View Creating View Objects
  7. a) Custom View: Class<TextView> clazz = ClassLoader#loadClass( "android.widget.TextView" ); TextView

    view = clazz.getConstructor().newInstance(); Creating View Objects
  8. example, AppCompat AppCompatViewInflater.java public View onCreateView(String viewName, ...) { if

    ("TextView".equals(viewName) { return new AppCompatTextView(); } ... }
  9. example, Calligraphy How it applies custom fonts without using any

    custom Views. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" fontPath="fonts/Roboto-Bold.ttf" />
  10. example, Calligraphy @Override View onCreateView(String name, AttributeSet) { View view

    = super.onCreateView(...); if (view instanceOf TextView) { applyTypeface((TextView) view); } return view; }
  11. example, Calligraphy @Override View onCreateView(String name, AttributeSet) { View view

    = super.onCreateView(...); if (view instanceOf TextView) { applyTypeface((TextView) view); } return view; }
  12. Balloon class FormatterAttrListener implements BalloonAttributeListener <> { @Override void onApplyAttribute(EditText

    view, int attrResId, String attrValue) { if ("IndianRupees".equals(attrValue)) { // Add rupee symbol and format digits } } }
  13. Challenges BalloonContext @Override Object getSystemService(String name) { if (LAYOUT_INFLATER_SERVICE.equals(name)) {

    return new BalloonLayoutInflater(); } return super.getSystemService(name); }
  14. Challenges BalloonContext @Override Object getSystemService(String name) { if (LAYOUT_INFLATER_SERVICE.equals(name)) {

    return new BalloonLayoutInflater(); } return super.getSystemService(name); }
  15. Learnings Why non-framework Views require their fully qualified names in

    XML whereas framework Views don’t. <is.uncommon.FancyCustomView /> vs. <TextView />