I am new to kotlin, I was wondering how to make a get HTTP request onCreate function
package com.example.kotlin
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)
//http request here
}
}
Related
why can't I access a variable from another class? (the variable is in the data class, and when I want to access it, it throws "unresolved reference")
Here's what It looks like:
The code that tries to access the variable:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = usernameStore.username //here's the error, the "username" thing is red
nameDisplay.setText(name2)
}
}
The data Class:
package com.ketchup.myquizzies
public data class usernameStore (
var username: String
)
Any help appreciated guys, I literally searched all things that came to my mind to solve this, but I couldn't help myself :(
Android Studio, Kotlin
usernameStore is a class, not an object. You need to create an instance of usernameStore to be able to use it.
class questionActivity : AppCompatActivity() {
val store = usernameStore()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = store.username
nameDisplay.setText(name2)
}
}
FWIW, creating instances of classes is covered by this section of this chapter of this free book.
You have not created instance of class
usernameStore
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
// replace with below code
var name2 = usernameStore().username
nameDisplay.setText(name2)
}
}
I don't know why text is not changing in the BirthdayGreetingActivity.
No error is coming but it's not changing text. I checked(by Logcat) that name has came successfully to BirthdayGreetingActivity.kt from MainActivity.kt.
I guess that this [binding.birthdayGreeting.text = "Happy Birthday $name"] line in BirthdayGreetingActivity.kt is not working properly.
MainActivity.kt --
package com.example.firstapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.firstapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
fun createBirthdayCard(view: View){
val name = binding.nameInput.editableText.toString()
// Toast.makeText(this,"Name is $name",Toast.LENGTH_SHORT).show()
val intent = Intent(this,BirthdayGreetingActivity::class.java)
intent.putExtra(BirthdayGreetingActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
BirthdayGreetingActivity.kt --
package com.example.firstapp
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.firstapp.databinding.ActivityBirthdayGreetingBinding
class BirthdayGreetingActivity : AppCompatActivity() {
private lateinit var binding: ActivityBirthdayGreetingBinding
companion object{
const val NAME_EXTRA ="name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBirthdayGreetingBinding.inflate(layoutInflater)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
// val greet = binding.birthdayGreeting.editableText.toString()
// print(name)
// if (name != null) {
// Log.d("greet = ",binding.birthdayGreeting.editableText.toString())
// }
binding.birthdayGreeting.text = "Happy Birthday $name" [maybe something wrong here]
}
}
:) :) Thanks for spending your valuable time to help me :) :)
On BirthdayGreetingActivity you are passing the layout ID for setContentView, instead you must pass the binding.root, just like you did on MainActivity...
Change this line:
setContentView(R.layout.activity_birthday_greeting)
To:
setContentView(binding.root)
Please help me figure out this Unresolved Error.
The code below gives an
Unresolved reference: text
below code is of
birthdayGreeting.kt
package com.example.birthdaygreet
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class BirthdayGreeting : AppCompatActivity() {
companion object{
const val NAME_EXTRA = "name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
BirthdayGreeting.text="happyBirthday$name"
}
}
The error according to android studio is in
birthdayGreeting.text = "Happy Birthday $name"
Here is code of main activity
package com.example.birthdaygreet
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
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()
val intent = Intent(this,BirthdayGreeting::class.java)
intent.putExtra(BirthdayGreeting.NAME_EXTRA,name)
startActivity(intent)
}
}
You are using the wrong synthetic import for the BirthdayGreeting class. You should use kotlinx.android.synthetic.main.activity_birthday_greeting.* instead of the kotlinx.android.synthetic.main.activity_main.*
package com.example.birthdayapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.birthdayapp.databinding.ActivityMainBinding
class birthdayActivity : AppCompatActivity() {
private lateinit var binding: birthdayActivity
//using companion object
companion object {
const val NAME_EXTRA="Name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//implementing view binding
binding= birthdayActivity.inflate(layoutInflater) //here red line shows below inflate
setContentView(binding.root) //here red line shows below root
//getting name from main activity
val name=intent.getStringExtra(NAME_EXTRA)
}
}
//MainActivity
package com.example.birthdayapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.birthdayapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater) //Here, I have successfully used viewbinding
setContentView(binding.root)
binding.birthdaybutn.setOnClickListener {
val name=binding.nam.editableText.toString()
val intent=Intent(this,birthdayActivity::class.java)
intent.putExtra(birthdayActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
}
I have successfully used viewbinding in MainActivity but when I tried to use viewbindig in birthdayActivity , i got red lines for ( inflate and root keyword ) ,because i have used companion object in birthdayActiviy whereas I have not companion object in MainActiviy
I add a textView in layout but when I'm trying to use Kotlin Android Extensions but I get Unsolved reference error on my TextView:
package com.normal.ff
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)
}
}
fun first(a:Int, b:Int){
textView.text
}
Add import kotlinx.android.synthetic.main.activity_main.* at the top of your activity.
You are almost there, you just need a reference from layout file for that text view.
something like below
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById<TextView>(R.id.textView)
}
}
fun first (a:Int, b:Int){
val text = textView.text
}