TimePicker.OnTimeChangedListener() in Android
Last Updated :
01 Feb, 2023
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 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
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
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 )
simpleTimePicker.setOnTimeChangedListener { view, hourOfDay, minute ->
Toast.makeText( this , "Time changed" ,Toast.LENGTH_SHORT).show()
time.text = "Time is :: $hourOfDay : $minute"
}
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);
time = (TextView) findViewById(R.id.time);
simpleTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);
simpleTimePicker.setIs24HourView( false );
simpleTimePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(getApplicationContext(),
"time changed " ,
Toast.LENGTH_SHORT).show();
time.setText( "Time is :: " + hourOfDay + " : " + minute);
}
});
ActionBar aBar = getSupportActionBar();
ColorDrawable cd = new ColorDrawable(Color.parseColor( "#FF00FF00" ));
if (aBar != null ) {
aBar.setBackgroundDrawable(cd);
}
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...