Android Examples .If you want to know about Android Examples , then this article is for you. You will find a lot of information about Android Examples in this article. We hope you find the information useful and informative. You can find more articles on the website.

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:

  1. Create a New Android Project in Android Studio.
  2. In your activity_main.xml file, 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"/>
  1. In your MainActivity.java (or MainActivity.kt for 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:

  1. In the activity_main.xml file, 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"/>
  1. In the MainActivity.java (or MainActivity.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:

  1. Add a ListView to your activity_main.xml:
<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  1. In MainActivity.java, create a list of items and set an ArrayAdapter to bind the data to the ListView:
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:

  1. In the activity_main.xml file 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"/>
  1. 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);
            }
        });
    }
}
  1. 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"/>
  1. 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:

  1. 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"/>
  1. 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:

  1. Displaying text in a TextView.
  2. Handling Button clicks to change UI elements.
  3. Creating a ListView to display lists of data.
  4. Navigating between Activities using Intents.
  5. 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.