My title is not displaying in the app.
I think there is a problem with the intent.
So, my main file is the login page and the code is:
class MainActivity : AppCompatActivity()
{
lateinit var username: EditText
lateinit var password1: EditText
lateinit var logIn: Button
lateinit var signup: TextView
lateinit var forgotpassword: TextView
val user="DiyaK"
val pass=arrayOf("Diya#2826","Sidd#2826","Prat#2826","Prash#2826")
var titlename: String? = "DiyaK"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scroll)
println("Create called")
title = "Login Page"
username=findViewById(R.id.username)
password1=findViewById(R.id.password1)
logIn=findViewById(R.id.logIn)
signup=findViewById(R.id.signup)
forgotpassword=findViewById(R.id.forgotpassword)
logIn.setOnClickListener {
val user1= username.text.toString()
val pass1=password1.text.toString()
var name="Diya K"
if(user1==user) {
if (pass1 == pass[0]) {
val intent = Intent(this#MainActivity, NewActivity::class.java)
startActivity(intent)
name="Diya K"
intent.putExtra("Name",name)
} else if (pass1 == pass[1]) {
val intent2 = Intent(this#MainActivity, NewActivity6::class.java)
startActivity(intent2)
name="Siddhant K"
intent2.putExtra("Name",name)
} else if (pass1 == pass[2]) {
val intent3 = Intent(this#MainActivity, NewActivity3::class.java)
startActivity(intent3)
name="Pratibha K"
intent3.putExtra("Name",name)
} else if (pass1 == pass[3]) {
val intent4 = Intent(this#MainActivity, NewActivity4::class.java)
startActivity(intent4)
name="Prashant K"
intent4.putExtra("Name",name)
}
} else {
Toast.makeText(this#MainActivity, "Incorrect! Try again", Toast.LENGTH_SHORT)
.show()
}
}
}
}
And, I have created 4 new activities, like for each different page and the code for all are similar, so I am showing the code for one only.
class NewActivity : AppCompatActivity() {
lateinit var img: ImageView
lateinit var state: TextView
lateinit var btn: Button
lateinit var btn1: Button
lateinit var header: TextView
lateinit var subheader: TextView
lateinit var hobby: TextView
var title1: String? = "Diya K"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new1)
img = findViewById(R.id.img)
state = findViewById(R.id.state)
btn = findViewById(R.id.btn)
btn1 = findViewById(R.id.btn1)
header = findViewById(R.id.header)
subheader = findViewById(R.id.subheader)
hobby=findViewById(R.id.hobby)
btn1.setOnClickListener {
Toast.makeText(this#NewActivity, "Hurray", Toast.LENGTH_SHORT).show()
val intent1 = Intent(this#NewActivity, MainActivity::class.java)
startActivity(intent1)
}
if(intent != null) {
title1=intent.getStringExtra("Name")
}
title=title1
}
}
My title is not displaying in the app. I think there is a problem with the intent.
The problem is you are setting the title after starting the activity
startActivity(intent)
name="Diya K"
intent.putExtra("Name",name)
Solution is set the extras first before starting the activity. Just swap the lines like below:
name="Diya K"
intent.putExtra("Name",name)
startActivity(intent)
if(intent != null) {
title1=intent.getStringExtra("Name")
}
title=title1
header.setText(title)// you get value but not set
Related
I have a ListView with clickable ListItems that display a Friend objects name and birthday. The onClick is supposed to start a new Activity with an edit page where you can change the name and birthday of said friend. For this I start a new activity with an Intent containing a Serialized Friend object.
The Activity works perfectly as it should as long as I don't add the Friend object to the intent:
intent.putExtra("friend", friendList.get(position) as Serializable)
but as soon as I add this line of code before starting the activity the screen just turns black. The program doesn't crash and there are no error messages, just a black screen.
MainActivity
class MainActivity : Activity() {
private var friendList : ArrayList<Friend> = arrayListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initButtons()
}
private fun initButtons() {
val registerButton = findViewById<Button>(R.id.register_friend)
registerButton.setOnClickListener {
val name = findViewById<EditText>(R.id.name).text.toString()
val birthday = findViewById<EditText>(R.id.birthday).text.toString()
if(name != "" && birthday != "") {
friendList.add(Friend(name,birthday))
}
}
val showFriendsButton = findViewById<Button>(R.id.show_friends)
showFriendsButton.setOnClickListener {
val listView = findViewById<ListView>(R.id.friend_list_view)
val adapter = FriendListAdapter(this, friendList)
listView.adapter = adapter
listView.choiceMode = ListView.CHOICE_MODE_SINGLE
if(listView.getVisibility() == View.VISIBLE){
listView.setVisibility(View.INVISIBLE)
}else {
listView.setVisibility(View.VISIBLE)
}
listView.setOnItemClickListener { parent, view, position, id ->
val intent = Intent("android.intent.action.EDIT")
intent.putExtra("friend", friendList.get(position) as Serializable)
startActivity(intent)
}
}
}
}
EditActivity
class EditFriend : Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.edit_friend)
val friend: Friend = intent.getSerializableExtra("friend") as Friend
findViewById<EditText>(R.id.name).setText(friend.name)
findViewById<EditText>(R.id.birthday).setText(friend.birthday)
val saveButton = findViewById<Button>(R.id.save)
saveButton.setOnClickListener {
val name = findViewById<EditText>(R.id.name).text.toString()
val birthday = findViewById<EditText>(R.id.birthday).text.toString()
if(name != "" && birthday != "") {
friend.name = name
friend.birthday = birthday
finish()
}
}
}
}
Friend
data class Friend(var name : String?, var birthday : String?) : Serializable
When I click the register button, it does not save the data and gives the following error;
com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted
Please help us find the error!
Register code page;
class RegisterActivity : AppCompatActivity() {
private lateinit var binding : ActivityRegisterBinding
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRegisterBinding.inflate(layoutInflater)
setContentView(binding.root)
firebaseAuth = FirebaseAuth.getInstance()
binding.registerButton.setOnClickListener {
val email = binding.yourEmail.text.toString()
val pass = binding.editTextTextPassword2.text.toString()
val username = binding.editTextTextPersonName.text.toString()
if(email.isNotEmpty() && pass.isNotEmpty() && username.isNotEmpty()){
firebaseAuth.createUserWithEmailAndPassword(email , pass).addOnCompleteListener{
if (it.isSuccessful){
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}else{
Toast.makeText(this,it.exception.toString(), Toast.LENGTH_LONG).show()
}
}
}else{
Toast.makeText(this,"Empty Fields Are not Allowed !!", Toast.LENGTH_LONG).show()
}
}
val loginresetActivity = findViewById<TextView>(R.id.textView6)
textView6.setOnClickListener {
val Intent = Intent(this, LoginActivity::class.java)
startActivity(Intent)
}
}
}
I don't know if this will help but Make sure the string doesn't have white spaces im both sides (left and right)
val email = binding.yourEmail.text.toString().trim()
I am having a ton of trouble passing the product of two EditTexts to a TextView in another activity. Here is my code for MainActivity.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button1: Button = findViewById(R.id.button1)
val editText1: EditText = findViewById(R.id.editText1)
val editText2: EditText = findViewById(R.id.editText2)
val firstNumber = editText1.toString().toInt()
val secondNumber = editText2.toString().toInt()
val product = firstNumber * secondNumber
button1.setOnClickListener{
val intent = Intent(this, Activity2::class.java)
intent.putExtra("RESULT_PRODUCT", product)
startActivity(intent)
}
}
}
Here is my code for Activity2:
class Activity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_2)
val product = intent.getIntExtra("RESULT_SUM", 0)
textView1.text = product.toString()
}
}
I am relatively new to Kotlin and Android Studio but this has caused crashes left and right.
First of all, You have to calculate the product inside OnClickListener to get correct result.
button1.setOnClickListener{
val firstNumber = editText1.text.toString().trim()
val secondNumber = editText2.text.toString().trim()
if(!(firstNumber.isEmpty() or secondNumber.isEmpty())) {
val product = firstNumber.toInt() * secondNumber.toInt()
val intent = Intent(this, Activity2::class.java)
intent.putExtra("RESULT_PRODUCT", product)
startActivity(intent)
} else {
//Show messages
}
}
And then you have to use the exact key RESULT_PRODUCT that you use in your activity to pass data through intent
val product = intent.getIntExtra("RESULT_PRODUCT", 0)
You are passing "RESULT_PRODUCT" from MainActivity but getting "RESULT_SUM" in your Activity2. You should use intent.getIntExtra("RESULT_PRODUCT", 0) in you second activity.
I am trying to pass two strings from AddNote to MainActivity. But it keeps getting null.
Unable to start activity (MainActivity)
java.lang.IllegalStateException: callingIntent.getStringExtra("intentTitle") must not be null
class MainActivity : AppCompatActivity() {
private val notes = arrayListOf<Note>()
private val db by lazy {
Room.databaseBuilder(this
,NoteDatabase::class.java
,"NoteDatabase.db")
.allowMainThreadQueries()
.build() }
lateinit var adapter: adapter
lateinit var title: String
lateinit var content: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notes.addAll(db.dao().getNotes())
AddNote.setOnClickListener {
val i = Intent(this#MainActivity,AddNote::class.java)
startActivity(i)
}
// startActivity(Intent(this, AddNote::class.java))
val callingIntent = intent
title = callingIntent.getStringExtra("intentTitle")
content = callingIntent.getStringExtra("intentContent")
val note = Note(title,content)
val id = db.dao().insert(note)
note.id = id.toInt()
notes.add(note)
adapter = adapter(notes, db)
rootView.layoutManager = LinearLayoutManager(this)
rootView.adapter = adapter
}
override fun onResume() {
super.onResume()
notes.clear()
notes.addAll(db.dao().getNotes())
adapter.notifyDataSetChanged()
}
}
class AddNote : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.add_note)
var intentTitle = "Title"
var intentContent = "Content"
saveNote.setOnClickListener {
intentTitle = addTitle.text.toString()
intentContent = addContent.text.toString()
}
val i = Intent()
i.putExtra("title",intentTitle)
i.putExtra("content",intentContent)
startActivity(i)
}
}
You must start activity like this...
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", value)
startActivity(intent)
You must put the code that starts MainActivity inside saveNote.setOnClickListener:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.add_note)
var intentTitle = "Title"
var intentContent = "Content"
saveNote.setOnClickListener {
intentTitle = addTitle.text.toString()
intentContent = addContent.text.toString()
val i = Intent(this, MainActivity::class.java)
i.putExtra("title",intentTitle)
i.putExtra("content",intentContent)
startActivity(i)
}
}
The way your code worked was to start MainActivity as soon as AddNote activity was loaded, so I'm not sure what you are trying to do.
This is the main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var userInput:EditText = findViewById(R.id.usr_input) as EditText
var button:Button = findViewById(R.id.ent_btn) as Button
button.setOnClickListener {
var name = userInput.text
val intent = Intent(this#MainActivity, screenTwo::class.java)
intent.putExtra("name", name)
startActivity(intent)
}
}
}
This is second screen
class screenTwo : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_screen_two)
var userName:TextView = findViewById(R.id.user_name) as TextView
var editText:EditText = findViewById(R.id.usr_text) as EditText
var outText:TextView = findViewById(R.id.output) as TextView
var showButton:Button = findViewById(R.id.btn_show) as Button
var back:Button = findViewById(R.id.btn_back) as Button
var name = intent.getStringExtra("name")
userName.text = name
showButton.setOnClickListener {
var text:String = editText.text.toString()
outText.text = text
}
back.setOnClickListener {
var goback = Intent(this#screenTwo, MainActivity::class.java)
startActivity(goback)
}
}
}
When I click on the button(variable_name) in the main activity, the app closes. Is it the intent or something is wrong in the second screen???
intent is not defind in SecondActivity
In SecondActivity : get intent by getIntent()
class screenTwo : AppCompatActivity() {
var name = getIntent().getStringExtra("name")
Most common error in this configuration is that you forgot to declare your screenTwo in your AndroidManifest.xml, inside the "application" node (your MainActivity should already be defined there) :
<activity
android:name=".screenTwo"
android:theme="yourTheme"/>
If this is not it, an error log might help us help you.
Sorry , it was a minor error from my side..
I didn't convert the variable 'name' to 'String'
var name = userInput.text.toString() //correction
It's working now,
Thanks anyway...