-1

This is the class

data class Crime(
val id:UUID=UUID.randomUUID(),
var title:String="",
var date: Date= Date(),
var isSolved:Boolean=false)

I want to display current date as a text in button.(Below is the layout for button)

<Button
    android:id="@+id/crime_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="Wed Nov 14 11:56 EST 2018"/>

This is the .kt code to set the text in button

crime_date?.apply {
            text = crime.date.toString()
            isEnabled = false
        }
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
Pawan Acharya
  • 89
  • 1
  • 14

4 Answers4

0

Try this :

val sdf = SimpleDateFormat("dd/MM/yyyy hh:mm:ss")   
val currentDate = sdf.format(Date())
crime_date.text = currentDate
System.out.println("Current DATE is  "+currentDate)
  • tried your code but it didnt worked. Date() should have worked for this. I tried date() method in simple kotlin program it worked there but it is not showing here. – Pawan Acharya Sep 27 '19 at 04:48
0

Try this

crime_date?.apply {
        val crime = Crime()
        text = crime.date.toString()
        isEnabled = false
    }

In your Button remove

tools:text="Wed Nov 14 11:56 EST 2018"

with

android:text="Wed Nov 14 11:56 EST 2018"
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0

Tools is just for development, text is the property that you want to set, to see when the app is built.

<Button
    android:id="@+id/crime_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="Wed Nov 14 11:56 EST 2018"/>

should be

<Button
    android:id="@+id/crime_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Wed Nov 14 11:56 EST 2018"/>

Check if you have a property text declared your app, before setting in apply, like

var text = ""
crime_date?.apply {
            text = crime.date.toString()
            isEnabled = false
        }

try if

crime_date?.text = "data" 

is working, if it is working the problem is with the crime.date.toString() part, try to debug with breakpoints, to check the values.

Edit:

If the code is called from a fragment make sure that it is called in or after onViewCreated, like:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        crime_date?.apply {
            text = crime.date.toString()
            isEnabled = false
        }
    }
barotia
  • 428
  • 4
  • 12
  • crime_date.text = "Hello world" writing this code it throws error "crime_date must not be null" – Pawan Acharya Sep 27 '19 at 13:36
  • Then the issue is that the corresponding xml does not contains the button with id crime_date. It is in a fragment or an activity? If it is a fragment, then you should call the mentioned code after, or in onViewCreated() method – barotia Sep 27 '19 at 13:46
  • Where did you put this code? It should be in override fun onViewCreated(... – barotia Sep 27 '19 at 13:54
  • Yes it is in onCreateView() method ' override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { crime_date?.apply { text = crime.date.toString() isEnabled = false }' – Pawan Acharya Sep 27 '19 at 13:59
  • 1
    Not onCreateView, onViewCreated, see my edited answer – barotia Sep 27 '19 at 13:59
  • Thank you that worked. Can you explain the reason or the good source where i can learn about it. – Pawan Acharya Sep 27 '19 at 14:59
  • Read the question here https://stackoverflow.com/questions/34541650/nullpointerexception-when-trying-to-access-views-in-a-kotlin-fragment&ved=2ahUKEwjJ_c-oo_HkAhXHxaYKHSA2DoIQjjgwAHoECAYQAQ&usg=AOvVaw2aeuVrjMdluswIrOFVJpNo,. Also mark my answer as correct and maybe upvote if it helped. – barotia Sep 27 '19 at 15:06
  • Why i am not able to implement onClickListner in that method(onViewCreated)? crime_date.setOnClickListener { Toast.makeText(this,"Clicked",Toast.LENGTH_LONG).show() } – Pawan Acharya Sep 27 '19 at 15:43
  • That's a separate question, open a new one I you do not find the answer – barotia Sep 27 '19 at 16:04
0

there is mistake in your xml correct this

 <Button
        android:id="@+id/crime_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wed Nov 14 11:56 EST 2018"/>
YuvrajsinhJadeja
  • 1,383
  • 7
  • 23