0% found this document useful (0 votes)
11 views

Practical 16

The document contains code for an Android application that uses TimePicker and DatePicker widgets. It includes XML layout code defining three TimePicker views and code in the MainActivity class to display date and time pickers and update text views with the selected values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Practical 16

The document contains code for an Android application that uses TimePicker and DatePicker widgets. It includes XML layout code defining three TimePicker views and code in the MainActivity class to display date and time pickers and update text views with the selected values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1

activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8" ?> package com.example.practical16;
<LinearLayout import
xmlns:android="http://schemas.android.com/apk/res/ androidx.appcompat.app.AppCompatActiv
android" ity;
android:layout_width="match_parent" import android.os.Bundle;
android:layout_height="match_parent" import android.widget.TimePicker;
android:orientation="vertical"> public class MainActivity extends
<TimePicker AppCompatActivity {
android:id="@+id/tp" TimePicker tp,tp2,tp3;
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content"/> protected void onCreate(Bundle
<TimePicker savedInstanceState) {
android:id="@+id/tp2" super.onCreate(savedInstanceState);
android:layout_width="wrap_content"
android:layout_height="wrap_content" setContentView(R.layout.activity_main);
android:timePickerMode="spinner" tp=findViewById(R.id.tp);
android:layout_marginLeft="70dp" /> tp2=findViewById(R.id.tp2);
<TimePicker tp3=findViewById(R.id.tp3);
android:id="@+id/tp3"
android:layout_width="wrap_content" tp.setIs24HourView(true);
android:layout_height="wrap_content" tp3.setIs24HourView(true);
android:timePickerMode="spinner" }
android:layout_marginLeft="70dp" /> }
</LinearLayout>
Q2

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> android:layout_height="wrap_content"
<AbsoluteLayout android:layout_x="24dp"
xmlns:android="http://schemas.android.com/apk/res/android android:layout_y="187dp"
" />
android:layout_width="match_parent" <Button
android:layout_height="match_parent" android:id="@+id/button"
> android:layout_width="wrap_content"
<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_x="243dp"
android:textStyle="bold" android:layout_y="90dp"
android:text="DateTimePickerDialog" android:text="Select Date"
android:textSize="30dp" android:onClick="showdate"
android:textColor="@color/white" />
android:background="@color/black" <Button
/> android:id="@+id/button2"
<TextView android:layout_width="wrap_content"
android:id="@+id/tv1" android:layout_height="wrap_content"
android:layout_width="150dp" android:layout_x="239dp"
android:layout_height="wrap_content" android:layout_y="169dp"
android:layout_x="24dp" android:text="Select Time"
android:layout_y="107dp" /> android:onClick="showtime"
<TextView />
android:id="@+id/tv2" </AbsoluteLayout>
android:layout_width="150dp"
MainActivity.java public void showtime(View view) {
package com.example.practical16; // Get the current time
import androidx.appcompat.app.AppCompatActivity; final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
import android.app.DatePickerDialog; int minute = c.get(Calendar.MINUTE);
import android.app.TimePickerDialog;
import android.os.Bundle; // Create a new TimePickerDialog instance
import android.view.View; TimePickerDialog timePickerDialog = new TimePi
import android.widget.DatePicker; new TimePickerDialog.OnTimeSetListener() {
import android.widget.TextView; @Override
import android.widget.TimePicker; public void onTimeSet(TimePicker view, in

import java.util.Calendar; {
tv2=findViewById(R.id.tv2);
public class MainActivity2 extends AppCompatActivity { String time=Integer.toString(hourOfDay)+
TextView tv1,tv2; tv2.setText(time);
@Override }
protected void onCreate(Bundle savedInstanceState) { }, hour, minute, true);
super.onCreate(savedInstanceState); // Show the TimePickerDialog
setContentView(R.layout.activity_main2); timePickerDialog.show();
} } public void showdate(View view) {
// Get the current date
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new DatePickerDialog instance
DatePickerDialog datePickerDialog = new DatePick
new DatePickerDialog.OnDateSetListener()

@Override
public void onDateSet(DatePicker view, int
tv1=findViewById(R.id.tv1);
String date=Integer.toString(dayOfMonth
+"/"+Integer.toString(year);
tv1.setText(date);
}
}, year, month, day);

// Show the DatePickerDialog


datePickerDialog.show();
}
}

You might also like