import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.widget.*
import android.os.Bundle
import android.R
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_main2)
val activity_btn = mainbtn
activity_btn.setOnClickListener(View.OnClickListener {
val intent = Intent(this, activity_main2::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
})
}
}
I have tried clean project,reopening project,rebuild project etc but none of these have worked.
It says Unresolved reference:activity_main,slide_in_right,slide_out_left etc.
remove line import android.R; from your code and rebuild again
Related
I am trying out a very simple intent example follow this youtube video. However, i facing a very weird error where this particular line cannot work:
Intent myIntent = new Intent(this, DisplayActivity.class)
It provide me error as shown in the picture:
Error
I also had tried out the "bulb" button in AS to debug it but it didn't show me a valid solution. The suggested action is as shown in the picture Original AS code editor image
The full code is shown below:
package com.example.parcelsort_ar
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.budiyev.android.codescanner.*
import com.example.parcelsort_ar.databinding.ActivityMainBinding
import android.content.Intent
private const val CAMERA_REQUEST_CODE = 101
class MainActivity : AppCompatActivity() {
private lateinit var codeScanner: CodeScanner
private lateinit var binding: ActivityMainBinding
val btn_click_me = findViewById(R.id.idBtnPost) as Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//View binding
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.idBtnPost.setOnClickListener {
openActivity2();
}
setupPermission()
codeScanner()
}
public fun openActivity2() {
Intent myIntent = new Intent(this, DisplayActivity.class);
startActivity(intent);
}
}
I had spending almost a day searching online but couldn't found any issue that meet my problem. Any help is aprreciated.
Intent myIntent = new Intent(this, DisplayActivity.class); is Java, not Kotlin. So it's getting to that first Intent and it doesn't know what to do with it.
You want this:
val myIntent = Intent(this, DisplayActivity::class.java)
The IDE should have caught it and offered to convert it to Kotlin, if you pasted the line in
moving from activity to activity
val myIntent = Intent(this, DisplayActivity::class.java) //you will use this code
moving from fragment to activity
val myIntent = Intent(requireContext(), DisplayActivity::class.java)
I want to send an editText string from a dialogFragment popup to the activity screen behind it. I'm not sure how to do this in Kotlin since most resources online are in Java. I've tried to implement: How to send data from dialog to my activity kotlin?. However, I couldn't quite get it work so I'm not sure what method to use from here.
Here is the SelectAPScreen.kt (the activity I want the string to go to)
package com.wcsng.dlocapp
import android.annotation.SuppressLint
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.findNavController
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.imu_data.*
import java.io.BufferedReader
import java.io.InputStreamReader
class SelectAPScreen : AppCompatActivity(), OnButtonClick {
#SuppressLint("ResourceAsColor")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_select_apscreen)
val directoryName = findViewById<EditText>(R.id.dir_field)
val pingIP = findViewById<EditText>(R.id.ip_field)
val rpiIP = findViewById<EditText>(R.id.rpiField)
val qtnIPs = {}
val addQtn = findViewById<Button>(R.id.addQtn)
addQtn.setOnClickListener {
var dialog = AddQtnFragment()
dialog.show(supportFragmentManager, "Add Qtn Popup")
}
}
}
Here is the DialogFragment Code:
package com.wcsng.dlocapp
import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.findNavController
import com.wcsng.dlocapp.R
class AddQtnFragment: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater
val qtn_ip = view?.findViewById<EditText>(R.id.qtn_ip)?.text.toString()
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.add_qtn_popup, null))
.setPositiveButton("Save", DialogInterface.OnClickListener{dialog, id ->
// TODO Return the qtn_ip to SelectAPScreen
// Close the dialog and return back to the parent activity
dialog.dismiss()
})
.setNegativeButton("Cancel", DialogInterface.OnClickListener{dialog, id ->
dialog.dismiss()
})
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
you can use getActivity() or requireActivity() methods for obtaing managing Activity, then you may cast it to proper class, e.g. (requireActivity() as SelectAPScreen) and now you can call methods of it
(requireActivity() as SelectAPScreen).selectAPScreenMethod()
selectAPScreenMethod() is a custom method placed inside SelectAPScreen class
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.*
Im learning Kotlin and Mvvm for Android. I am using a recycler view, and when i try to set the adapter i cant import the Adapter class, I dont know if the problem is in the code because Android Studio let me import ViewHolder class inside the Adapter class but not Adapter class
FrontPageActivity.kt
package com.jmyp.resport.view
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.jmyp.resport.model.New
import com.jmyp.resport.R
import com.jmyp.resport.viewmodel.NewViewModel
class FrontPageActivity : AppCompatActivity() {
lateinit var adapter : NewsAdapter // I can not import this but i can
// NewsAdapter.NewViewHolder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_front_page)
var viewModelNews = ViewModelProviders.of(this).get(NewViewModel::class.java)
viewModelNews.getNews().observe(this, Observer<ArrayList<New>> { news ->
adapter = NewsAdapter(this, news)
})
}
}
NewsAdapter.kt
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jmyp.resport.model.New
import com.jmyp.resport.R
import com.jmyp.resport.view.FrontPageActivity
import kotlinx.android.synthetic.main.row_front_page.view.*
class NewsAdapter(private val context: FrontPageActivity, private val news : ArrayList<New>) : RecyclerView.Adapter<NewsAdapter.NewViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewViewHolder {
return NewViewHolder(LayoutInflater.from(context).inflate(R.layout.row_front_page,parent,false))
}
override fun getItemCount(): Int {
return news.size
}
override fun onBindViewHolder(holder: NewViewHolder, position: Int) {
holder.title.text = news.get(position).titulo
}
class NewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val cover = itemView.iv_cover
val title = itemView.tv_title
}
}
You seem to be missing package in your Adapter class. Add it and try again :)
Can anyone tell me what is the problem. This is the code:
package com.mohdjey.user.inflate
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.mohdjey.user.inflate.R.id.root_layout
import com.mohdjey.user.inflate.R.layout.activity_main
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_main)
var inflater: LayoutInflater? = null
var view: View? = null
// inflater.inflate(R.layout.child_layout_to_merge, parent_layout, true);
view = inflater?.inflate(R.layout.another_view , null)
val layout = findViewById<LinearLayout>(R.id.root_layout)
layout.addView(layout)
} }
I am practicing layout inflate.
I don't Know what to write.
You're trying to add the LinearLayout with the ID root_layout as its own child here:
layout.addView(layout)
Perhaps you meant to add your newly inflated View as its child?
layout.addView(view)
Your entire code block should just be
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(activity_main)
val root = findViewById<LinearLayout>(R.id.root_layout)
val view = layoutInflater.inflate(R.layout.another_view, root, false)
root.addView(view)
}
That being said, it's not clear why you don't just include your R.layout.another_view directly in your main layout.