Hello Android Example with Android Studio
Syntax
Here is the basic syntax for creating a "Hello World" message in Android Studio:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Display "Hello World!" message
Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
}
}
Example
Here is an example of how to implement the "Hello World" message in Android Studio:
- Open Android Studio and create a new project.
- In the project explorer, navigate to app > java > com.example.yourappname > MainActivity.
- Replace the existing code with the code provided in the Syntax section.
- Run the app on an emulator or your Android device.
Output
When you run the app, a message saying "Hello World!" will appear on the screen, using the Android Toast class.
Explanation
This code creates an activity, which is the main entry point of the app. The onCreate()
method sets the layout of the activity to activity_main.xml
. Then, it uses the Toast.makeText()
method to create a pop-up message that displays the text "Hello World!".
Use
The "Hello World" example is a simple way to get started with Android development in Android Studio. It demonstrates how to create an activity, set a layout, and use the Toast
class to display a message.
Important Points
- The
onCreate()
method is called when the activity is created. - The
setContentView()
method sets the layout of the activity to the XML file specified. - The
Toast.makeText()
method creates a message that pops up on the screen. - The
Toast.LENGTH_LONG
parameter sets the duration of the message to be displayed.
Summary
In this example, we learned how to create a "Hello World" message in Android Studio using an activity, layout, and the Toast
class. This simple example is a great way to start learning Android development in Android Studio.