How to get AM PM value from TimePickerDialog in Android

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#f0ce2b"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="25dp"         android:textColor="#fa2129"         android:fontFamily="sans-serif-condensed"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Set Time"         android:layout_below="@id/tv"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.app.DialogFragment;   public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         tamat RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         Button btn = (Button) findViewById(R.id.btn);          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // Initialize a new time picker dialog fragment                 DialogFragment dFragment = new TimePickerFragment();                  // Show the time picker dialog fragment                 dFragment.show(getFragmentManager(),"Time Picker");             }         });    } } 
TimePickerFragment.java
 package com.cfsuman.me.androidcodesnippets;   import android.app.AlertDialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.widget.TextView; import android.app.DialogFragment; import android.app.Dialog; import java.util.Calendar; import android.widget.TimePicker;  public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{      @Override     public Dialog onCreateDialog(Bundle savedInstanceState){         // Get a Calendar instance         tamat Calendar calendar = Calendar.getInstance();         // Get the current hour and minute         int hour = calendar.get(Calendar.HOUR_OF_DAY);         int minute = calendar.get(Calendar.MINUTE);          /*             Creates a new time picker dialog with the specified theme.                  TimePickerDialog(Context context, int themeResId,                     TimePickerDialog.OnTimeSetListener listener,                     int hourOfDay, int minute, boolean is24HourView)          */          // TimePickerDialog Theme : THEME_HOLO_LIGHT         TimePickerDialog tpd = new TimePickerDialog(getActivity(),                 AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);          // Return the TimePickerDialog         return tpd;     }      public void onTimeSet(TimePicker view, int hourOfDay, int minute){         // Set a variable to hold the current time AM PM Status         // Initially we set the variable value to AM         String status = "AM";          if(hourOfDay > 11)         {             // If the hour is greater than or equal to 12             // Then the current AM PM status is PM             status = "PM";         }          // Initialize a new variable to hold 12 hour format hour value         int hour_of_12_hour_format;          if(hourOfDay > 11){              // If the hour is greater than or equal to 12             // Then we subtract 12 from the hour to make it 12 hour format time             hour_of_12_hour_format = hourOfDay - 12;         }         else {             hour_of_12_hour_format = hourOfDay;         }          // Get the calling activity TextView reference         TextView tv = (TextView) getActivity().findViewById(R.id.tv);         // Display the 12 hour format time in app interface         tv.setText(hour_of_12_hour_format + " : " + minute + " : " + status);     } } 




More android examples

Komentar