0

I am a just a beginner to Android Studio and have started as a hobby, and to learn it's basics I have tried to make a simple calculator in Android Studio(Kotlin).

The problem is that if the answer is to long to be diplayed in the text view box it cuts out...So to solve this problem I made the text view Horizontally Scrollable using "Horizontal Scroll View" in the xml File,
But now the problem is when the user types the text after the view is full, it does not automatically scroll to the last digit he has typed, rather he/she has to manually scroll it to the end. Here is the code for the text box which displays the result:

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 
    <TextView
        android:id="@+id/tvExpression"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#1C1C1C"
        android:textColor="#FFFFFF"
        android:textSize="27dp"
</HorizontalScrollView>`

Could you please tell a way so that each time a new value is added to the box, it automatically scrolls to the right most part of the box, so that the users can see his/her last inputted value?

Please Help me!
Thanks,
Shaurya

Shaurya Goyal
  • 169
  • 1
  • 11

1 Answers1

0

Preferably delete a tag of HorizontalScrollView Because this makes textview More flexible to contain the result of the calculator. You can also select the max line of the textView by using this attribute:

android:maxLines="select int number"

If you want a way to make the scroll of horizontalScrollView from the end you can use this code in your activity that contents the horizontalScrollView:

horizontalScrollView.postDelayed(
            Runnable { 
            horizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) },
            100L
        )
Amnah
  • 27
  • 3
  • Could you tell me how to do it? – Shaurya Goyal Aug 06 '21 at 09:53
  • The above code will be on the code side, I mean in the activity use these lines of horizontalScrollView, then the run will find that the scroll starts from the end – Amnah Aug 06 '21 at 10:21
  • Do I need to put it any class or somethin? – Shaurya Goyal Aug 06 '21 at 10:41
  • In the Main Activity.kt? – Shaurya Goyal Aug 06 '21 at 11:00
  • yes, put it in the activity that contains this view (horizontalScrollView) – Amnah Aug 06 '21 at 11:28
  • This link has the same question as yours https://stackoverflow.com/questions/4720469/horizontalscrollview-auto-scroll-to-end-when-new-views-are-added And looks at this to make sure the calculator has correct practice https://medium.com/swlh/build-a-simple-calculator-application-for-android-using-kotlin-and-android-studio-e0fb9b4fa0c9 – Amnah Aug 10 '21 at 12:39