3

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.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
CJF
  • 147
  • 3
  • 15
  • 2
    why would `TextView` have a inputType – Ramanlfc Sep 25 '17 at 03:48
  • You dont need inputType for TextView, you use it for EditText... For eg if you want you edittext to input phone number you can set the inputType to 'phone' which will restrict the user from input charters.. – Adolf Dsilva Sep 25 '17 at 03:53
  • Maybe I should explain better what I want to do. If you open up calculator and type enough numbers and operators, you will see the line at the top that displays the input history scrolls left as more numbers are entered. That's what I'm trying to make my textView do. I don't care about the ability to navigate left and right though. – CJF Sep 25 '17 at 04:08
  • I assume that you've tried `android:gravity="right"` on your textview. If that doesn't do what you want, please update your question with graphics/diagrams to show what you are trying to accomplish. It may also be that the behavior you are looking for will require a Custom View. Related: https://stackoverflow.com/questions/25247018/android-textview-singleline-field-hides-long-text – Morrison Chang Sep 25 '17 at 04:18
  • Here is a video of with and without inputType="text" : https://youtu.be/HYt1Ntu89X4 first the program is ran with inputType in the XML. This is the way i want it to work. The second part shows how it runs incorrectly once inputType is removed. I haven't tried the solution posted by Supra yet so I will try that next. – CJF Sep 25 '17 at 04:43

1 Answers1

2

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);
                }
            });
        }
    });
supro_96
  • 570
  • 6
  • 14
  • I am using a constraintLayout and I tried using this code and it doesn't seem to work. I am probably doing something wrong, but the text just keeps going off the screen without scrolling. I can upload a video with the code running if it would help. I plan to add the second text view later that displays the actual calculations. I just need to figure out how to get a textView to scroll dynamically at the moment as in the first part of the video I posted above. Thanks for the help! – CJF Sep 25 '17 at 05:04
  • Here is a pastebin of the XML code I am using for the horizontal scroll and textView if it helps: https://pastebin.com/qRUh28HF – CJF Sep 25 '17 at 05:10
  • Updated the answer have a look – supro_96 Sep 25 '17 at 05:25
  • One thing I overlooked that I just realized is that the horizontal scroll view can be manually scrolled. Using your Java code hzw.fullScroll(View.FOCUS_RIGHT); works almost perfectly. The only problem now is that it seems like the view only scrolls to the number right before the last one I entered. for example, if I enter the number 2 and then the number 1, the view will scroll to show number 2 when I entered number 1. It's almost like the view is slightly off the screen, but it looks fine in layout view. It's almost like a padding/margin issue... not sure. – CJF Sep 25 '17 at 06:24
  • android:layout_margin="10dp" in HorizontalScrollView. That will solve your problem. – supro_96 Sep 25 '17 at 06:31
  • That didn't seem to work, but it's worth noting that I'm using a ConstraintLayout with a Horizontal View inside it. Here is the XML if it helps: https://pastebin.com/bJ6Fb4Dq Thanks so much for all the help by the way. – CJF Sep 25 '17 at 06:40
  • Add margin to the root layout (Constraint layout). I just tried it, works fine. – supro_96 Sep 25 '17 at 06:49
  • It was because I had layout_marginEnd="16dp" in the TextView, thanks so much! – CJF Sep 25 '17 at 07:11