Home > Android > Android Program – Time Picker Activity

Android Program – Time Picker Activity

ANDROID

Android Program – Time Picker Activity

    

Java Code(TimeAct)

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

public class TimeAct extends Activity {
static final int Dialog1 = 1;
int hour,min,second;

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

public void next(View v)
{
showDialog(Dialog1);
}
@Override
protected Dialog onCreateDialog(int id) {

{
return new TimePickerDialog(TimeAct.this,listener2,hour,min,true);

}

}
private TimePickerDialog.OnTimeSetListener listener2 = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
min = minute;
second=(hour/3600)+(min/60);
String time=hour+”:”+min;
Toast.makeText(TimeAct.this,time, Toast.LENGTH_SHORT).show();

}
};

}

Layout XML Code(activity_time)

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 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:orientation=”vertical”
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.andro.tech.androidproject.TimeAct”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”@dimen/textsize”
android:layout_gravity=”center”
android:textStyle=”bold”
android:text=”@string/timetext”
android:layout_marginBottom=”120dp” />

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”SET TIME”
android:onClick=”next”
android:layout_marginBottom=”150dp”/>

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/sourcecode”
android:background=”@color/bgcolor”
android:textStyle=”bold”
android:textColor=”@color/text”
android:onClick=”view1″/>

</LinearLayout>


Output

    

Download Source Code

Follow Us – Never Miss Update

Fb/PageTwitterGoogle+InstagramPinterest 


[metaslider id=3165]

Recent   

 

Is this Useful content For You?