onTimeChangedListener is a method that is called when time is edited or changed inside a TimePicker. TimePicker is an essential class provided by Android Developers. The callback interface is used to indicate the time has been adjusted. It can be helpful for providing instantaneous clock field responses.
Syntax For Java:
public abstract void onTimeChanged (TimePicker view, int hourOfDay, int minute)
Parameters:
view TimePicker: The view associated with this listener.
hourOfDay int: The current hour.
minute int: The current minute.
Syntax For Kotlin:
abstract fun onTimeChanged( view: TimePicker!, hourOfDay: Int, minute: Int ): Unit
Parameters:
view TimePicker!: The view associated with this listener.
hourOfDay Int: The current hour.
minute Int: The current minute.
To use this method, we will create an android application in which we will add a TimePicker and whenever the TimePicker is edited/changed, onTimeChanged method will be called then we toast a message to the user that onTimeChanged method is called and the time is edited. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<!-- xml code for above implementation --> <? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:background = "#00FF00" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > <!-- textView for show time --> < TextView android:id = "@+id/view" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center" android:layout_marginTop = "30dp" android:gravity = "center" android:background = "@color/white" android:textColorHighlight = "@color/white" android:text = "Time" android:textColor = "@color/black" android:textSize = "40dp" /> <!-- button for pick time --> < Button android:id = "@+id/button" android:layout_marginTop = "10dp" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "change time" android:layout_gravity = "center" android:textColor = "#00FF00" android:backgroundTint = "@color/white" /> </ LinearLayout > </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
package com.ujjwal.ontimechangedlistenergfg import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) simpleTimePicker.setIs24HourView( true ) // perform set on time changed listener event simpleTimePicker.setOnTimeChangedListener { view, hourOfDay, minute -> // make a toast that time is changed Toast.makeText( this , "Time changed" ,Toast.LENGTH_SHORT).show() // set the current time in text view time.text = "Time is :: $hourOfDay : $minute" } // for changing the background color of title bar val aBar = supportActionBar val cd = ColorDrawable(Color.parseColor( "#FF00FF00" )) aBar?.setBackgroundDrawable(cd) } } |
Java
package com.ujjwal.ontimechangedlistenergfg; import android.app.TimePickerDialog; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView time; TimePicker simpleTimePicker; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initiate the view's time = (TextView) findViewById(R.id.time); simpleTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker); // used to display AM/PM mode simpleTimePicker.setIs24HourView( false ); // perform set on time changed listener event simpleTimePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // display a toast with changed values of time picker Toast.makeText(getApplicationContext(), "time changed " , Toast.LENGTH_SHORT).show(); // set the current time in text view time.setText( "Time is :: " + hourOfDay + " : " + minute); } }); // for changing the background color of title bar ActionBar aBar = getSupportActionBar(); ColorDrawable cd = new ColorDrawable(Color.parseColor( "#FF00FF00" )); if (aBar != null ) { aBar.setBackgroundDrawable(cd); } } } |
Output: