Skip to main content

Time Picker Dialog Box in android

Android Time picker are used a lot in android apps.

Android Button Click And Open TatePickerDialog Box

In your build.gradle add latest appcompat library, at the time 24.2.1

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' 
    // where X.X.X version
}

  1. activity_main.xml

  • Set On TextView Click Listener 
  • <TextView  
    android:id="@+id/tvTime"
    android:layout_width="match_parent"
    android:layout_height="44dp"   android:layout_alignParentStart="true"   android:layout_alignParentLeft="true"   android:layout_alignParentTop="true"   android:layout_marginStart="0dp"   android:layout_marginLeft="0dp"   android:layout_marginTop="38dp"   android:text="DD-MM-YYYYY"  
    android:textSize="24sp" /> 
  • After Select the date and  set the textview
==>MainActivity.java

  • Find the id of TextView and and store TextView Object
  • TextView tvTime = findViewById(R.id.tvTime);

  1. tvTime.setOnClickListener(new View.OnClickListener() {
  2.     @Override
  3.     public void onClick(View view) {
  4.     // Get Current Time
  5.     final Calendar c = Calendar.getInstance();
  6.     mHour = c.get(Calendar.HOUR_OF_DAY);
  7.     mMinute = c.get(Calendar.MINUTE);
  8.     // Launch Time Picker Dialog
  9.     TimePickerDialog timePickerDialog = new TimePickerDialog(context,
  10.         new TimePickerDialog.OnTimeSetListener() {
  11.             @Override
  12.             public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
  13.                 tvTime.setText(hourOfDay + ":" + minute);
  14.             }
  15.         }, mHour, mMinute, false);
  16.     timePickerDialog.show();
  17.     }
  18. });

Comments

Popular posts from this blog

Error user email lenth not specified

Set up Database Migration Default String Length To set up the default string length for the database migration, go to   app/Providers/AppServiceProvider.php and update the code with the following:   use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; // public function boot() { Schema::defaultStringLength(191); }    

Laravel Artisan Command

1) Clear Cache facade value: php artisan cache:clear 2) Clear Route cache: php artisan route:cache 3) Clear View cache: php artisan view:clear 4) Clear Config cache: php artisan config:cache 5) Key Generate php artisan key:generate