1

As part of my application, I have simple screen for entering text. When the focus is in the EditText, it never receives the tab character.

I press on the tab key on the virtual keyboard, but the behaviour is that the keyboard disappears.

Here is my layout :

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Intitulé"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etTextTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:inputType="textCapSentences"
        android:singleLine="true"
        android:imeOptions="actionNext" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contenu"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|top"
        android:layout_marginLeft="15dp"
        android:textSize="25sp"
        android:lineSpacingExtra="4dp"
        android:singleLine="false"
        android:inputType="textMultiLine"
        android:scrollHorizontally="false"
        android:scrollbarStyle="insideOverlay"
        android:scrollbars="vertical"
        android:overScrollMode="always" />

</LinearLayout>

The control in question is the last EditText, as it is multiline, with scrolling.

I need to provide this so that the user can do some (very) basic formatting.

Any ideas ?

Simon
  • 2,208
  • 4
  • 32
  • 47
  • Check out : http://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android – Haresh Chhelana May 05 '15 at 12:08
  • You seem to have misunderstood. I want to be able to add the tab character to the text content, not jump to the next field. – Simon May 05 '15 at 16:07

2 Answers2

3

According to your comment, "tab" character is not much different from "space" character. So workaround should be enlarging tab width.

Here is the working code.

tab width = INDEX_CHAR * TAB_NUMBER

Search every TAB character in the string and apply it the custom ReplacementSpan , with getSize() returning tab width.
private static final String INDEX_CHAR="m";
private static final int TAB_NUMBER = 3;
public void applyTabWidth(Editable text, int start, int end)    {
    String str=text.toString();
    float tabWidth=getPaint().measureText(INDEX_CHAR) * TAB_NUMBER;     
    while (start < end)     {
        int index=str.indexOf("\t",start);
        if(index<0)
            break;
        text.setSpan(new CustomTabWidthSpan(Float.valueOf(tabWidth).intValue()) , index, index+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        start = index+1;
    }
}

CustomTabWidthSpan

class CustomTabWidthSpan extends ReplacementSpan
{
    int tabWidth=0;
    CustomTabWidthSpan(int tabWidth){
        this.tabWidth=tabWidth;
    }
    @Override
    public int getSize(Paint p1, CharSequence p2, int p3, int p4, Paint.FontMetricsInt p5)
    {
        return tabWidth;
    }

    @Override
    public void draw(Canvas p1, CharSequence p2, int p3, int p4, float p5, int p6, int p7, int p8, Paint p9)
    {           
    }
}

Optimization:

  • I choose not to apply ReplacementSpan to the whole string but only to each TAB character.

  • Everytime text has been changed, search TAB characters only in changed text. Below is how to apply TextWacher.

    editText.addTextChangedListener(new TextWatcher(){
        int start=0,end=0;
            @Override
            public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
            {
            }
    
            @Override
            public void onTextChanged(CharSequence p1, int start, int before, int count)
            {
                this.start=start;
                this.end=start+count;
            }
    
            @Override
            public void afterTextChanged(Editable p1)
            {
                applyTabWidth(p1, start, end);
            }
        });
    
johnkarka
  • 365
  • 2
  • 14
1

Which virtual keyboard did you use? I thought stock keyboards do not contain "TAB" character.

If you use third-party keyboard, make sure if it enters real TAB character (ascii code - 61) or it simulate PC-like TAB behaviour (searching and focusing next EditText)

You should check like this.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    switch(keyCode){
        case KeyEvent.KEYCODE_TAB:
            Toast.makeText(this,"TAB key triggered!",Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onKeyUp(keyCode, event);
}

Note: I'm not answering your question,just giving a debug tip.

johnkarka
  • 365
  • 2
  • 14
  • Hi @johnkarka, thanks for your answer. In fact I need to be able to import text files which hold tab characters and then display them correctly. On my Samsung Galaxy Tab Pro, the default softkeyboard has a tab key, which could mean I could capture it and then insert it into the edittext field. However, just to this point, it produces the same effect as a space character. – Simon May 20 '15 at 15:19