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 Guide to Understanding Android Apps and Features

Android is a versatile operating system that powers billions of devices worldwide, ranging from smartphones and tablets to wearables and TVs. If you are new to Android or looking to explore its features, this article will present a variety of Android examples that showcase its capabilities. Whether you are a developer looking for coding examples or a user curious about Android features, you’ll find something valuable here.

1. Basic Android App Example – Hello World

The most basic example of an Android app is the classic "Hello World" application. It’s a great starting point for anyone learning Android development.

Steps to Create a Hello World App:

  • Step 1: Install Android Studio and create a new project.

  • Step 2: Choose the Basic Activity template.

  • Step 3: Open res/layout/activity_main.xml and add the following code:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:textSize="24sp"
        android:layout_centerInParent="true"/>
    
  • Step 4: In MainActivity.java, ensure the code looks like this:

    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 simple example will display the text "Hello, World!" on the screen when you run the app.

2. Button Click Event – Displaying a Message

One of the most common elements in Android apps is handling user interactions, such as button clicks. Let’s build a simple example where a button click shows a message.

Steps for Button Click Example:

  • Step 1: Modify the activity_main.xml file to include a button and a TextView:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click Me" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Message will appear here"
            android:textSize="18sp"
            android:layout_marginTop="20dp"/>
    </LinearLayout>
    
  • Step 2: In MainActivity.java, set up the button click event to change the TextView text:

    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 {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = findViewById(R.id.button);
            final TextView textView = findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView.setText("Hello, Android!");
                }
            });
        }
    }
    

This code demonstrates how to detect a button click and update the text on the screen.

3. Android RecyclerView Example – Displaying a List

A RecyclerView is used in Android to display a large set of data efficiently. It is one of the most common UI components when working with lists or grids.

Steps for RecyclerView Example:

  • Step 1: Create a new layout file (item_layout.xml) for individual list items:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="18sp" />
    
  • Step 2: Create a RecyclerView in your activity_main.xml:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
  • Step 3: Create a MainActivity.java file with a simple ArrayList and Adapter:

    package com.example.recyclerviewexample;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RecyclerView recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
            ArrayList<String> data = new ArrayList<>();
            data.add("Item 1");
            data.add("Item 2");
            data.add("Item 3");
            data.add("Item 4");
            data.add("Item 5");
    
            RecyclerView.Adapter adapter = new RecyclerView.Adapter() {
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
                    return new RecyclerView.ViewHolder(view) {};
                }
    
                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                    ((TextView) holder.itemView).setText(data.get(position));
                }
    
                @Override
                public int getItemCount() {
                    return data.size();
                }
            };
            recyclerView.setAdapter(adapter);
        }
    }
    

This example demonstrates how to display a list of items using RecyclerView.

4. Android Shared Preferences Example

Shared Preferences in Android allow you to store small amounts of data, such as user preferences or settings.

Steps for Shared Preferences Example:

  • Step 1: In MainActivity.java, create code to save and retrieve data using Shared Preferences:
    package com.example.sharedpreferencesexample;
    
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String PREFS_NAME = "MyPrefsFile";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final EditText editText = findViewById(R.id.editText);
            final TextView textView = findViewById(R.id.textView);
    
            SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
            String savedText = sharedPreferences.getString("user_text", "No text saved");
            textView.setText(savedText);
    
            findViewById(R.id.saveButton).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String textToSave = editText.getText().toString();
                    SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
                    editor.putString("user_text", textToSave);
                    editor.apply();
                    textView.setText(textToSave);
                }
            });
        }
    }
    

This example stores a string entered by the user in Shared Preferences and displays it even after the app is closed and reopened.


5. Android WebView Example

A WebView in Android is used to display web content directly within an app.

Steps for WebView Example:

  • Step 1: In activity_main.xml, add a WebView:

    <android.webkit.WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
  • Step 2: In MainActivity.java, load a website in the WebView:

    package com.example.webviewexample;
    
    import android.os.Bundle;
    import android.webkit.WebView;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            WebView webView = findViewById(R.id.webView);
            webView.loadUrl("https://www.example.com");
        }
    }
    

This example loads a webpage directly inside the Android app using WebView.


Conclusion

These Android examples highlight a variety of features and concepts that every Android developer should understand. From creating a basic app to working with advanced components like RecyclerView, SharedPreferences, and WebView, these examples cover the essentials of Android development. Whether you're building your first app or looking to add advanced features, these examples are a great starting point to explore Android’s full potential.