Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Build a ChatGPT Like App in Android using OpenAI API?

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 23 Jan, 2023
Improve Article
Save Article

Chat GPT is nowadays one of the famous AI tools which are like a chatbot. This chatbot answers all the queries which are sent to it. In this article, we will be building a simple ChatGPT-like android application by integrating the OpenAI API(ChatGPT) where we can ask any question and get an appropriate answer.

How to Make ChatGPT Like Android Application

We have created a sample application and will take a look at its output of it and then we will proceed further to creating a new project in android studio. 

Step-By-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add the below dependency in your build.gradle file

Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section. We have used the Picasso dependency for image loading from the URL.  

// below line is used for volley library
implementation ‘’

After adding this dependency sync your project and now move toward the AndroidManifest.xml part.  

Step 3: Adding permissions to the internet in the AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it. 


<!--permissions for INTERNET-->
<uses-permission android:name="android.permission.INTERNET"/>

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
            <!-- text view for displaying question-->
                android:textSize="17sp" />
            <!-- text view for displaying response-->
                android:textSize="15sp" />
    <!-- text field for asking question-->
        android:hint="Enter your query"
            android:textSize="14sp" />

Step 5: Generating bearer token for using the API. 

Navigate to the below URL and simply sign up with your email and password. On this screen click on Create a new secret key to generate your new key. Once your key is generated that we have to use it as a token for making our API key.

Step 6: Working with MainActivity.kt file. 

Navigate to app > java > your app’s package name > MainActivity.kt file and add the below code to it. Comments are added in the code to get to know it in detail. 


import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import android.widget.Toast
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
    // creating variables on below line. 
    lateinit var responseTV: TextView
    lateinit var questionTV: TextView
    lateinit var queryEdt: TextInputEditText
    override fun onCreate(savedInstanceState: Bundle?) {
        // initializing variables on below line. 
        responseTV = findViewById(
        questionTV = findViewById(
        queryEdt = findViewById(
        // adding editor action listener for edit text on below line. 
        queryEdt.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                // setting response tv on below line. 
                responseTV.text = "Please wait.."
                // validating text 
                if (queryEdt.text.toString().length > 0) {
                    // calling get response to get the response.
                } else {
                    Toast.makeText(this, "Please enter your query..", Toast.LENGTH_SHORT).show()
                return@OnEditorActionListener true
    private fun getResponse(query: String) {
        // setting text on for question on below line. 
        questionTV.text = query
        // creating a queue for request queue.
        val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
        // creating a json object on below line. 
        val jsonObject: JSONObject? = JSONObject()
        // adding params to json object. 
        jsonObject?.put("model", "text-davinci-003")
        jsonObject?.put("prompt", query)
        jsonObject?.put("temperature", 0)
        jsonObject?.put("max_tokens", 100)
        jsonObject?.put("top_p", 1)
        jsonObject?.put("frequency_penalty", 0.0)
        jsonObject?.put("presence_penalty", 0.0)
        // on below line making json object request. 
        val postRequest: JsonObjectRequest =
            // on below line making json object request. 
            object : JsonObjectRequest(Method.POST, url, jsonObject,
                Response.Listener { response ->
                    // on below line getting response message and setting it to text view. 
                    val responseMsg: String =
                    responseTV.text = responseMsg
                // adding on error listener 
                Response.ErrorListener { error ->
                    Log.e("TAGAPI", "Error is : " + error.message + "\n" + error)
                }) {
                override fun getHeaders(): kotlin.collections.MutableMap<kotlin.String, kotlin.String> {
                    val params: MutableMap<String, String> = HashMap()
                    // adding headers on below line. 
                    params["Content-Type"] = "application/json"
                    params["Authorization"] =
                        "Bearer Enter your token here"
                    return params;
        // on below line adding retry policy for our request. 
        postRequest.setRetryPolicy(object : RetryPolicy {
            override fun getCurrentTimeout(): Int {
                return 50000
            override fun getCurrentRetryCount(): Int {
                return 50000
            override fun retry(error: VolleyError) {
        // on below line adding our request to queue. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!