简体   繁体   中英

Get Android EditText to scroll horizontally without word wrap

So I have an android app that transcribes normal text to ascii-art text, say like from "hello world" to

  _          _ _                            _     _ 
 | |__   ___| | | ___   __      _____  _ __| | __| |
 | '_ \ / _ \ | |/ _ \  \ \ /\ / / _ \| '__| |/ _` |
 | | | |  __/ | | (_) |  \ V  V / (_) | |  | | (_| |
 |_| |_|\___|_|_|\___/    \_/\_/ \___/|_|  |_|\__,_|

(sorry mobile users, you might not be able to see that, idk)

and naturally, I need the EditText in which the final product (the ascii-art text) doesn't word-wrap since if it did, it'd look all jumbled and everything. I need a horizontally scrolling EditText. Here is my current code.

super.onCreate(savedInstanceState);

    Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    String letter;
    EditText textView = new EditText(this);
    Typeface courier = Typeface.createFromAsset(getAssets(),"Courier_Prime.ttf");
    textView.setTextSize(20);
    textView.setTypeface(courier);
    setContentView(textView);
    String value;
    String toPrint;
    int textLength = message.length();
    for(int i = 0; i <= 14; i++){
        for(int j = 0; j < textLength; j++){
            letter = message.substring(j,j+1).toUpperCase(Locale.getDefault());
            value = letter + i;
            toPrint = isometric.get(value);
            textView.append(toPrint);
            if(j == textLength-1){
                textView.append("\n");
            }
        }
    }

And nothing but a little RelativeLayout in the xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dumpong.myfirstapp.DisplayMessageActivity" >

<EditText 
    android:id="@+id/asciiBox"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollHorizontally="true"/>


</RelativeLayout>

I'm not exactly sure how to make a horizontal scroll for the EditText though. Programmatic or through XML is fine. Thanks for any help!

Maybe I've misunderstood but here is how to have an EditText which can scroll horizontally.

The needed layout :

<HorizontalScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#333333"
    android:fillViewport="true"
    android:scrollbars="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:textColor="#cccccc"
            android:textSize="35sp" />
    </LinearLayout>
</HorizontalScrollView>

And here is the associated code : you have to call init() under OnCreate().

private EditText mEditText;
private HorizontalScrollView mScrollView;

private void init() {
    mEditText = (EditText) findViewById(R.id.TEXT_STATUS_ID);
    mScrollView = (HorizontalScrollView) findViewById(R.id.SCROLLER_ID);
    // loadDoc();
    String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            + "Aliquam tempus convallis metus, ac congue dui elementum ut."
            + "Suspendisse rutrum non sapien feugiat fermentum."
            + "Phasellus vulputate quam in sapien vulputate venenatis."
            + "Pellentesque porta tincidunt nisi, et scelerisque augue facilisis nec."
            + "Curabitur eget risus quam."
            + "Maecenas pellentesque egestas enim, in ornare nisl lobortis id."
            + "Nunc vitae facilisis libero, vitae porttitor tellus.";
    mEditText.setText(s);
    scrollToBottom();
}

private void scrollToBottom() {
    mScrollView.post(new Runnable() {
        public void run() {
            mEditText.setHorizontallyScrolling(true);
            mEditText.setMovementMethod(new ScrollingMovementMethod());
            mScrollView.smoothScrollTo(0, mEditText.getBottom());
        }
    });
}

Don't hesitate to comment if I've misunderstood or if you need more explanation.

Wrapping your edit text with a horizontalscrollview should do it.

<HorizontalScrollView> 
  <Edittext>...</Edittext>
 </HorizontalScrollView>
  • Horizontal Scrolling EditText

Adapted from Kassisdion's answer [tested and working on api 21; android: 5.0]:

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

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginStart="2dp"
    android:layout_marginTop="2dp"
    android:fillViewport="true">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:fadingEdge="none"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:isScrollContainer="true"
        android:overScrollMode="ifContentScrolls"
        android:scrollHorizontally="true"
        android:scrollbarStyle="insideOverlay"
        android:scrollbars="vertical|horizontal"/>

</HorizontalScrollView>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM