How to add a new textView to empty TextView array in Kotlin - kotlin

I have this array
val textViewList = arrayListOf<TextView>()
and I want to add a new textView with code
textViewList.add(TextView)
to do that
textViewList[0].textSize = 20f
textViewList[0].text = "Programmatically created textView "
myLayout.addView(textViewList[0])
but
textViewList.add(TextView)
is not working.

It took some try and error but this
textViewList.add(TextView(this))
worked.

Related

setOnClickListener is not showing in intellij

setOnClickListener is not showing in intellij even the textview names I can't write them without R.id.the_name.
What is going wrong?
This is not how android works. It seems you miss the very basics.
R.id.button is just an Int, the id of a view. To get the view you can do findViewById(id) for example.
So then in your case maybe:
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
//on click
}
You should implement view binding to your projects.
https://developer.android.com/topic/libraries/view-binding
So instead of val btn = findViewById<Button>(R.id.yourbuttonId) you can val btn = binding.yourbuttonId

ImageView is not respecting the size of the CardView

I am trying to make some sort of gallery app. I would like to display images inside of a ScrollView inside a GridLayout so I used CardView for this and it looks like this:
But when I put ImageView inside each CardView, the CardView spreads and fills the whole ScrollView. Here's a preview:
The CardView is created programatically. Here's the code
fun CreateCardView(ItemCount: Int){
for (i in 1 until ItemCount){
/////////////////////CARD VIEW//////////////////////////////
val card_view = CardView(this)
card_view.radius = 8F
card_view.setContentPadding(8,8,8,8)
card_view.setCardBackgroundColor(Color.LTGRAY)
card_view.cardElevation = 8F
val param = GridLayout.LayoutParams()
param.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)
param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)
card_view.layoutParams = param
val param2 = card_view.layoutParams as GridLayout.LayoutParams
param2.setMargins(10,10,10,10)
card_view.layoutParams = param2
subLayoutGrid?.addView(card_view)
////////////////////////END//////////////////////////////////////
///////////////////////IMAGEVIEW////////////////////////////////
val img = ImageView(this)
val imageparams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT
)
img.layoutParams = imageparams
img.scaleType = ImageView.ScaleType.CENTER_CROP
img.setImageResource(R.drawable.animaleight)
card_view.addView(img)
////////////////////////END///////////////////////////
}
}
How can I put ImageView inside the CardView without filling the whole ScrollView?
I've already fixed my problem. I've tried to use a 40px x 40px Picture then it worked the way I want to. See the image:
It seems like when you use high resolution Picture, the app cant handle the populating of ImageView in GridLayout.

Kotlin read variable and post yes or no answer

I use Java and Kotlin for personal projects with little experience and I might use your help with this one. What I am trying to do is to read telemetry data (temperature, pressure, light level) from Estimote beacon. I have already managed to put & update values into textviews.
Now I would like to get IF conditions at this point (later I will try to save & upload them to Firebase database). Basically I want to check if one of the telemetry values is higher or less than Double number. Eg. if the light level is below 6 lux, write "Less then 6!", if more "More than 6!"
val scanner = EstimoteBluetoothScannerFactory(applicationContext).getSimpleScanner()
val scanHandler = scanner.estimoteTelemetryFullScan()
.withBalancedPowerMode()
.withOnPacketFoundAction {
var temperature: TextView = findViewById<TextView>(R.id.temperature)
temperature.setText("Temperature: ${it.temperatureInCelsiusDegrees}")
var magnetometer: TextView = findViewById<TextView>(R.id.magnetometer)
magnetometer.setText("Magnetometer: ${it.magnetometer}")
var pressure: TextView = findViewById<TextView>(R.id.pressure)
pressure.setText("Pressure: ${it.pressure}")
var ambientLight: TextView = findViewById<TextView>(R.id.ambientLight)
ambientLight.setText("Ambient Light: ${it.ambientLightInLux}")
var motionState: TextView = findViewById<TextView>(R.id.motionState)
motionState.setText("Motion State: ${it.motionState}")
}
.start()
if(ambientLight > 6) {
var checkLight: TextView = findViewById<TextView>(R.id.checkLight)
checkLight.setText("More than 6!")
}else{
var checkLight: TextView = findViewById<TextView>(R.id.checkLight)
checkLight.setText("Less than 6!")
}
This code does not update the textview value

Android scroll to position in Recylerview not working

I want to scroll the RecyclerView to a position, and I use LinearLayoutManager.scrollToPositionWithOffset() to scroll.
But it does not work. Could any one help me?
RecyclerView rv = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
rv.setLayoutLayoutManager(layoutManager);
layoutManager.smoothScrollToPosition(position);
Scrolling RecyclerView to certain item position can be achieved with LayoutManager which is used with that particular RecyclerView. Plz refer to above piece of code
If you don't have a lot of items in your list, you can use LinearSmoothScroller
val smoothScroller = object : LinearSmoothScroller(activity) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
and then when you want to scroll:
smoothScroller.targetPosition = position // position on which item you want to scroll recycler view
binding.cardsRecycler.layoutManager?.startSmoothScroll(smoothScroller)

Problem with getting the id's of image view in Android?

I am implementing an application displaying images and messages obtained from .net server. For that I'm adding LinearLayout's with TextView and ImageView dynamically by using for loop.
At first I am getting messages and I'm adding them to the TextView with dummy images placed in ImageView:
for(int i = 0; i < messages.size(); i++) {
FrameLayout f1 = new FrameLayout(this);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
im = new ImageView(this);
TextView tv = new TextView(this);
tv.setText(messages.get(i).getmessage());
im.setImageResource(R.drawable.person);
l1.addView(tv);
l2.addView(im);
f1.addView(l1);
f1.addView(l2);
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1);
}
After some time I am getting original images. My intention is to replace the dummy images with original ones. To achieve this I'm using imageview.getid() unfortunately I'm getting last id of the imageview. But I need to get each individual imageview id's.
What is the best way of changing dummy images with real ones?
Looks like you need to store each imageView id in a map from each message -> imageView like so:
Map<Message, Long> messageMapping = new HashMap<Message, Long>();
for(int i = 0; i < messages.size(); i++) {
FrameLayout f1 = new FrameLayout(this);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
im = new ImageView(this);
TextView tv = new TextView(this);
//add mapping
messageMapping.put(messages.get(i), im.getId());
tv.setText(messages.get(i).getmessage());
im.setImageResource(R.drawable.person);
l1.addView(tv);
l2.addView(im);
f1.addView(l1);
f1.addView(l2);
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(f1);
}
Then when it comes time to lazy load your images into the existing imageViews you simply need to look up the imageView id reference by message mapping:
(Total guess of what your code could look like)
for(int i = 0; i < messages.size(); i++){
Image image = queryServerForImage(messages.get(i).getImageId());
ImageView imageView = ((ImageView)findViewById(messageMapping.get(messages.get(i))));
//some conversion & seteup may be needed to get image into proper format
imageView.setImageBitmap(image);
}
You may be able to do something similar with async tasks. You could span each one in the message ImageView creation loop with the associated resource id and having each asyc task update the ui from the network response.