はじめに
はじめまして、記事主は2023年8月からAndroidエンジニアとして働いています。
最近個人開発を始めようと思い、Androidの開発環境を整えてみたのですが、ふとChatGPTに作成依頼したらどこまでのものができるのか興味が湧いたので、試してみたいと思います。
使用する言語はKotlinになります。
また、今回作成するアプリは簡単なToDoアプリです。
それではいきましょう。
まずは雑に依頼してみる
まず依頼した内容がこちら
なんとも雑ですね。回答がこちら
MainActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
private lateinit var adapter: ArrayAdapter<String>
private val todoList = ArrayList<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, todoList)
listView.adapter = adapter
fab.setOnClickListener { view ->
addTodo()
}
}
private fun addTodo() {
val todo = editText.text.toString()
if (todo.isNotEmpty()) {
todoList.add(todo)
adapter.notifyDataSetChanged()
editText.text.clear()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add a task..."
android:inputType="text"
android:maxLines="1"
android:layout_margin="16dp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@android:drawable/ic_input_add" />
</RelativeLayout>
これをそのままコピペするとやはりexception(エラー)がでました。流石に一発で作れません。
exceptionが消えるまで修正を加えます。
MainActivity(修正後)
package com.example.myapplication
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private lateinit var adapter: ArrayAdapter<String>
private val todoList = ArrayList<String>()
private lateinit var listView: ListView
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
listView = findViewById(R.id.listView)
editText = findViewById(R.id.editText)
val fab: FloatingActionButton = findViewById(R.id.fab)
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, todoList)
listView.adapter = adapter
fab.setOnClickListener {
addTodo()
}
}
private fun addTodo() {
val todo = editText.text.toString()
if (todo.isNotEmpty()) {
todoList.add(todo)
adapter.notifyDataSetChanged()
editText.text.clear()
}
}
}
activity_main.xml(修正後)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add a task..."
android:inputType="text"
android:maxLines="1"
android:layout_margin="16dp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText" />
</RelativeLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@android:drawable/ic_input_add"
android:contentDescription="TODO" />
</RelativeLayout>
完成
ここまでかかった時間約30分です。
少し手直しした部分はありますが、ほぼ彼の実力です。すごい
最後に
正直こんなに完成度高いものができると思っていませんでした。
皆さんもぜひChatGPTを使ってAndroidアプリ作ってみてください。