Slide 1

Slide 1 text

Finding the view Hadi Tok CitizenMe @hadi_tok

Slide 2

Slide 2 text

findViewById(int id)

Slide 3

Slide 3 text

View.findViewById(int id) /** * Finds the first descendant view with the given ID, the view itself if * the ID matches getId(), or null if the ID is invalid */

Slide 4

Slide 4 text

View.findViewById(int id) Generally used to access views generated with LayoutInflater

Slide 5

Slide 5 text

LayoutInflater

Slide 6

Slide 6 text

LayoutInflater /** * Instantiates a layout XML file into its corresponding View */

Slide 7

Slide 7 text

@Override public void setContentView(@LayoutRes int layoutResID ) { getDelegate().setContentView( layoutResID ); }

Slide 8

Slide 8 text

LayoutInflater.from(mContext).inflate( layoutResID , contentParent);

Slide 9

Slide 9 text

final XmlResourceParser parser = res.getLayout( layoutResID );

Slide 10

Slide 10 text

final String name = parser .getName();

Slide 11

Slide 11 text

clazz = mContext.getClassLoader().loadClass( prefix != null ? (prefix + name) : name ).asSubclass(View.class);

Slide 12

Slide 12 text

constructor = clazz .getConstructor(mConstructorSignature);

Slide 13

Slide 13 text

final View view = constructor .newInstance(args);

Slide 14

Slide 14 text

root.addView( view , params);

Slide 15

Slide 15 text

findViewById(int id)

Slide 16

Slide 16 text

@Override public T findViewById(@IdRes int id) { return getDelegate().findViewById( id ); }

Slide 17

Slide 17 text

return (T) mWindow.findViewById( id );

Slide 18

Slide 18 text

return getDecorView().findViewById( id );

Slide 19

Slide 19 text

if ( id == mID) { return (T) this; }

Slide 20

Slide 20 text

for (int i = 0; i < len; i++) { View v = mChildren[i]; v = v.findViewById( id ); if (v != null) { return (T) v; } } return null;

Slide 21

Slide 21 text

Data Binding

Slide 22

Slide 22 text

Data Binding Allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically

Slide 23

Slide 23 text

Data Binding Allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically

Slide 24

Slide 24 text

Data Binding Allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically

Slide 25

Slide 25 text

https://viblo.asia/p/understanding-data-bindings-generated-code-and-how-does-android-data-binding-compiler-work-Ljy5Vd1yZra

Slide 26

Slide 26 text

public class MainActivity extends AppCompatActivity { @Inject MainViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding bind = DataBindingUtil.setContentView(this, R.layout.activity_main); bind.setViewModel(viewModel); } }

Slide 27

Slide 27 text

public class MainViewModel extends ViewModel { public final ObservableField text = new ObservableField<>(); }

Slide 28

Slide 28 text

https://viblo.asia/p/understanding-data-bindings-generated-code-and-how-does-android-data-binding-compiler-work-Ljy5Vd1yZra

Slide 29

Slide 29 text

Slide 30

Slide 30 text

false

Slide 31

Slide 31 text

public class ActivityMainBinding extends android.databinding.ViewDataBinding { … /** * Where the magic happens */ }

Slide 32

Slide 32 text

Kotlin Android Extensions(Synthetics)

Slide 33

Slide 33 text

Kotlin Android Extensions plugin Allows us to obtain the same experience we have with some of these libraries, without having to add any extra code.

Slide 34

Slide 34 text

apply plugin: 'kotlin-android-extensions'

Slide 35

Slide 35 text

androidExtensions { experimental = true }

Slide 36

Slide 36 text

res/layout/activity_free.xml

Slide 37

Slide 37 text

res/layout/activity_free.xml import kotlinx.android.synthetic.activity_free.*

Slide 38

Slide 38 text

res/layout/activity_free.xml import kotlinx.android.synthetic.activity_free.* textView.text = “DevFest"

Slide 39

Slide 39 text

import kotlinx.android.synthetic.main.activity_splash.* class PresentationActivity: BaseActivity() { override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { super.onCreate(savedInstanceState, persistentState) setContentView(R.layout.activity_splash) logoIv.visibility = View.VISIBLE } }

Slide 40

Slide 40 text

public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); this.setContentView(-1300174); ImageView var10000 = (ImageView)this._$_findCachedViewById(id.logoIv); Intrinsics.checkExpressionValueIsNotNull(var10000, "logoIv"); var10000.setVisibility(0); }

Slide 41

Slide 41 text

private HashMap _$_findViewCache; public View _$_findCachedViewById(int var1) { if (this._$_findViewCache == null) { this._$_findViewCache = new HashMap(); } View var2 = (View)this._$_findViewCache.get(var1); if (var2 == null) { var2 = this.findViewById(var1); this._$_findViewCache.put(var1, var2); } return var2; }

Slide 42

Slide 42 text

View Binding

Slide 43

Slide 43 text

View Binding Is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.

Slide 44

Slide 44 text

android { ... viewBinding { enabled = true } }

Slide 45

Slide 45 text

Slide 46

Slide 46 text

private lateinit var binding: ResultProfileBinding @Override fun onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) binding = ResultProfileBinding.inflate(layoutInflater) setContentView(binding.root) }

Slide 47

Slide 47 text

binding.name.text = viewModel.name binding.button.setOnClickListener { viewModel.userClicked() }

Slide 48

Slide 48 text

Jetpack Compose

Slide 49

Slide 49 text

Jetpack Compose Android’s modern toolkit for building native UI

Slide 50

Slide 50 text

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Greeting("Android") } } } @Composable fun Greeting(name: String) { Text(text = "Hello $name!") }

Slide 51

Slide 51 text

Questions?

Slide 52

Slide 52 text

Hadi Tok, CitizenMe @hadi_tok Thank you!