Create new class android studio with oncreate
Here are the steps to create a new class in Android Studio with an onCreate
method:
Step 1: Open Android Studio
Open Android Studio on your computer.
Step 2: Create a new class
In the Android Studio menu, go to File > New > Java Class (or Kotlin Class if you prefer Kotlin).
Step 3: Name the class
In the "New Java Class" dialog box, enter a name for your class, such as MyActivity
. Make sure to select the correct package name and location.
Step 4: Choose the superclass
Select the superclass for your class. In this case, choose android.app.Activity
(or android.app.Application
if you want to create an application class).
Step 5: Create the class
Click OK to create the new class. Android Studio will generate the basic structure for your class.
Step 6: Add the onCreate
method
In the MyActivity.java
file (or MyActivity.kt
if you chose Kotlin), add the following code:
package com.example.myapp;
import android.os.Bundle;
import android.app.Activity;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add your code here
}
}
Or in Kotlin:
package com.example.myapp
import android.os.Bundle
import android.app.Activity
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Add your code here
}
}
Step 7: Save the file
Save the file by clicking File > Save (or press Ctrl+S
on Windows or Cmd+S
on Mac).
That's it! You have now created a new class in Android Studio with an onCreate
method. You can add your own code to the onCreate
method to perform any initialization or setup tasks for your activity.