0

I've just started learning Android, and have been working on a simple application where the user has a simple conversation with the computer.

My question is, how can I set up the UI in a way that allows the input from the user to be displayed on the view, during runtime? As the input prompts increase, there will be more content to be displayed, so I'll need to use a scrolling bar, which I believe could be taken care of by the scrollview layout.

Basically, what I'm trying to achieve is very similar to how chat applications display content, during runtime. How do I go about doing that? I've looked around, but can't exactly point myself in the right direction.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Malfunction
  • 1,324
  • 4
  • 15
  • 25
  • using asynctask, all hard work is done in background without blocking the user interface.. allowing the user to make action in the main UI, while in background the hard work is done. – StarsSky Jan 29 '13 at 16:52
  • 1
    I don't exactly understand how this can help me... – Malfunction Jan 29 '13 at 16:55

2 Answers2

1

There are multiple approaches to this:

  1. Use a ListView or even a ListActivity and add every new message as a new entry. ListView is scrollable by default.
  2. Use a TextView with android:inputType=textMultiLine to get a simple, multi lined textview.
  3. You can also use 2. with a EditText to be able to mark parts of the conversation, copy them and so on. Also see: Copy text from Android AlertDialog?

If I where you, I'd try it in this order. I would also create a custom ListAdapter and overwrite getView() to customize the messages like you want. Here is an example (not exactly the same, but should give you an idea): Android Spinner databind using array list

One idea would be to make all messages you sent to be aligned left and all received messages aligned left. This way, it would look like the iOS messaging application.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

You can use a ScrollView to create a chat session. To start, try something like this:

ChatActivity.java :

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChatActivity extends Activity{

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

        LinearLayout layout = (LinearLayout)findViewById(R.id.chatWindow);

        //When you get a new message to display, you can add it at runtime like this:
        TextView message1 = new TextView(this);
        message1.setText("how are you");
        layout.addView(message1);

        //Add another message
        TextView message2 = new TextView(this);
        message1.setText("I'm fine");
        layout.addView(message2);

    }

}

Then create a chat.xml resource:

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

    <LinearLayout
        android:id="@+id/chatWindow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" />

</ScrollView>

Later, you can use something like an AsyncTask to get better interaction with the user while updating the chat. An AsyncTask allows you to do things in the background while the user uses the UI.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
Shellum
  • 3,159
  • 2
  • 21
  • 26