activity_main.xml;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_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=".MainActivity" > <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ayarla" android:id="@+id/btnTime" android:paddingLeft="15dp" android:paddingRight="15dp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtTime" android:layout_marginLeft="20dp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtTime2" android:layout_marginLeft="20dp" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:timePickerMode="spinner" android:visibility="gone" /> </LinearLayout> </RelativeLayout>
MainActivity.java;
package com.example.okul.saatayar; import android.app.Dialog; import android.app.TimePickerDialog; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; public class MainActivity extends ActionBarActivity { private Button saatsec2txt = null; private TimePicker timePicker; private int hour; private int minute; static final int TIME_DIALOG_ID = 999; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); saatsec2txt = (Button) findViewById(R.id.txtTime); timePicker = (TimePicker) findViewById(R.id.timePicker); final Calendar c = Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE); saatsec2txt.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute))); timePicker.setCurrentHour(hour); timePicker.setCurrentMinute(minute); saatsec2txt.setOnClickListener(clickListener); } private View.OnClickListener clickListener = new View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.txtTime: showDialog(TIME_DIALOG_ID); break; } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, timePickerListener, hour, minute,true); } return null; } private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { hour = selectedHour; minute = selectedMinute; saatsec2txt.setText(new StringBuilder().append(padding_str(hour)).append(":").append(padding_str(minute))); timePicker.setCurrentHour(hour); timePicker.setCurrentMinute(minute); } }; private static String padding_str(int c) { if (c >= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } }
AndroidManifest.xml;
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.okul.saatayar" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>