Set TimePicker to 24 Hour View in Android UI

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TimePicker android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

   <Button android:id="@+id/btnSet"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="I am all set!"
       android:onClick="onClick" />

</LinearLayout>


————————- Java Code —————————

 

package com.studentsempire.myapplication3.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
    TimePicker timePicker;

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

        timePicker = (TimePicker) findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);
    }

    public void onClick(View view) {
        Toast.makeText (getBaseContext(),
                "Time selected:" +
                        timePicker.getCurrentHour() +
                        ":" + timePicker.getCurrentMinute(),
                Toast.LENGTH_SHORT).show();
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *