Android Examples: A Beginner's Guide to Android Development
If you're new to Android development or just want to explore some basic examples, this guide will walk you through a variety of simple Android app examples. These examples will help you get a practical understanding of how to build Android apps using Java or Kotlin, and they will introduce you to common Android components such as Activities, Fragments, Views, and Intents.
1. Simple Hello World Application
One of the most basic Android app examples is a Hello World app. This app demonstrates how to set up a project and display a simple message on the screen.
Steps:
- Create a New Android Project in Android Studio.
- In your
activity_main.xmlfile, add a TextView:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp"
android:layout_centerInParent="true"/>
- In your
MainActivity.java(orMainActivity.ktfor Kotlin), ensure the following code to display the message:
package com.example.helloworld;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This basic app will simply show "Hello, World!" on the screen when you run the app.
2. Button Click Example
This example shows how to handle a Button click event in an Android app.
Steps:
- In the
activity_main.xmlfile, add a Button and a TextView:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You haven't clicked yet."
android:textSize="18sp"
android:layout_below="@id/button"
android:layout_marginTop="16dp"/>
- In the
MainActivity.java(orMainActivity.kt), add logic for the Button click event:
package com.example.buttonclick;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("You clicked the button!");
}
});
}
}
This example changes the TextView text when the user clicks the button.
3. Simple ListView Example
A ListView is used to display a list of items in a scrollable format. This example shows how to create a simple ListView in Android.
Steps:
- Add a ListView to your
activity_main.xml:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- In
MainActivity.java, create a list of items and set an ArrayAdapter to bind the data to theListView:
package com.example.listviewexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
This example creates a basic list with items such as "Item 1", "Item 2", etc.
4. Intent and Activity Navigation Example
Intents are used for navigating between different Activities. In this example, we’ll create two Activities and use an Intent to navigate from one to the other.
Steps:
- In the
activity_main.xmlfile of MainActivity, add a Button:
<Button
android:id="@+id/navigateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"
android:layout_centerInParent="true"/>
- In the
MainActivity.java, create an Intent to navigate to the SecondActivity:
package com.example.intentexample;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navigateButton = findViewById(R.id.navigateButton);
navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
- Create the SecondActivity and its layout. In
activity_second.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Second Activity!"
android:textSize="24sp"
android:layout_centerInParent="true"/>
- In
SecondActivity.java, display the message:
package com.example.intentexample;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
In this example, clicking the button in MainActivity takes the user to SecondActivity.
5. Simple SharedPreferences Example
SharedPreferences is used to store small amounts of key-value data in Android, such as user preferences. Here's an example of saving and retrieving data using SharedPreferences.
Steps:
- In
activity_main.xml, add a Button and a TextView:
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Data"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/dataTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data saved"
android:textSize="18sp"
android:layout_below="@id/saveButton"
android:layout_marginTop="16dp"/>
- In
MainActivity.java, save and retrieve data from SharedPreferences:
package com.example.sharedpreferencesexample;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView dataTextView;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataTextView = findViewById(R.id.dataTextView);
saveButton = findViewById(R.id.saveButton);
// Retrieve saved data
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String savedData = preferences.getString("dataKey", "No data saved");
dataTextView.setText(savedData);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString("dataKey", "Hello, SharedPreferences!");
editor.apply();
Toast.makeText(MainActivity.this, "Data Saved!", Toast.LENGTH_SHORT).show();
dataTextView.setText("Hello, SharedPreferences!");
}
});
}
}
In this example, clicking the "Save Data" button saves a string into SharedPreferences, which is displayed in the TextView.
Conclusion
These Android app examples cover a variety of fundamental Android concepts and components, such as:
- Displaying text in a TextView.
- Handling Button clicks to change UI elements.
- Creating a ListView to display lists of data.
- Navigating between Activities using Intents.
- Storing and retrieving data with SharedPreferences.
By following these examples, you’ll gain a good foundation for building more complex Android apps as you advance in your development journey. Each example introduces essential building blocks for Android development, making it easier to create functional and interactive apps.
0 Comments