Jaydip Kalkani
Jaydip Kalkani

Reputation: 2607

Access edittext Of Activity from BroadcastReceiver Class in android using kotlin

I want to disable my edittext field if no internet connection is available. So, for that, i had make broadcastreceiver class to detect network change activities and when i receive broadcast that no internet connection is available, at that time i want to disable my edittext field and as internet connection is made on by user the field should be enabled automatically. Most important thing is that i am doing this thing using kotlin and i had found many solutions but all are for java. So, please help me how can i do that. I had tried to get class instance for accessing edittext like below

var activityInstance = SetProfileActivity::class.objectInstance

and by using activityInstance object i tried to access my edittext like below

activityInstance.myEtId.setEnabled(false)

but i can't access it and it returns null. How can i do this?

Upvotes: 0

Views: 647

Answers (1)

Jaydip Kalkani
Jaydip Kalkani

Reputation: 2607

I got Solution....I have to register my receiver in my activity class and important thing is that declare broadcastreceiver class as inner class to access elements of activity.

e.g.

inner class InternetOnReceiver : BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        etLocationPicker.hint = "Pick Location"
        etLocationPicker.isEnabled = true
    }
}

Upvotes: 1

Related Questions