Textview won't scroll when using append - kotlin

I'm trying to get a TextView/ScrollView in Kotlin to scroll to bottom after appending text.
Weird thing is, I have it working perfectly for 1 TextView, however, another (near identical) TextView doesn't work?
So after a while of struggling to find a difference, I finally found ... something ...
I was populating one of them:
textView = text
and the other
textView2.append ( text )
The one using append, would not scroll to bottom, nor would it show the shaded curve for "overScrollmode" (which is set to true).
Obviously, I was using append, because I'm adding a small amount of text, repeatedly.
The other textView, is an "all or nothing" replacement. (in reality, I don't want the latter to scroll down, I actually set it to scroll to 0,0 (top), however, I used it as a test to find this issue :) )
So why does a text view, when using append, not honor/scroll as a result ?
<androidx.cardview.widget.CardView
android:id="#+id/cvMoveHistory"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="5dp"
app:cardBackgroundColor="#025D68"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toTopOf="#id/cvMoveHint"
app:layout_constraintEnd_toStartOf="#+id/linearLayoutBoardButtons"
app:layout_constraintStart_toEndOf="#+id/frLayGameBoard"
app:layout_constraintTop_toTopOf="parent">
<ScrollView
android:id="#+id/svMoveHistory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:fillViewport="true">
<TextView
android:id="#+id/moveHistory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#939393"
android:fontFamily="monospace"
android:padding="4dp"
android:scrollbars="vertical"
android:text="123) 5e2-5e3 1a1-1a3"
android:textColor="#000000"
android:textSize="11sp" />
</ScrollView>
</androidx.cardview.widget.CardView>
and in code:
-- in onCreate
tvMoveHistory = findViewById ( R.id.moveHistory )
svMoveHistory = findViewById ( R.id.svMoveHistory )
tvMoveHistory.movementMethod = ScrollingMovementMethod()
-- to actually add text
moveHistory.append ( newText )
-- during a central Refresh routine (yes, confirmed it runs via debugger)
svMoveHistory.post {
svMoveHistory.fullScroll(View.FOCUS_DOWN)
}
What's even more strange, I created a new project, minimal with just a few objects in order to re-create the issue.
It behaves perfectly whether I use append, or just straight assign it.
So I'm again baffled as to what's causing the issue ? (ie using append in my current project means no scroll to bottom, using .text = full text means scroll to bottom works).
However, re-testing in another fresh project results in expected behaviour?
Text I'm adding is very simple, no spannable string, no bold/html markup etc.
Just simple line in format of a simple chess-like notation:
12) a5-g6 e3-h7
... etc. (+ CR ie \n at end of line for new line)
I've scoured my logic, I literally don't touch that textView for any reason other than to append a small piece of text. Occassionally (ie new game), I reset it: = ""
but aside from above code, there is nothing else updating/modifying this textview/scrollview ?
Any ideas what I'm missing?

Related

Nested Recycler Views, Vertically Scrolled ( Parent using Pagination )

<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_sales_report"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:clipToPadding="false"
android:paddingHorizontal="10dp"
android:paddingBottom="120dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/mtv_sales_report_date"
tools:listitem="#layout/item_reportings_new" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_dynamic_inner_reporting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:paddingBottom="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="#id/mtv_date_dynamic"
tools:listitem="#layout/item_inner_reporting" />
The inner recycler view is not scrolling I have tried many solutions, non helped me, how can I make the inner recycler view scroll vertically, they both scroll vertically.
android:layout_height="0dp" You shouldn't use this without setting either the layout weight or without constraining both the top and bottom of the view because without constraints, it might just act as wrap_content basically extending beyond the screen.
Also, for the inner view, if the orientation is vertical, then height shouldn't be wrap_content maybe changing that would help.
Also, just a tip, having both recycler be vertical is a bad idea because how would android (or the user) know which view you are scrolling on if they are together.
Issue is solved by showing only four items in the inner Recycler View, and enabling nestedScrollView = true, also handling OnTouch Events for the parent Recycler View.

