Why I have no logs by my tags? No errors or something like that.
Trying to get device info, using Kotlin.
class MainActivity : AppCompatActivity() {
var webview: WebView? = null
var _MODEL = Build.MODEL
var _MANUFACTURER = Build.MANUFACTURER
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("myTag", _MANUFACTURER)
Log.d("myTag2", _MODEL)
webview = findViewById<View>(R.id.browser) as WebView
webview!!.loadUrl("https://stackoverflow.com")
}
}
Related
The code I wrote is not working because viewbinding is not suitable. I got some help from my friend but I still couldn't do the application please help me my application is interrupted
/*MainActivity*/
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding?=null//
private val binding
get()=_binding!!
override fun onCreate(savedInstanceState: Bundle?) {
_binding=ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.apply{ //5.işlemimiz ;)
editText
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun aktiviteDegistir (view:View){
val kullaniciVerisi = editText.text.toString()
val intent = Intent(applicationContext,IkinciActivity::class.java)
intent.putExtra("yollananVeri",kullaniciVerisi)
startActivity(intent)//activiteyi başlatım güzel yer
}
/*Activity2*/
val intent = intent//intent
val alinanVeri = intent.getStringExtra("yollananVeri")
textView2.text = alinanVeri
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding?=null//3.islemimiz
private val binding
get()=_binding!!
override fun onCreate(savedInstanceState: Bundle?) {//4.işlemiiz
_binding=ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.apply{ //5.işlemimiz ;)
editText
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
After adding this code, I waited for the messages to go away, but they didn't.
my application will write something about this to the editText button will be pressed then it needs to come to the textview part in the other activity
you are following approach, that is used to bind in fragment binding. The problem is that you are setting the binding data but not connecting it with the content view. for better approach in activity view binding just the same for more cleaner activity binding.
class MainActivity : AppCompatActivity() {
private lateinit var binder : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binder = ActivityMainBinding.inflate(layoutInflater)
setContentView(binder.root)
viewImpl()
}
/**you view calls...
* like button.setonclick() and all UI related interactions...
*/
private fun viewImpl() {
binder.run {
//here...
}
}
}
In here I have created binding lateinit var for activity binder which will be initialise when binder = ActivityMainBinding.inflate(layoutInflater) and then using that binder reference for setting my content view as setContentView(binder.root).
Happy Coding ✌️.
Why I'm getting a null string through Intent even after passing the value?
MainActivity
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnFirst.setOnClickListener {
val name=binding.etName.text.toString()
Intent(this,SecondActivity::class.java).also{
it.putExtra("EXTRA_NAME",name)
startActivity(it)
}
}
}
}```
SecondActivity
```class SecondActivity:AppCompatActivity() {
private lateinit var binding: ActivitySecondBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivitySecondBinding.inflate(layoutInflater)
setContentView(binding.root)
val name=intent.getStringArrayExtra("EXTRA_NAME")
val toPrint="$name hahahaha"
binding.tvNameIntent.text=toPrint
}
}
toPrint is getting "null hahahaha"
Can someone please rectify my error
You are putting in a String so you should be also reading a String and not StringArray. You can use e.g. getStringExtra() for that.
val name=intent.getStringArrayExtra("EXTRA_NAME") should be changed to
val name=intent.getStringExtra("EXTRA_NAME")
MainActivity.kt
PutExtra(String!.String?) is givng error (in android Kotlin), what could be the problem?
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
var name:EditText? = null
fun createBirthdayCard(view: View) {
name = findViewById(R.id.NameInput)
val intent = Intent(this,BirthdayGreetActivity::class.java)
intent.putExtra(BirthdayGreetActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
when sending data
intent.putExtra("name",name)
when receiving data
val name= intent.getStringExtra("name").toString()
intent.putExtra(BirthdayGreetActivity.NAME_EXTRA,name)
this line of yours adds NameInput to intent not string. I dont know what your NameInput is but you may try name.text. Maybe if you gave the code of NameInput I might help better.
I have an activity that has a button. On the button click I want to update text in text view.
I want to use ViewBinding instead of the normal findViewById
This is how I created the val binding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
Now in rollDice I want to update the text view but I'm not able to access binding which make sense because its scope is limited to onCreate() , so what is the best practice for this?
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
You have two options.
1. Store in a property
Since the inflated content of Activity is fully bound to it's lifecycle, it's safe to keep the reference as a property
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
2. Pass the binding to the methods
That's what I usually do, it avoids creating a property where it's not really a necessity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice(binding)
}
}
private fun rollDice(binding: ActivityMainBinding) {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
Both options are valid ways to make the binding visible to Activities methods.
Store the binding in an instance variable on the Activity. Then you have access to it from all the methods in the Activity.
As the question has accepted answer and it is already addressed but here is my approach to the viewBinding
class MainActivity : AppCompatActivity() {
private val binding by lazy{
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
I go with lazy initialization of binding so that way it is only intitialized if it is required.
More you can read about lazy initialization here
https://www.baeldung.com/kotlin/lazy-initialization
I am new in Android development and I provided my code snippet.
I'm wondering why is it saying other must not be null
other = findViewById(R.id.otherId)
My Activity:
class SplashScreen : AppCompatActivity() {
lateinit var topAnimation:Animation
lateinit var bottomAnimation:Animation
lateinit var logoImage:ImageView
lateinit var bangerText:TextView
override fun onCreate(savedInstanceState: Bundle?) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
topAnimation=AnimationUtils.loadAnimation(this,R.anim.top_animation)
bottomAnimation=AnimationUtils.loadAnimation(this,R.anim.bottom_animation)
logoImage=findViewById(R.id.logoImage)
bangerText=findViewById(R.id.bangerTextView)
val other=findViewById<TextView>(R.id.textView5)
logoImage.animation=topAnimation
bangerText.animation=bottomAnimation
other.animation=bottomAnimation
}
}
Can you check whether layout/activity_splash_screen.xml actually has id/textView5?
If that id is not specified in that layout, findViewById might return null