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

Practical no 16

The document describes two practical exercises involving a DatePicker and a TimePicker in Android applications. Each exercise includes the XML layout and corresponding Java code for handling user input and displaying selected date or time using Toast messages. The DatePicker allows users to select a date, while the TimePicker allows users to select a time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical no 16

The document describes two practical exercises involving a DatePicker and a TimePicker in Android applications. Each exercise includes the XML layout and corresponding Java code for handling user input and displaying selected date or time using Toast messages. The DatePicker allows users to select a date, while the TimePicker allows users to select a time.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical no 16(Datepicker)

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<DatePicker

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:datePickerMode="spinner"

android:calendarViewShown="false"

android:id="@+id/da"/>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/button"

android:text="selected"/>

</LinearLayout>

MainActivity.java

package com.example.ex161;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button b1=(Button) findViewById(R.id.button);

DatePicker d1=(DatePicker) findViewById(R.id.da);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int y=d1.getYear();

int m=d1.getMonth()+1;

int d=d1.getDayOfMonth();

String s="day-"+d+"\n"+"month"+m+"\n"+"year"+y;

Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();

});

Output:
Practical no. 16(Timepicker)

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TimePicker

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/time"/>

<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/bu"

android:text="selected"/>

</LinearLayout>

MainActivity.java

package com.example.ex162;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TimePicker;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TimePicker t1=(TimePicker)findViewById(R.id.time);

Button button =(Button) findViewById(R.id.bu);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int t=t1.getHour();

int m=t1.getMinute();

String s="hour-"+t+"\n"+"Minute"+m;

Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();

});

Output:

You might also like