Android Single line textView (for a calculator app) that takes input from buttons, is this an issue?

Viewed 1642

I am a beginner trying to make a calculator app in Android Studio that takes input from buttons. This is proving to be much more difficult than I thought it would be compared to just using EditText, but it has been a great learning experience so far.

I am working on the display portion of the app for which I am using a textView. I have it set up so that when a button is clicked, the button's value is appended to the textView. It's a calculator, so I want the textView to be displayed on a single line and shift horizontally for new input when the width of the textView is full.

I found a way to do this from another person's post that works by setting the following in the XML:

    android:inputType="text"
    android:maxLines="1"

When I did this it does exactly what I want, but I get an IDE warning that says:

Warning: Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?

Without the android:inputType="text" piece of code, the textView doesn't seem to scroll properly and I don't want to use an EditText. Is this something to worry about? Is there a better way to do this?

Thanks for the help.

1 Answers

Do not use inputType in TextView.

From this link - https://youtu.be/HYt1Ntu89X4

What I understood is you want the text to scroll dynamically i.e. on Button press.

You can do it like this :

XML

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/hzw"
    android:fillViewport="true"
    android:layout_gravity="end">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv1"
    android:maxLines="1"
    android:textSize="20dp"
    android:textStyle="bold"
    android:gravity="end"/>

</HorizontalScrollView>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/b"
    android:text="Add Text"/>

Java

    tv1 = (TextView)findViewById(R.id.tv1);
    tv1.setText("123+123");
    hzw = (HorizontalScrollView)findViewById(R.id.hzw);
    b = (Button)findViewById(R.id.b);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tv1.append("d+1+cff");

            hzw.post(new Runnable() {
                @Override
                public void run() {
                    hzw.fullScroll(View.FOCUS_RIGHT);
                }
            });
        }
    });
Related