2

I'm writing a terminal-like app (actually an ssh client). What I have now is a TextView which outputs the response and an EditText fixed at the bottom to type commands.

I'm trying to attach the EditText to the end of the TextView output (dynamically), like in a real terminal:

[TextView]root@whatever:~#[EditText] |command

Any thoughts on how to do this?

I will try to clarify what I'm trying to accomplish.

I can't set a fixed weight, cause more text is being added to the TextView asynchronously as more bytes are arriving from the stream. The LinearLayout solutions doesn't set the EditText cursor position at the end of the TextView input.I've tried RelativeLayout toRightOf but the EditText just goes out of screen as the TextView gets populated.

What I want:

example

Community
  • 1
  • 1
Spy
  • 39
  • 3

3 Answers3

1

Try this in your code, add weight to the views as per your requirements

   LinearLayout linearLayout=new LinearLayout(this);
   LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
   LayoutParams.WRAP_CONTENT);
   linearLayout.setLayoutParams(params);
   linearLayout.setWeightSum(1.0f);
   linearLayout.setOrientation(LinearLayout.HORIZONTAL);
   TextView textView=new TextView(this);
   EditText editText=new EditText(this);
   textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
   LayoutParams.WRAP_CONTENT,
   0.5f));
   editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
   LayoutParams.WRAP_CONTENT,
   0.5f));
   linearLayout.addView(textView);
   linearLayout.addView(editText);
Ankit Khare
  • 1,345
  • 11
  • 30
  • Thanks for the answer. Unfortunately, the EditText doesn't follow the end of the text in the TextView as more text is being added asynchronously. The weight needs to be set dynamically. I will add more details. – Spy Jan 15 '15 at 12:13
0

To avoid that on different screen sizes the editText get "eaten up", it will be better practise to use layout_weight. For example:

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

    <TextView
       android:id="@+id/yourTextView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1" />

    <EditText
       android:id="@+id/yourEditText"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1" />

    </LinearLayout>

with this, both views will have the same dimensions (length, height). You can play with the layout_weight attribute. If You want to have the TextView larger than the EditText, set the weight of textView for example to 0.25 and the weight of EditText to 0.75. And for sure vice versa is possible.

EDIT

if You want to have a static text which is not changeable by the user, You can do following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   >

 <EditText
   android:id="@+id/yourEditText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   />

 <TextView
   android:id="@+id/yourTextView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@id/yourEditText"
   android:layout_alignBaseline="@+id/yourEditText"
   android:text="C:\" />

 </RelativeLayout>

I can´t test it at the moment, but it should give a hint how You can do it.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Thanks for the answer. But is the same result as the above answer. I need the EditText to follow the end of the input in the TextView, not a fixed a weight. – Spy Jan 15 '15 at 12:15
  • oh, You mean like sombody puts in a file path into the terminal and You want to give some static sign or path before? Like the slash / for example at the beginning which is nocht changeable? – Opiatefuchs Jan 15 '15 at 13:37
  • Not really. I just want the EditText to be positioned after the last character of the TextView, considering the linewraps. Tried RelativeLayout but the EditText just goes offscreen. – Spy Jan 15 '15 at 14:09
  • Take a look here: http://img.photobucket.com/albums/v489/brspy/image_zps78a576ae.png The TextView is dynamic. – Spy Jan 15 '15 at 14:19
  • Similar question, but this guy wanted to add a button to the end of the TextView: http://stackoverflow.com/questions/17306961/how-could-find-out-the-end-of-text-position-in-textview – Spy Jan 15 '15 at 14:22
  • ok, now I know what You mean. But there is no TextView that has this behaviour. Your picture is like a TextView that gets cut of when the text inside is finished. The only thing You can do is, to set the EditText at this place You want like in the picture, but than the last text inside the textView can be hidden by the EditText. – Opiatefuchs Jan 15 '15 at 14:22
  • Maybe I have an idea. But I have to wait until I am home because I got no IDE here. I made some kind of auto wrap layout in the past. Don´t know if it will be the right thing, but I will check it. If here is no solution until then, I will post.... – Opiatefuchs Jan 15 '15 at 14:31
  • Do you know how I can get the x_pos of the last character of the TextView? I'm trying to move the EditText with a text changed listener. – Spy Jan 15 '15 at 14:31
0

Solved by updating the EditText(etInput) position after each append on the TextView(etShell):

Layout layout = etShell.getLayout();
int pos = etShell.length();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
etInput.setMaxWidth(width - (int) x);
etInput.setX(x);
etInput.setY(y);
Spy
  • 39
  • 3