Skip to main content

Posts

Showing posts with the label Android Overview Basic

Android Overview Basic

The Android WayfindViewById()One particularly jarring example of Android boilerplate code is the findViewById() method. Every time you want to access an Android view defined in your XML, you need to use this method, often with a typecast. For large Activities with many views, this can add a lot of code that does nothing but pull variables out of the xml.publicclassMainActivityextendsBaseActivity{ privateImageView logo; @OverridepublicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); logo = (ImageView) findViewById(R.id.logo); } } setContentView()In the code example above, we have the setContentView(R.layout.main) line. You need something like this in every Activity class, with the sole purpose of inflating your layout. It's not a big deal, but it is one extra step you have to go through when creating your Activity classes because it has to be put in exactly the right spot. It needs to be…