The MainActivity Class

Learn to implement the MainActivity of an image classification Android app that utilizes a TF Lite model to classify images.

The MainActivity class is the main activity of our app that extends the AppCompatActivity class. It overrides the onCreate() method to set up the UI, initializes the UI components, and handles user interactions.

Fields of the MainActivity

The fields of the MainActivity include:

  • A constant field to define the request code for loading an image.

  • A field (Button) to load images from the storage.

  • A field (ImageView) to view the image on the UI.

  • A field (Bitmap) to represent the selected image.

  • A field (of the type ImageClassifier) to perform image classification using a TF Lite model.

  • A field (TextView) to display the classification result.

  • A field (an instance of ActivityResultLauncher) handling the result of picking an image.

The following code defines these fields:

Get hands-on with 1200+ tech skills courses.