Writing a “Hello World” app on an Android phone is a great way to get started with Android app development. This simple app displays a “Hello World” message on the screen when the app is launched. Here’s a step-by-step guide on how to create a “Hello World” app on an Android phone using Android Studio:
- Install Android Studio: Android Studio is the integrated development environment (IDE) used to create Android apps. Install Android Studio by downloading it from the official website and following the installation instructions.
- Create a new project: Once Android Studio is installed, open it and click on “Start a new Android Studio project” or go to File -> New -> New Project. Choose “Empty Activity” as the template and click “Next.”
- Configure your project: Enter a name for your project and choose a location to save it. Choose your preferred language (Java or Kotlin) and select “Phone and Tablet” as the form factor. Click “Finish” to create your new project.
- Add a TextView to the layout: In the “res/layout/activity_main.xml” file, add a TextView to the layout. You can do this by clicking on the “Design” tab and dragging a TextView from the “Palette” on the left to the layout. You can also switch to the “Text” tab and add the following XML code:
pythonCopy code<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
- Add code to display the message: In the MainActivity.java (or MainActivity.kt) file, add the following code to the onCreate() function:
Java:
scssCopy codeTextView messageTextView = findViewById(R.id.messageTextView);
messageTextView.setText("Hello World!");
Kotlin:
pythonCopy codeval messageTextView = findViewById<TextView>(R.id.messageTextView)
messageTextView.text = "Hello World!"
This code sets the text of the TextView to “Hello World!” when the activity is created.
- Build and run your app: Connect your Android phone to your computer and make sure USB debugging is enabled on your phone. In Android Studio, select your phone from the “Run” menu and click “Run” (or use the keyboard shortcut Shift + F10) to build and run your app on your phone. You should see the “Hello World!” message displayed on the screen.
Congratulations! You’ve just created a “Hello World” app on an Android phone using Android Studio. This is just the beginning of your Android app development journey. You can now explore other Android features and APIs to create more advanced apps.