Android Development Prompt Examples with Outputs to Enhance Your Skills

Android development is a dynamic field that offers numerous opportunities for developers to create innovative mobile applications. To improve your skills, practicing with practical prompts and reviewing their outputs can be highly beneficial. In this article, we explore several prompt examples designed to enhance your Android development expertise.

Prompt Example 1: Create a Simple Counter App

Develop an Android app that displays a number on the screen. When the user taps a button, the number increases by one. This exercise helps you understand basic UI components and event handling.

// MainActivity.java
package com.example.counterapp;

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 int count = 0;
    private TextView counterText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counterText = findViewById(R.id.counterText);
        Button incrementButton = findViewById(R.id.incrementButton);

        incrementButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                counterText.setText(String.valueOf(count));
            }
        });
    }
}

Prompt Example 2: Implement a Login Screen

Create a login screen with fields for username and password, and a login button. When the button is pressed, validate the input and display a message indicating success or failure. This task helps you practice input validation and UI design.

// activity_login.xml
<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">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:marginTop="20dp" />
</LinearLayout>
// MainActivity.java
package com.example.loginapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText username, password;
    private TextView message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        message = findViewById(R.id.message);
        Button loginButton = findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = username.getText().toString();
                String pass = password.getText().toString();

                if (user.equals("admin") && pass.equals("1234")) {
                    message.setText("Login successful!");
                } else {
                    message.setText("Invalid credentials.");
                }
            }
        });
    }
}

Prompt Example 3: List View of Items

Create an app that displays a list of items, such as fruits or countries. When an item is clicked, show a message with the item’s name. This helps you learn about RecyclerView and item click handling.

// item_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/itemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
</LinearLayout>
// MainActivity.java
package com.example.listapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List items = Arrays.asList("Apple", "Banana", "Cherry", "Date", "Elderberry");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ItemAdapter(items));
    }

    private class ItemAdapter extends RecyclerView.Adapter {
        private List data;

        ItemAdapter(List data) {
            this.data = data;
        }

        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.item_list, parent, false);
            return new ItemViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ItemViewHolder holder, int position) {
            String item = data.get(position);
            holder.itemName.setText(item);
            holder.itemView.setOnClickListener(v -> 
                Toast.makeText(MainActivity.this, "Clicked: " + item, Toast.LENGTH_SHORT).show()
            );
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView itemName;

        ItemViewHolder(View itemView) {
            super(itemView);
            itemName = itemView.findViewById(R.id.itemName);
        }
    }
}

Conclusion

Practicing with these prompt examples can significantly improve your Android development skills. Try modifying the code, adding new features, or creating your own projects based on these templates. Continuous practice and experimentation are key to mastering Android app development.