Free Download Software and Android Tutorials

Tuesday, September 1, 2015

Android studio tutorial retrieve data clock with TimePicker

In the previous android tutorial I have discussed about the following DatePicker in a simple application. In this article we will discuss how to retrieve data via TimePicker hour’s later change. For more details, we will immediately practice just to create a new project from the android studio and the project named TimePicker then you select Blank Activity. TimePicker Project consists of two main files of the first file for later layout which we give the name activity_main.xml and files to run a program that we named MainActivity.java.

Source Code activity_main.xml

<?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">
    <Button
        android:id="@+id/btnGantiWaktu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Klik untuk ubah waktu"/>
    <TextView
        android:id="@+id/lblWaktu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Waktu sekarang: (jam:menit):"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <TextView
        android:id="@+id/tvWaktu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
</LinearLayout>

Source Code MainActivity.java

package com.dsoccerupdate.timepicker;

import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
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;
import android.os.Bundle;


public class MainActivity extends Activity {
    private TextView lblWaktu;
    private TimePicker timePicker1;
    private Button btnGantiWaktu;
    private int hour;
    private int minute;
    static final int TIME_DIALOG_ID = 999;

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

    //display current time
    public void setCurrentTimeOnView() {
        lblWaktu = (TextView) findViewById(R.id.tvWaktu);
        timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        //mengeset tanggal sekarang ke textview
        lblWaktu.setText(new StringBuilder()
                .append(pad(hour)).append(":").append(pad(minute)));
        timePicker1.setCurrentHour(hour);
        timePicker1.setCurrentMinute(minute);
    }

    public void tambahkanListenerkeButton() {
        btnGantiWaktu = (Button) findViewById(R.id.btnGantiWaktu);
        btnGantiWaktu.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this, timePickerListener,hour,minute,false);
        }
        return null;
    }
    private TimePickerDialog.OnTimeSetListener timePickerListener
            = new TimePickerDialog.OnTimeSetListener(){

        public void onTimeSet(TimePicker view, int selectedHour,
                              int selectedMinute) {
            hour = selectedHour;
            minute = selectedMinute;
            lblWaktu.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
            timePicker1.setCurrentHour(hour);
            timePicker1.setCurrentMinute(minute);
        }
    };
    private static String pad(int c){
        if(c>=10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
}

TimePicker Project Result

Android studio tutorial retrieve data clock with TimePicker

Android studio tutorial retrieve data clock with TimePicker

TimePicker Project Download

For those of you who want to download this TimePicker project may click on the Download link I provided below, hopefully this simple article can be enlightening for those of you who are learning android programming language.

DOWNLOAD


Android studio tutorial retrieve data clock with TimePicker Rating: 4.5 Diposkan Oleh: Unknown

1 comments:

  1. There is SHOCKING news in the sports betting world.

    It's been said that every bettor needs to see this,

    Watch this now or stop betting on sports...

    Sports Cash System - Sports Betting Robot

    ReplyDelete