0

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>
ashu
  • 1,756
  • 4
  • 22
  • 41
Abhinav
  • 97
  • 2
  • 10
  • 1
    Wow, you gotta remove all of this "`first start`" code, it certainly should not be there and looks soooo ugly. Then we can tackle the issue of why the buttons don't respond to clicks the first time. Also, you shouldn't be calling `setContentView` so many times. Once is enough. Sort that out, Restart and invalidate Android studio, edit the question and then come back and reply to my comment and I'll check out the code. – Vucko Dec 07 '20 at 13:17
  • How did you find out it doesn't work? Did you place a breakpoint inside listener and it wasn't reached? – Ircover Dec 07 '20 at 14:19
  • What is the problem? – cutiko Dec 07 '20 at 16:28
  • Hi Vucko! I just copied the first start code from a tutorial (though I don't understand it). When I use an intent and start another activity, I don't know why but the app crashes on launch. So I thought to replace it with `set content View`. Do you think that is the thing that is causing the problem? Also if you can please tell me a way that I could start activity inside of buttonSubmit.OnclickListner() – Abhinav Dec 08 '20 at 00:49

1 Answers1

1

Good morning! Have you tried to invert the order to diagnostic it? Try to do it if you didn't, and check if the problem will be this order. Sounds silly, but it will help us to eliminate the possibilities. If the order influences the button behavior, then the problem might be the condition inside the "if clause"

if (!firstStart) { ... }

Probably you're calling this "firstStart" more than necessary and it is confusing the program, as you already did it above. In this code, this second if could be completely removed.

Another alternative, you can declare a global value to intent, something like:

var dashboard = Intent();

and you transform this if into a "when" statement, sort of like:

when(btnActions) { 
   R.id.btn_today -> dashboard = new Intent(applicationContext, SomeActivity::class.java)
   R.id.btn_weekly -> dashboard = new Intent(applicationContext, AnotherActivity::class.java)
   
   startActivity(dashboard)

}

  • Hmm. Thank Victor, I never thought of using a when statement. Also, yes I too think that the problem is inside of the if clause that I copied from a tutorial. But if I remove that, then how can I ask the app to start the login activity on the first launch? – Abhinav Dec 08 '20 at 00:53
  • Hello Adbhinav. In this case, it is not interesting if you control it by checking if is a first start. It's better to use a variable, and an" if statement" to check: "var currentUser = editLogin.text.toString()" and then "if (currentUser.isEmptyOrNull()) { ... }. Be aware that this will only work if ou are referring to a database, like Firebase or SQLite for example. Although, if you want to learn more about "first launch specification", this link this problem was solved: https://stackoverflow.com/questions/4636141/determine-if-android-app-is-being-used-for-the-first-time. Hope it helps you! – Victor Magosso Dec 08 '20 at 14:47
  • Yeah it did help, Thank you. I deleted the project and started all over again. The thing is, this is my first app and this is the 20th time I deleted it. I have to always delete it due to errors. Is kotlin all about correcting errors? Do you think since I anyway deleted the project, creating a new project with java would help? Or should I just continue just like what you told in the above? – Abhinav Dec 09 '20 at 01:52
  • You're welcome! I think you don't need to quit learning Kotlin, because it helps on the development due to the short and intuitive syntax, at least for me. I started with Java programming in Android, and I can tell you that I was just like you: deleting and creating the same project a thousand of times, but when I migrate to Kotlin, things got clearer and I don't break my codes as I used to do in the beggining. The point is: selecting Java or Kotlin, you might have the same problems hehe. Another thing, if you prefer, you can use Java and paste the code in Android Studio to translate for you! – Victor Magosso Dec 09 '20 at 15:42