Create new empty activity in android studio
Here's a step-by-step guide to create a new empty activity in Android Studio:
Method 1: Using the "New Activity" Wizard
- Open Android Studio and create a new project or open an existing one.
- Click on the "File" menu and select "New" > "Activity" > "Empty Activity".
- In the "New Android Activity" dialog, enter the following details:
- Activity name: Enter a name for your activity, e.g., "MyActivity".
- Package name: Enter the package name for your activity, e.g., "com.example.myapp".
- Save location: Choose the location where you want to save your activity, e.g., "app/src/main/java".
- Language: Choose the programming language for your activity, e.g., "Java" or "Kotlin".
- Click "Next" to proceed.
- In the "Configure Activity" dialog, choose the following options:
- Activity type: Select "Empty Activity".
- Minimum SDK: Choose the minimum SDK version for your activity.
- Theme: Choose a theme for your activity, e.g., "Light" or "Dark".
- Click "Finish" to create the new activity.
Method 2: Using the "New" Menu
-
Open Android Studio and create a new project or open an existing one.
-
Click on the "File" menu and select "New" > "Java Class" (or "Kotlin Class" if you're using Kotlin).
-
In the "New Java Class" dialog, enter the following details:
- Class name: Enter a name for your activity, e.g., "MyActivity".
- Package name: Enter the package name for your activity, e.g., "com.example.myapp".
- Save location: Choose the location where you want to save your activity, e.g., "app/src/main/java".
-
Click "OK" to create the new activity.
-
In the "MyActivity.java" (or "MyActivity.kt") file, add the following code:
public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } }
-
Create a new layout file for your activity by right-clicking on the "app/src/main/res/layout" directory and selecting "New" > "Layout resource file".
-
Name the file "activity_my.xml" and add the following code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />