TimePicker

TimePicker Example in Android

MainActivity CLASS

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

private TimePicker timePicker;
private Button button;

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

setClickListener();
}

private void setClickListener() {
timePicker = (TimePicker) findViewById(R.id.timePicker);
button = (Button) findViewById(R.id.bShowTime);
timePicker.setIs24HourView(true);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), timePicker.getCurrentHour() + ” : ” + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();
}
});
}

}

 

MainActivity XML

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<TimePicker
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/timePicker”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Show Time”
android:id=”@+id/bShowTime”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>

 

OutPut :

pic

 

Share

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.