Tuesday, October 11, 2016

android - Get TimePicker AM PM value

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#baac98"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select time."
        android:gravity="center"
        android:layout_gravity="center_horizontal|top"
        android:background="#61b16e"
        android:textSize="30sp"
        android:layout_margin="10dp"
        android:padding="15dp"
        android:textStyle="bold"
        />
    <TimePicker
        android:id="@+id/time_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/text_view"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        android:timePickerMode="spinner"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TimePicker mTimePicker;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTimePicker = (TimePicker) findViewById(R.id.time_picker);
        mTextView = (TextView) findViewById(R.id.text_view);

        // Set a time changed listener to time picker
        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            /*
                onTimeChanged

                    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.
            */
            @Override
            public void onTimeChanged(
                    TimePicker timePicker, // TimePicker view
                    int i, // hourOfDay
                    int i1 // Minute
            ) {
                String getAMPMValue = "AM";
                if(i>11){
                    getAMPMValue="PM";
                    if(i!=12){
                        i=i-12;
                    }
                }
                // Display the 12 hour format time in TextView
                mTextView.setText(""+i+":"+ i1+ " "+ getAMPMValue);
            }
        });
    }
}