I have looked through many examples but i really cant figure this out. I literally just started working with Kotlin and android studio. Any help would be appreciated
Hey is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_background"
android:gravity="center"
android:padding="30dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Enter_No"
android:textSize="22sp"
android:inputType="phone"
/>
<Button
android:id="#+id/btnLogin"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#fff"
android:textColor="#1495d7"
android:text="#string/Sign_in" />
<Button
android:id="#+id/FrgtPass"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/Frgt_pass_prmt"
android:textAllCaps="false" />
</LinearLayout>
MainActivity.kt
package com.example.hackapp1
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)
btnLogin.setOnClickListener{}
}
}
Maybe, you did not apply kotlin-android-extension plugin.
It was applied automatically, but not now.
kotlin-android-extension has some issues. It will be deprecated in near future.
See details here.
Instead of kotlin-android-extension ViewBinding and DataBinding is recommended.
You can simply enable like following:
android {
...
viewBinding { enabled = true }
dataBinding { enabled = true }
}
See below links for details.
DataBinding
ViewBinding
Related
I'm writing a simple tip calculator app in android studio ( which is one of the programs in the https://developer.android.com/courses/android-basics-kotlin/course course ).
But I'm getting an unexpected error, unresolved reference: toDouble, when I try to covert a string to a double. Even after pasting the solution code from the website into my application, the error won't go away. I also tried first converting the string to an int using toInt function but I get the same error.
Here is the code inside MainActivity.kt.
package com.example.tiptime
import java.text.NumberFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.tiptime.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.calculateButton.setOnClickListener{ calculateTip() }
}
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
}
Here is my code in 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"
android:padding="16dp"
tools:context=".MainActivity"
tools:ignore="UseSwitchCompatOrMaterialXml">
<EditText
android:id="#+id/cost_of_service"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="#string/cost_of_service"
android:inputType="numberDecimal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="#string/cost_of_service" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_was_the_service"
android:id="#+id/service_question"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cost_of_service"/>
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/tip_options"
android:checkedButton="#+id/option_twenty_percent"
app:layout_constraintTop_toBottomOf="#id/service_question"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_twenty_percent"
android:text="#string/amazing_20"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_eighteen_percent"
android:text="#string/great_18"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_fifteen_percent"
android:text="#string/ok_15"/>
</RadioGroup>
<Switch
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/round_up_switch"
android:text="#string/round_up_tip"
android:checked="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tip_options" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:id="#+id/calculate_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/round_up_switch"
/>
<TextView
android:id="#+id/tip_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tip_amount"
app:layout_constraintTop_toBottomOf="#id/calculate_button"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the screenshot of the error
Any help is appreciated!!
Seems like you do not have the latest kotlin support .
Update the Kotlin version to the latest one from :
File ->> Setting -->> Language & Frameworks -->> Kotlin ...
Try after removing
import java.text.NumberFormat
comment
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
build & clean
uncomment the code in above method you will get correct import
Use
val cost = Double.parseDouble(stringInTextField)
I want to take Input from user(their name) with editableText just like the below video(watch at 8:24). But, I am not able to take input because android studio is not recognizing "editableText". Am i missing something?
Android Development Tutorial Video at 8:24
Error ->>
Unresolved reference: nameInput [In the below MainActivity.kt]
MainActivity.kt
package com.example.firstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun createBirthdayCard(view: View){
val name = nameInput.**editableText**.toString() <-- [error is in this line]
}
}
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">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:text="Enter Name"
android:textColor="#000000"
android:textSize="50sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:textAlignment="center" />
<EditText
android:id="#+id/nameInput"
android:layout_width="280dp"
android:layout_height="54dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
tools:textAlignment="center" />
<Button
android:id="#+id/createBirthdayCard"
android:layout_width="307dp"
android:layout_height="81dp"
android:layout_marginTop="144dp"
android:maxLines="1"
android:text="Create Birthday Card"
android:textSize="19sp"
android:onClick="createBirthdayCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/nameInput"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
:) :) Thanks for spending your valuable time to help me :) :)
Android kotlin extensions removed at Android studio 4.1V.
I recommend use the ViewBinding or DataBinding.
This is a good reference.
https://android-developers.googleblog.com/2020/11/the-future-of-kotlin-android-extensions.html
ViewBinding : https://developer.android.com/topic/libraries/view-binding?hl=ko
DataBinding : https://developer.android.com/topic/libraries/data-binding
As Android kotlin extensions is deprecated and removed you may use
View binding, Data binding as suggested by Seungho Kang
OR you can declare variable as shown below.
fun createBirthdayCard(view: View){
val editText=findViewById<EditText>(R.id.nameInput)
val name = editText.editableText.toString()
}
I have made a simple Android-UI. Actually I started with the spinner. The button I added only for counterchecking, after the spinner didn't work.
<?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/spinner2"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:entries="#array/months"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="156dp"
android:text="Test ..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner2" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.mizech.playground
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
testButton.setOnClickListener {
Toast.makeText(this, "Testing 123 ...", Toast.LENGTH_LONG).show();
}
}
}
The strings.xml (although I think it isn't relevant):
<resources>
<string name="app_name">Playground</string>
<string-array name="months">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
</string-array>
</resources>
The emulator is like frozen. Neither the spinner nor the button react to any interaction.
On a physical device all is fine. The spinner expands it's list when clicked. The button works as well.
What can be the cause, that the emulator behaves suddenly that way? How can it be fixed?
I have used this virtual device for some time. The described problems came out of the sudden.
Maybe your minsdk and maxsdk versions are not matching with the emulators minsdk and maxsdk.
When this program runs on the emulator it works, however, when I run it on my phone the timePicker is referenced as null.
An emulator running API 26 can run it fine, however, at API 28 it does not work. So I think It might be something with API 28 (which also is what my phone runs)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val picker = findViewById<TimePicker>(R.id.timePicker)
val t = picker == null // Is true on phone and is false on emulator
Toast.makeText(this#MainActivity, t.toString(), Toast.LENGTH_LONG).show()
//picker.setIs24HourView(true)
val activity = findViewById<EditText>(R.id.activityName)
val mCallApiButton = findViewById<Button>(R.id.submit)
mCallApiButton?.setOnClickListener{
val time = GetTime(picker)
PostActivity(time, activity.text.toString()) // Function that does other stuff
}
}
}
I need to reference the timePicker so that I can set it to 24H.
Here is a look at the layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent">
<TimePicker android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/activityName"
android:singleLine="true" android:hint="Activity" android:textSize="18sp"
android:textAlignment="center" android:autofillHints="Activity"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/submit"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot of the emulator: (Which is how it should look)
Screenshot of the phone:
This is because I use API 28 and not 27 and under. The answer can found here:
https://github.com/xamarin/Xamarin.Forms/issues/5159
I have enabled databinding, but while I execute the code I get this error.
error
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
I created a fragment class and XML for that class.
Im able to import datbindingutil class.
I have done rebuilt/ sync with gradle files/ invalidate cache and restart, nothing worked.
xml
<layout>
<!--suppress AndroidUnknownAttribute -->
<data class=".databinding.ProfileFragmentBinding">
<variable
name="user"
type="com.sample.sample.user.User" />
<variable
name="vm"
type="com.sample.sample.user.UserViewModel" />
<variable
name="handler"
type="com.sample.sample.user.profile.ProfileFragment" />
</data>
<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">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profileIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/medium"
android:layout_marginTop="#dimen/medium"
android:contentDescription="#null"
android:src="#mipmap/ic_launcher_round"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:url="#{user.avatarUrl}" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="#+id/profileIV"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/profileIV">
<TextView
android:id="#+id/profileNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/myriad_pro_semibold"
android:text="#{user.name}"
android:textColor="#color/black_transparent_de"
android:textSize="#dimen/text_regular"
tools:text="NAME" />
<TextView
android:id="#+id/badgeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/myriad_pro_semibold"
android:text="#{user.badge}"
android:textColor="#color/grey_000000"
android:textSize="#dimen/text_regular"
tools:text="Superman" />
<TextView
android:id="#+id/profile_Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto_bold"
android:text="#{user.badge}"
android:textColor="#color/green_39b54a"
android:textSize="#dimen/text_small"
tools:text="farmer_v1" />
</LinearLayout>
<ImageView
android:id="#+id/badgeIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/medium"
android:layout_marginTop="#dimen/medium"
android:contentDescription="#null"
android:src="#mipmap/ic_launcher"
app:error="#{#drawable/ic_profile_default_grey_24dp}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholder="#{#drawable/ic_profile_default_grey_24dp}"
app:url="#{user.badgeUrl}" />
<ImageView
android:id="#+id/locationPinIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/medium"
android:contentDescription="#null"
android:src="#drawable/ic_location_pin"
app:layout_constraintStart_toStartOf="#+id/profileIV"
app:layout_constraintTop_toBottomOf="#+id/profileIV" />
<TextView
android:id="#+id/profileAddressTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/narrow"
android:fontFamily="#font/roboto"
android:textColor="#color/grey_000000"
app:layout_constraintBottom_toBottomOf="#+id/locationPinIV"
app:layout_constraintLeft_toRightOf="#+id/locationPinIV"
app:layout_constraintTop_toTopOf="#+id/locationPinIV"
tools:text="bangalore, Karnataka" />
<ImageView
android:id="#+id/dobIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/standard"
android:layout_marginTop="#dimen/medium"
android:contentDescription="#null"
android:src="#drawable/ic_dob"
app:layout_constraintLeft_toRightOf="#+id/profileAddressTV"
app:layout_constraintTop_toBottomOf="#+id/profileIV" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/narrow"
android:fontFamily="#font/roboto"
android:textColor="#color/grey_000000"
app:layout_constraintBottom_toBottomOf="#+id/locationPinIV"
app:layout_constraintLeft_toRightOf="#+id/dobIV"
app:layout_constraintTop_toTopOf="#+id/locationPinIV"
tools:text="born on 01/01/2000" />
<TextView
android:id="#+id/activityLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/big"
android:fontFamily="#font/myriad_pro_semibold"
android:text="#string/activities"
android:textColor="#color/black_transparent_de"
android:textSize="#dimen/text_regular"
app:layout_constraintStart_toStartOf="#+id/profileIV"
app:layout_constraintTop_toBottomOf="#+id/locationPinIV" />
<View
android:id="#+id/dividerV"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="#dimen/small"
android:layout_marginStart="#dimen/small"
android:layout_marginTop="#dimen/regular"
android:background="#color/grey_000000"
app:layout_constraintTop_toBottomOf="#+id/activityLabel" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/dividerV">
<!--<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:pager="#{(pager)}"
app:tabGravity="fill"
app:tabIndicatorColor="#color/black"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/black"
app:tabTextAppearance="#style/CustomTextTab"
app:tabTextColor="#b4ffffff" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tablayout"
app:handler="#{handler}"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />-->
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
class
class ProfileFragment : Fragment() {
#Inject
lateinit var mFactory: ViewModelProvider.Factory
private lateinit var mBinding: ProfileFragmentBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_profile, container, false);
return mBinding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val vm: UserViewModel = getViewModel(mFactory)
mBinding.vm = vm
//mBinding.handler = this
//mBinding.setLifecycleOwner(this)
}
/*#BindingAdapter("bind:handler")
fun bindViewPagerAdapter(view: ViewPager, activity: MainActivity) {
val adapter = ProfilePagerAdapter(view.context, activity.supportFragmentManager)
view.adapter = adapter
}
#BindingAdapter("bind:pager")
fun bindViewPagerTabs(view: TabLayout, pagerView: ViewPager) {
view.setupWithViewPager(pagerView, true)
}*/
}
in my case I was able to find it when the mouse was hovering that line in the build output, as shown here:
without hover:
with hover:
it's really a shame how they show the error, for the simplest error ever, I was trying 10 different solutions as well invalidating the cache and ...
UPDATE:
you can also click here :
and you'll get something like this:
which is very detailed information about the error, I was missing this button in 7 years of Android Development :D
Run ./gradlew build --stacktrace to check the details, which will tell you where the issue happens, something like:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
Could not find accessor xx
file:xxx/app/src/main/res/layout/fragment_xxxx.xml Line:108
Sometimes if you changed the property name, especially when changed by refactor => rename, the property name won't be changed in xml automatically.
Mostly this error occurs when the name of the variable passed in the XML file through data binding is incorrect. Just hover over the mouse on the error and you would be able to identify the culprit variable there.
if it happen when change jdk versions, delete all build folders in every module that you have on the project,
It's fixed my issue.
delete caches folder to
finder -> home -> click on "command +shift + ." to see hidden folders -> .gradle -> caches