Android Studio 4.1 Kotlin widget-ID not recognized in AminActivity.kt - android-studio

I try to create a new project in Android Studio (AS) 4.1 with Kotlin. I use "empty activity" and create project "MyTest" .
AS creates activity_main.xml with unnamed TextView "Hello World". I give the id the name myText.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The created MainActivity.kt is standard and looks like this
package com.example.mytest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
All this is normal AS, BUT
now in MAinActivity.kt it should be possible to type
myText = "any string"
but here myText is not recognised. And I don't understand that.
I have some small test-projects from earlier AS version. They look identical - and it works to type myText and myText is immediatly recognized.
What is wrong with my coding or project creation! Having created several projects and compared with former projects, I see no fault with my doing. Who has an idea?

You need to integrate Kotlin Android Extensions in your project in order to you have your views recognized like that. Just add the following lines in your build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
More info in this article: https://antonioleiva.com/kotlin-android-extensions/
BUT
synthetic will be deprecated in Kotlin 1.4.20, https://youtrack.jetbrains.com/issue/KT-42121 due to not being null-safe. If you are going to maintain the project you are building for a awhile it will be batter not to use Kotlin Android Extensions.
Suggested alternatives in the link above are:
ViewBinding
FindViewById
Kotterknife (deprecated)
AndroidAnnotations
FindViewById is the 'official' method and it's easy to use, Kotlin suggests ViewBinding which has some boilerplate as I read (I haven't used it yet).

just add these lines in your build.gradle (app) at the top:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions' }
after that include this import statements in your MainActivity.kt file:
import kotlinx.android.synthetic.main.activity_main.*
for more clear info visit
`https://youtrack.jetbrains.com/issue/KT-42121`
After that don't forget to sync your gradle file..

As "kotlin-android-extensions" is deprecated now it's better to use view binding.
For this first enable view binding in build.gradle file by writing these line of codes under android block.
buildFeatures {
viewBinding true
}
then in activity file to use view binding features
first declare a global variable for binding as
private lateinit var binding: ActivityHomeBinding
Here ActivityHomeBinding is a auto generated class
then write these codes in OnCreate() method
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)

Related

Definite bug in Android Studio regarding Spinners and the `clickable` attribute?

In my app, I want to make my spinners clickable=true or clickable=false at different times. But, despite setting the attribute both in the Attributes Inspector and in code, Android Studio simply does not respect the clickable attribute.
(I do NOT want to enable/disable the spinners, because that changes the way they are displayed, ie. greyed out.)
Here's a bare-bones project to show....
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="#+id/spinnerA"
android:layout_width="0dp"
android:layout_height="50dp"
android:clickable="false"
android:entries="#android:array/organizationTypes"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spinnerB"
android:layout_width="0dp"
android:layout_height="50dp"
android:entries="#android:array/imProtocols"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinnerA" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.heewhack.clickablespinners
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinnerB.isClickable = false
}
}
This seems to be a major bug in Android Studio...
Questions:
Can someone confirm my findings, or better yet, show me what I'm missing?
Does anyone know a work-around?
Can I change the way enabled/disabled items are displayed?
How should I report this bug, if it's actually a bug?

EditText with hint, where hint moves

I have seen EditText that has a hint and when user taps on it the hint moves up reducing the font size and making the field editable by the user. When the user moves to another EditText leaving it blank the hint appears back in full size.
What is it and how can I add it in my activity on Android Studio using XML?
I am on Android Studio using Kotlin.
if you mean this
it is TextInputLayout
Add the dependency for the design support library inside the build.gradle (Module: app) file as shown below.
implementation 'com.google.android.material:material:<version>'
the latest version at this time is 1.2.0-alpha03 you can see latest version from mvnrepository
Then, you can use it like this in your xml layouts.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/myTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/my_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/YOUR_HINT"
android:singleLine="true"
android:textColor="#color/colorDarkestGray" />
</com.google.android.material.textfield.TextInputLayout>
This is TextInputLayout and you can use editText as child in it like this :
At first add the dependency for the design support library inside the build.gradle file as shown below.
implementation 'com.android.support:design:25.3.1'
And if you are using AndroidX use this :
implementation 'com.google.android.material:material:1.0.0'
Second ,Then implement that like this :
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/activity_horizontal_margin">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Enabled Default" />
</android.support.design.widget.TextInputLayout>
Android TextInputLayout Features
1-Enabling/Disabling floating hints
2-Enabling/Disabling floating hint animation
3-Displaying Error Messages
4-Showing Character Counter
5-Password Visibility Toggle and . . .

Android Studio can't find classes for MotionLayout

I'm trying to use android MotionLayout, but it can't find appropriate classes for it and wont work.
I tried implementing the below dependencies:
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha4'
but none of them seems to work...
here is my XML file:
<android.support.constraint.motion.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
footstools="http://schemas.android.com/tools"
android:id="#+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showPaths="true"
tools:context=".PlayGameActivity">
<View
android:id="#+id/button"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#color/colorAccent"
android:text="Button" />
</android.support.constraint.motion.MotionLayout >'
but it gives me always this error massage:
Missing classes
The following classes could not be found:
- android.support.constraint.motion.MotionLayout (Fix Build Path, Edit XML, Create Class)
Tip: Try to build the project.
Tip: Try to refresh the layout.
and in the preview window of the projects it fills the entire layout in gray and writes in the center:
android...MotionLayout
There is no android.support.constraint.motion.MotionLayout
in implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta2".
It's called androidx.constraintlayout.motion.widget.MotionLayout.
If MotionLayout shows up gray, you might be lacking the app:layoutDescription XML.
There's an example for AndroidX & Support Library versions.

Cannot Resolve Symbol R Upon New Project Creation

There seem to be many threads addressing this question, but none have worked for me. My problem is that whenever I start a new project, R is highlighted red, and the project will not run.
Luckily - for the sake of simplicity - I have not implemented any code, all the code in the project is simply the code created by the creation of a new project:
package com.abc.android.unnamed;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The XML file remains unchanged:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.android.unnamed.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I have tried:
Deleting the project files and creating a new project
Doing Build > Clean Project, then Build > Rebuild Project
All of which yield the same result.
Each time, the Gradle Console displays:
What can I do to fix this issue?
After some digging, I fixed my problem. My antivirus (Bitdefender) was not giving access to aapt2.exe, as seen in the error from the Gradle Console.
This was not immediately obvious as I did not see any notification that it was blocked. Additionally, sample code was able to resolve R without any fuss, so I was confused why I was not able to get it working. After allowing aapt2.exe access, with a Gradle rebuild, there is no longer an issue.
I'm just gonna add this here as i can't comment yet, for anyone experiencing the same issue.
I think Windows Defender does the same thing. As soon as i shut off the real-time protection the symbol R was resolved.

How to use a ShapeDrawable with an xml layout resource

I would like to be able to define a ShapeDrawable in my layout file and then inflate that so I can draw on it.
I have followed the tutorial on the android developers' site but I cannot see how I reference my ShapeDrawable in my main class file.
How do I actually inflate it?
I have put the following the following into my layout file:
<com.example.shapedrawable.CustomDrawableView
android:id="#+id/customDrawableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Once you have defined the ShapeDrawable in its own XML file, you need to add the following to the layout.xml where you want it to be included:
<ImageView
android:src="#drawable/shapeDrawable_filename"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you want to do it programmatically, I don't have an answer (yet), but from your question, it seems this should work!
Let me know if that helps ;)

Resources