In the following code , I have to add space before = , why ?
I think private val listofMDetail: MutableList<MDetail>= (No space before =) would be OK, but in fact, the Android Studio 3.1.3 give me an error.
private val listofMDetail: MutableList<MDetail> = //I have to add space before =
try{
myGson.fromJson<MutableList<MDetail>>(mJson)?: mutableListOf<MDetail>() //Load
}catch(e:Exception) {
e.message?.let{ logError("Paser: "+it)}
throw Exception(e)
}
The symbol >= is read as greater than or equal to, that is why the compiler show the error Expecting a '>'.
Just add the space, as recommended in the Kotlin style guide maintained by Google: https://android.github.io/kotlin-guides/style.html
Related
There is a special case in our code where we need to call setText with getText of the same TextView. SonarLint warn this with kotlin:S1656, to suppress it we use #SuppressWarnings annotation but it does not work. It started showing an error to the IDE and the warning from SonarLint still there. //NOSONAR is not working as well
No action is available as well for suppressing it.
Can you put the SuppressWarnings annotation before your method?
// Sonarlint plugin version 6.8.0.50884
#SuppressWarnings("kotlin:S1656")
fun yourMethod() {
// ...
withContext(coroutineDispatcherMain) {
// Set the text again to fix images overlapping text
htmlTextView.text = htmlTextView.text
}
}
I also wonder that if htmlTextView.invalidate() would work for you.
UPDATE:
SuppressWarnings annotation is also available for LOCAL_VARIABLE but I think the problem is more complicated in kotlin code.
If you examine the byte code of these lines below, you can see that some.num = ... is some.setNum(...) in byte code.
Sonar plugin's code analyser may not handle this kind of kotlin specific codes.
class Sonar {
private val some = Something(0)
private var member = 0
#Deprecated("...")
fun myNumber(): Int = 1
fun doThat() {
// local variable working
#SuppressWarnings("kotlin:S1874") // Code annotated as deprecated should not be used
val localNum = myNumber()
// not working
#SuppressWarnings("kotlin:S1874")
some.num = myNumber()
// not working
#SuppressWarnings("kotlin:S1874")
member = myNumber()
// ...
}
}
I found the docs for the issue to confirm its the right number: https://rules.sonarsource.com/kotlin/RSPEC-1656
Also read the source at https://github.com/SonarSource/sonarlint-intellij but not much help.
It seems like "// NOSONAR" is what you want (I know you said you tried it, but did you have the space? And have it at the end of the line?)
htmlTextView.text = htmlTextView.text // NOSONAR
Just a guess here, but you could also try:
#SuppressWarnings("RSPEC:S1656")
lastly to check suppress is working you can ignore everything:
#SuppressWarnings("all")
htmlTextView.text = htmlTextView.text
Big discussion here: Turning Sonar off for certain code
This article also gives you options to choose from: https://docs.codescan.io/hc/en-us/articles/360012109711-Ignoring-violations
I'm building an app which reads EXIF data from images and overlays that data on the image so you can share your camera settings with a nice graphic rather than manually typing them out (EG: "F/1.4 at 1/200 ISO400")
I'm using AndroidX ExifInterface 1.1.0-beta01 and the blow code works to get every piece of data except the LensMake and LensModel are always null.
I've tried reverting to ExifInterface 1.0.0 and that made no difference, it still behaves identically.
I note that the documentation for ExifInterface refers to LensMake and LensModel as returning an "ASCII String" which Camera Make and Camera Model just return a "String" so i've tried different variations of getAttribute without success.
These exact files work fine on the iOS version of the app I've previously built and i've tried files from multiple different cameras (Fuji X-T3, Canon 5D III)
var stream: InputStream? = null
try {
stream = contentResolver.openInputStream(uri)
val exifInterface = ExifInterface(stream!!)
FS = exifInterface.getAttribute(ExifInterface.TAG_F_NUMBER)!!
SS = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME)!!
ISO = exifInterface.getAttribute(ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY)!!
val LensMake = exifInterface.getAttribute(ExifInterface.TAG_LENS_MAKE) //THIS APPEARS TO BE ALWAYS NULL :(
val LensModel = exifInterface.getAttribute(ExifInterface.TAG_LENS_MODEL) //THIS APPEARS TO BE ALWAYS NULL :(
val CameraMake = exifInterface.getAttribute(ExifInterface.TAG_MAKE)
val CameraModel = exifInterface.getAttribute(ExifInterface.TAG_MODEL)
}
I'd like to be able to read the lens information, I know it's in the file but this library doesn't seem to want to expose it.
There is an open bug filed on the issue tracker, which states, that:
Although the constants are available for LensMake and LensModel, the getter does not return the actual values from the file. It seems like proper support is missing. I think the reason is that ExifTag[] IFD_EXIF_TAGS does not contain an array item for lens make and model. Adding the following lines at the right place of the aforementioned array, seems to fix things:
new ExifTag(TAG_LENS_MAKE, 42035, IFD_FORMAT_STRING),
new ExifTag(TAG_LENS_MODEL, 42036, IFD_FORMAT_STRING),
Not sure how reliable this is, but it is at least a solution approach.
When I work with android studio it shows hints very often, I have 2 questions about it:
1) Why do android studio when I press enter (in scenario like on screenshot) makes just
noteRef.get()
.addOnSuccessListener {}
}
instead of
noteRef.get()
.addOnSuccessListener { documentSnapshot -> }
}
Is there a way generate { documentSnapshot -> } automatically?
2) Second problem: imagine, I have code like this:
var planetMarsSize = 0
var planetEarthSize=0
planetMarsSize = 5
for example I made mistake in this code and have to make "planetEarthSize = 5" instead of "planetMarsSize = 5" so i place cursor between "Mars" and "size", delete "Mars" and start tipping "Earth" - in this case Android studio shows me a hint to autocomplete text, I press enter. At the end text become "planetEarthSizeSize = 5" so android studio shows error. How can I make android studio to autocomplete "planetEarthSize" instead of "planetEarthSizeSize"?
Video of problem: https://photos.app.goo.gl/qAbcxtZsV4URwWwR8
1) The only difference between the two methods is that in the second, you can choose the name of the parameter. In the first one, you can access the parameter with it
2) You can press TAB on your keyboard to overwrite the whole variable name (it actually says this in the little dialog that pops up :) )
New to Android. Trying to understand how to get the list of available apertures. The code below does not work as expected. Could someone please explain how I can extract the list from this property? Using camera 2 api.
apertures = CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES;
You have to use getCameraCharacteristics("camerayouwant").get() to get the characters.
float[] aperatures = CameraManager.getCameraCharacteristics("camerayouwant").get(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES);
Use aperatures = CameraManager.getCameraCharacteristics("camerayouwant").get(CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES);
In android setFastScrollEnabled(true); is used for making ListView fast scroll.
This fast scroll does not work when there are less items in ListView. I read it somewhere that fast scroll in android works only when listview total height is 4 times or more than listview visible height. I have spent hours trying to find it in framework source code, but I am not able to find it.
Can someone point me to place in android framework source code where this condition to disable fast scroll when there are less items in ListView.
Yes ofcourse, here is the link:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/FastScroller.java
This is a condidion between lines 224-227. And for setting how many pages it will be needed to show fast scroll, there is a constant:
private static int MIN_PAGES = 4;
And about disabling it... It's a private field so there is no simply way to do it. You can try use reflections or create custom FastScroller based on original.
But i think the simpliest way is to check like in this condidion in Android code:
//pseudocode
int numberOfPages = listView.itemsCount / listView.visibleItemsCount;
if(numberOfPages > yourValue)
listView.setFastScrollEnabled(true);
else
listView.setFastScrollEnabled(false);
But it may only work if yourValue will be greater than 4. If you want to do it for less values then you need to use reflection or create custom class.
EDIT:
For newest version there is the link:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/FastScroller.java/
And lines are 444-447 :)
And for reflections I would try something like this:
try {
Field scrollerField = AbsListView.class.getDeclaredField("mFastScroller"); //java.lang.reflect.Field
scrollerField.setAccessible(true);
FastScroller instance = scrollerField.get(listViewInstance);
Field minPagesField = instance.getClass().getDeclaredField("MIN_PAGES");
minPagesField.setAccessible(true);
minPagesField.set(instance, yourValue);
} catch (Exception e) {
Log.d("Error", "Could not get fast scroller");
}
It's not tested so i don't know if it really works.
You can try setting the attribute
android:fastScrollAlwaysVisible="true"
in your listview xml
Trying to adapt the answer from MichaĆ Z., I finally ended up doing this :
listView.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
val numberOfPages: Int = listView.count / listView.visibleItemsCount
listView.isFastScrollAlwaysVisible = numberOfPages > THRESHOLD
}
It works just fine for me