DEV Community

Rajkumar Chaudhary
Rajkumar Chaudhary

Posted on

Created Android App Using ChatGPT

Here's a step-by-step guide on how to create a simple Android app for Biketimes – a bike news app – using Android Studio and basic Android components. This example will cover essential aspects, including designing the UI, fetching news from a server (or hardcoded for simplicity), displaying lists, and navigating between screens. The code snippets below provide a foundation, which you can expand upon as needed.

Image description

Prerequisites

Install Android Studio: Make sure you have the latest version.
Create a New Project: Start a new project in Android Studio with an empty activity.

Step 1: Set Up Project and Dependencies

*1.1. Create a New Project
*

Open Android Studio, choose "New Project", and select Empty Activity.
Set the project name to Biketimes and select Kotlin as the language.

*1.2. Add Required Dependencies
*

To handle HTTP requests (for fetching bike news data) and JSON parsing, add dependencies like Retrofit and Gson. Open the build.gradle (app) file and add these dependencies:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'

Enter fullscreen mode Exit fullscreen mode

Sync the project after adding the dependencies.

Step 2: Set Up the User Interface

*2.1. Design the Main Layout
*

Open res/layout/activity_main.xml and create a RecyclerView to list the bike news.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

Enter fullscreen mode Exit fullscreen mode

*2.2. Create an Item Layout for News
*

Create a new layout file, res/layout/item_news.xml, to define each news item.

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

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/descriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="4dp" />
</LinearLayout>

Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up Data Models

Create a data class, BikeNews.kt, to represent each Biketimes news article.

data class BikeNews(
    val title: String,
    val description: String
)

Enter fullscreen mode Exit fullscreen mode

Step 4: Create the RecyclerView Adapter

Create a new Kotlin class, BikeNewsAdapter.kt, to bind the data to the RecyclerView.

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class BikeNewsAdapter(private val newsList: List<BikeNews>) : RecyclerView.Adapter<BikeNewsAdapter.NewsViewHolder>() {

    class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
        val descriptionTextView: TextView = itemView.findViewById(R.id.descriptionTextView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_news, parent, false)
        return NewsViewHolder(view)
    }

    override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
        val newsItem = newsList[position]
        holder.titleTextView.text = newsItem.title
        holder.descriptionTextView.text = newsItem.description
    }

    override fun getItemCount() = newsList.size
}

Enter fullscreen mode Exit fullscreen mode

Step 5: Create a Retrofit API Interface (Mock Data)

Create a new Kotlin interface, BikeNewsApi.kt, to define the API endpoint. In a real app, replace this with a real API. For now, let’s use mock data.

import retrofit2.Call
import retrofit2.http.GET

interface BikeNewsApi {
    @GET("bike_news")  // Replace with real endpoint
    fun getBikeNews(): Call<List<BikeNews>>
}

Enter fullscreen mode Exit fullscreen mode

Step 6: Fetch Data in MainActivity

In MainActivity.kt, initialize Retrofit, fetch data, and set up the RecyclerView.

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: BikeNewsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        fetchBikeNews()
    }

    private fun fetchBikeNews() {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.example.com/")  // Use actual base URL
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val api = retrofit.create(BikeNewsApi::class.java)
        api.getBikeNews().enqueue(object : Callback<List<BikeNews>> {
            override fun onResponse(call: Call<List<BikeNews>>, response: Response<List<BikeNews>>) {
                if (response.isSuccessful) {
                    response.body()?.let { newsList ->
                        adapter = BikeNewsAdapter(newsList)
                        recyclerView.adapter = adapter
                    }
                } else {
                    Toast.makeText(this@MainActivity, "Failed to load news", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<List<BikeNews>>, t: Throwable) {
                Toast.makeText(this@MainActivity, "Error: ${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 7: Run the App

Build and Run: Connect an Android device or start an emulator in Android Studio and run the app.

Test: Ensure that the app fetches and displays the bike news as expected.

Now live on Play Store: https://play.google.com/store/apps/details?id=com.biketimes.biketime

Billboard image

Monitor more than uptime.

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay