Android Project: Date & Time Picker:
Android Project: Date & Time Picker
>>>Create a Java Class named
MainActivity.java
package com.example.time_date_picker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private
DatePicker dp;
private
TimePicker tp;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dp
= (DatePicker) findViewById(R.id.etdatePicker);
tp
= (TimePicker) findViewById(R.id.ettimePicker);
}
public
void checkDate(View v) {
int
day = dp.getDayOfMonth();
int
month = dp.getMonth();
int
year = dp.getYear();
Toast.makeText(
getApplicationContext(),
"Your
Date of Birth:(dd/mm/yyyy)" + day + "/" + (month +1)+
"/"
+
year, Toast.LENGTH_LONG).show();
}
public
void checkTime(View v) {
int
hour=tp.getCurrentHour();
int
minute=tp.getCurrentMinute();
boolean
is24hr=tp.is24HourView();
String
ampm = null;
if(is24hr)
{
}else{
if(hour>=12){
ampm="PM";
}else{
ampm="Am";
}
}
Toast.makeText(
getApplicationContext(),
"Your
Birth of Time is:" + hour + ":" +minute+ " "+ ampm,
Toast.LENGTH_LONG).show();
}
}
>>> Create an Xml file name Activity_main
<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=".MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/etdatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/scrollView1"
android:layout_marginRight="26dp"
android:layout_marginTop="29dp" />
<Button
android:id="@+id/etbtnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/datePicker1"
android:onClick="checkDate"
android:text="Check Date" />
<TimePicker
android:id="@+id/ettimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/datePicker1"
android:layout_marginTop="84dp" />
<Button
android:id="@+id/etbtnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker1"
android:layout_toLeftOf="@+id/scrollView1"
android:onClick="checkTime"
android:text="Check Time" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
>>>>> If you Face any Problem Comment
Here I Will Try to solution it, thanks.
No comments:
Post a Comment