In Android Studio, Java files are an essential part of Android app
development because they contain the code that defines the behavior and
logic of your application.
Below is a simple example of a Java file in an Android Studio project. Suppose you have an activity named MainActivity.
Java file "MainActivity.kt" looks like this:
package com.microappvalley.newtestproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Package Declaration:
package com.microappvalley.newtestproject;
Imports:
The import statement includes the required classes and packages.
This example imports classes related to Android development.
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
onCreate Method:
You
can use this method to perform initialization tasks, set up the user
interface, and perform other necessary operations when creating an
activity.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}