how to get variable into string (string.xml using kotlin

Hi i'm new to programming so ...
I made a simple app and in MainActivity.kt I just have one textview that point to a string.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/firsttext"
in string.xml I have
lost of text that is just her for test\n\n does not make sence\n XXX and more text\n
and in middle YYY ad some text
so far so good ,, it show text.
I also have made a settingsFragment.
In root_preferances.xml i have
<EditTextPreference
android:defaultValue="not empty"
android:key="edit_text_FrontPress"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Front pressure should be"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="not empty"
android:key="edit_text_RearPress"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Rear pressure should be"
app:useSimpleSummaryProvider="true" />
Now here is my question !
do I get the value from settings menu
edit_text_RearPress into the string.xml (on place where now XXX is show)
edit_text_FrontPress into the string.xml (on place where now YYY is show)
so that is is visible in my text

Android Spinner text difference

I'm adding two different spinners to my XML layout, one thats filled through an XML file and one thats filled programmatically.
<Spinner
android:id="#+id/spHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="textEnd" />
<Spinner
android:id="#+id/spDevice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/spinner_data"
android:textAlignment="textEnd" />
So the one Spinner is filled through this XML file, which goes great and gives a good clear text quality.
<resources>
<string-array name="spinner_data">
<item>Alle apparaten</item>
<item>Light</item>
<item>Door Sensor</item>
<item>Socket</item>
<item>Smart meter</item>
<item>Multi sensor</item>
</string-array>
<string-array name="solo_data">
<item>Hele huis</item>
</string-array>
The other spinner gets filled programmatically, with the following code:
private var roomDtoList: ArrayList<RoomItemDto> = ArrayList()
private var spRoomData : ArrayList<String> = ArrayList<String>()
private fun fillSpRooms(){
spRoomData.clear()
for(item in roomDtoList){
spRoomData.add(item.name)
}
var spAdapter : ArrayAdapter<String> = ArrayAdapter<String>(this.context, R.layout.spinner_drop_layout, R.id.spinnerTextview, spRoomData)
spHome!!.adapter = spAdapter
}
So whenever I fill this spinner with the following code, the filled text looks ugly and has no margin/spacing between every line. Even when I add margin and textStyling into the R.id.spinnerTextview, it won't make the text look clear.
Anyone knows a fix?
Try doing this. Hope this will help.
for(item in roomDtoList){
spRoomData.add(item.name)
}
spHome!!.setOnItemSelectedListener(this)
// Create an ArrayAdapter using a simple spinner layout and languages array
val aa = ArrayAdapter(this, android.R.layout.simple_spinner_item, spRoomData)
// Set layout to use when the list of choices appear
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Set Adapter to Spinner
spHome!!.setAdapter(aa)

Glide Cache management in Recyclerview with Grid layout

I am trying to build a gallery for my app. Following some tutorials and stackoverflow, I finally managed to make the gallery. I am using Glide to set images, so no issue of memory crashing and all. The only problem is, when I fling, Images are not set into their views as fast as those views are being shown. Here is a gif. So I get to see the placeholders or the background of the recyclerview while imageviews are being set. Depending on the fling speed, this may last for 500-1000ms. But that is sufficient to make the gallery look bad. This happens when I use fling to scroll up or down. To my knowledge, this is because glide freeing the images from it's cache. Is there any solution for it.
Here is my code.
activity_gallery:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_monuments"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GalleryActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:alwaysDrawnWithCache="true"
android:clipToPadding="false"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"/>
</LinearLayout>
Here is how I am setting recyclerview:
RecyclerView gridView = (RecyclerView)findViewById(R.id.recyclerview_gallery);
gridView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),NUM_OF_COLUMNS);
gridView.setLayoutManager(layoutManager);
gridView.isDrawingCacheEnabled();
gridView.addItemDecoration(new MarginDecoration(GalleryActivity.this, NUM_OF_COLUMNS, GRID_PADDING, true));
gridView.setHasFixedSize(true);
gridView.setVerticalScrollBarEnabled(true);
gridView.setBackgroundColor(getResources().getColor(R.color.colorBlack));
GalleryAdapter galleryAdapter = new GalleryAdapter(GalleryActivity.this, GalleryActivity.this, ImageNamesList, columnWidth);
gridView.setAdapter(galleryAdapter);
Here is how I am setting imageview in adapter:
viewHolder.galleryImage.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
Glide.with(context)
.load(uri)
.asBitmap()
.placeholder(R.drawable.monument)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.galleryImage);
Please let me know of the possible solutions.
P.S: The gif is low frame rate and hence showing the slow fling which is not the case.
To solve cache management, i use a singleton store best practice. Realm for android is perfect it is NoSql and super fast, what i normally do is create a realm object, in the adapter check if the current url is in realm, if true dont pull new image else, pull and store.
Then when the main activity or fragment is ever being destroy just delete all from realm.

Image view relative position

I'm new to Android programming and I'm facing a problem: I don't know how to set the position of an imageView over another imageView. I want the position to be relative, maybe with percentages, to maintain the aspect on different screens or in both landscape and portrait.
thank you!
to place an Image over another use relativelayout as the root layout for both image views. This will make both image views start at the same coordinate and you can flexibly arrange them as you like.
example :
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<ImageView android:id="#+id/videoImage"
android:layout_width="match_parent"
android:layout_height="170dip"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<ImageView
android:id="#+id/playVideo"
android:src="#drawable/play_"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"/>
Alternatively you can use frame layout.