Android Chat Application Development with Turing API

<TextView android:id="@+id/message_text" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_below="@+id/timestamp" android:layout_marginTop="10dp" android:layout_marginRight="5dp" android:layout_marginLeft="50dp" android:layout_toLeftOf="@+id/avatar_image" android:gravity="center_vertical" android:textSize="17sp" android:background="@drawable/chat_bubble_normal" android:padding="10dp" />


The above layout includes a custom circular ImageView component for user avatars. The implementation details for this component can be found in the source code provided at the end of this article. The key challenge in this layout is creating proper chat bubbles, which can be efficiently handled using Android Studio's built-in tools.

When implementing chat bubbles, I used the .9.png tool available in Android Studio (highly recomended). For those not using Android Studio, you can launch this tool by double-clicking draw9patch.bat in your Android SDK\\tools directory.

![Nine-patch example](https://img-blog.csdn.net/20150720163308407?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)

When creating nine-patch images:

1. The top and left borders (1dp each) define the stretchable areas. If your content becomes wider or taller, the image will extend proportionally based on these markers. For example, if your content extends 300dp vertically, the image will automatically add 300dp × 1dp to maintain proper scaling.
2. The right and bottom borders are typically longer. The overlapping area in the example above represents the content region. Clicking "Show content" demonstrates how the content fills the defined area.

Mastering nine-patch images ensures your UI elements scale properly without distortion. With this technique, the UI design and development for the chat application is essentially complete.

Calling Turing API and Parsing JSON Responses
---------------------------------------------

Visit the official Turing Robot website at http://www.tuling123.com/openapi/ to register an account and obtain your API KEY from the personal center. The personal center also offers features like robot configuration and training, allowing your Turing robot to learn and respond to specific questions. You may explore these options further if interested.

Calling the official API is straightforward. Use a GET request to send your API KEY and message string in the URL, then process the returned string through JSON parsing. The implementation involves these steps:

### Network Access Class Implementation

package com.example.android.turingrobot;

import android.os.AsyncTask; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader;

/**

  • Asynchronous network access class */ public class NetworkRequestTask extends AsyncTask<String, Void, String> { private String requestUrl; private HttpClient httpClient; private HttpGet httpGet; private HttpResponse httpResponse; private HttpEntity httpEntity; private inputStream;

    // Using callback pattern


</div></div>

Tags: Android Chat UI Turing API JSON Parsing Nine-patch Images

Posted on Sat, 20 Jun 2026 16:47:20 +0000 by Franko126