activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/chooseTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Time"
android:textSize="20dp" />
<TextView
android:id="@+id/displayTimeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java:
import java.util.Calendar;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
Calendar c = Calendar.getInstance();
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button changeTimeBtn = (Button) findViewById(R.id.chooseTimeButton);
display = (TextView) findViewById(R.id.displayTimeText);
changeTimeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this, t, c
.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
true).show();
}
});
}
TimePickerDialog.OnTimeSetListener t = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
display.setText("Choosen time is :" + hourOfDay + ":" + minute);
}
};
}