I'm using kotlin in android studio and when I run my code, only 2 out of 3 buttons work. I'm still confused why it does happen, because I just copy-pasted the same code for all the three buttons.
The 'TODAY' Button below doesn't work. It's id is btn_today
Other App details: The app should ask the name on the first install. The 3 buttons here open different activities.
MainActivity.kt:
package com.example.mytimetable
import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
val firstStart = prefs.getBoolean("firstStart", true)
if (firstStart) {
showStartDialog()
setContentView(R.layout.activity_login)
login() // asks name on first launch
val buttonSubmit: Button = findViewById(R.id.btn_submit)
buttonSubmit.setOnClickListener {
// TODO(reason = store name on first launch)
string_name = editText_login.text.toString()
// name_text.text = string_name //uncommentting this line gives an error!
setContentView(R.layout.activity_main)
Toast.makeText(this, "Please exit and open the app again",
Toast.LENGTH_LONG).show() // I had to ask the user to close and open the app again
// because
// the app doesn't respond to any of the buttons on the first start.
}
}
if (!firstStart) {
val button_today: Button = findViewById(R.id.btn_today)
button_today.setOnClickListener {
val intent = Intent(this, today_activity::class.java)
startActivity(intent)
}
val button_tomorrow: Button = findViewById(R.id.btn_tomorrow)
button_tomorrow.setOnClickListener {
val intent = Intent(this, tomorrow_activity::class.java)
startActivity(intent)
}
val button_all_days: Button = findViewById(R.id.weekly)
button_all_days.setOnClickListener {
val intent = Intent(this, all_days::class.java)
startActivity(intent)
}
val button_settings: ImageButton = findViewById(R.id.setting_btn)
// TODO(reason = "Finish the Settings part")
button_settings.setOnClickListener {
Toast.makeText(this, "Settings still in development", Toast.LENGTH_SHORT).show()
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
}
}
private fun showStartDialog() {
Toast.makeText(this, "Welcome to 9A timetable app", Toast.LENGTH_SHORT).show()
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
val editor = prefs.edit()
editor.putBoolean("firstStart", false)
editor.apply()
}
fun login() {
val intent2 = Intent(this, login::class.java)
}
}
Also, if it does matter, my main xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/school_bg"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/setting_btn"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_baseline_settings_applications_24"/>
<TextView
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/card"
android:layout_marginTop="1dp"
android:text="9A Timetable"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"/>
<androidx.cardview.widget.CardView
android:id="@+id/card"
android:layout_marginBottom="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_centerInParent="true"
android:background="@android:color/white"
app:cardCornerRadius="12dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:layout_gravity="center">
<TextView
android:id="@+id/dtime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Good Morning,"
android:textSize="30sp"
android:textColor="#363A43"
android:textStyle="bold"
android:gravity="center"></TextView>
<TextView
android:id="@+id/name"
android:layout_below="@id/dtime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Abhinav"
android:textSize="25sp"
android:textColor="#4f4d46"
android:textStyle="bold"
android:gravity="center"></TextView>
<Button
android:layout_below="@id/name"
android:id="@+id/btn_today"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="today"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
style="@style/Widget.AppCompat.Button.Colored"
android:gravity="center"></Button>
<Button
android:layout_below="@id/btn_tomorrow"
android:id="@+id/weekly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All Days"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
style="@style/Widget.AppCompat.Button.Colored"></Button>
<Button
android:layout_below="@id/btn_today"
android:id="@+id/btn_tomorrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tomorrow"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
style="@style/Widget.AppCompat.Button.Colored"></Button>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>