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

Finding the View

Hadi Tok
November 24, 2019
110

Finding the View

On Android, there are many ways to interact with the views created with the XML files. This presentation reviews how things work in the background.

Hadi Tok

November 24, 2019
Tweet

Transcript

  1. 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 */
  2. @Override public <T extends View> T findViewById(@IdRes int id) {

    return getDelegate().findViewById( id ); }
  3. for (int i = 0; i < len; i++) {

    View v = mChildren[i]; v = v.findViewById( id ); if (v != null) { return (T) v; } } return null;
  4. Data Binding Allows you to bind UI components in your

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

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

    layouts to data sources in your app using a declarative format rather than programmatically
  7. <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="viewModel" type="com.example.main.MainViewModel"

    /> </data> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@{ viewModel.text }" /> </layout> https://viblo.asia/p/understanding-data-bindings-generated-code-and-how-does-android-data-binding-compiler-work-Ljy5Vd1yZra
  8. 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); } }
  9. <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="viewModel" type="com.example.main.MainViewModel"

    /> </data> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@{ viewModel.text }" /> </layout> https://viblo.asia/p/understanding-data-bindings-generated-code-and-how-does-android-data-binding-compiler-work-Ljy5Vd1yZra
  10. <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Layout layout="activity_main" absoluteFilePath="/home/framgia/Projects/harpa-crista/harpacrista/ android/app/src/main/res/layout/activity_main.xml" directory="layout" isMerge="false"

    modulePackage="com.harpacrista"> <Variables declared="true" name="viewModel" type="com.example.main.MainViewModel"> <location endLine="8" endOffset="51" startLine="6" startOffset="8" /> </Variables> <Imports name="View" type="android.view.View"> <location endLine="10" endOffset="42" startLine="10" startOffset="8" /> </Imports> <Targets> <Target tag="layout/activity_main_0" view="TextView"> <Expressions> <Expression attribute="android:text" text=" viewModel.text "> <Location endLine="16" endOffset="41" startLine="16" startOffset="8" /> <TwoWay>false</TwoWay> <ValueLocation endLine="16" endOffset="39" startLine="16" startOffset="24" /> </Expression> </Expressions> <location endLine="16" endOffset="44" startLine="14" startOffset="4" /> </Target> </Targets> </Layout>
  11. 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.
  12. 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 } }
  13. 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); }
  14. 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; }
  15. 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.
  16. <LinearLayout ... > <TextView android:id="@+id/name" /> <ImageView android:cropToPadding="true" /> <Button

    android:id="@+id/button" android:background="@drawable/rounded_button" /> </LinearLayout>
  17. private lateinit var binding: ResultProfileBinding @Override fun onCreate(savedInstanceState: Bundle) {

    super.onCreate(savedInstanceState) binding = ResultProfileBinding.inflate(layoutInflater) setContentView(binding.root) }
  18. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

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