Tuesday, March 16, 2021

Android DatePicker

 

Android DatePicker Example

android datepicker

Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.

The android.widget.DatePicker is the subclass of FrameLayout class.


Android DatePicker Example

Let's see the simple example of datepicker widget in android.



activity_main.xml

File: activity_main.xml



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.datepicker.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_above="@+id/button1"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentStart="true"  
  16.         android:layout_marginBottom="102dp"  
  17.         android:layout_marginLeft="30dp"  
  18.         android:layout_marginStart="30dp"  
  19.         android:text="" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/button1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignParentBottom="true"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:layout_marginBottom="20dp"  
  28.         android:text="Change Date" />  
  29.   
  30.     <DatePicker  
  31.         android:id="@+id/datePicker"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_above="@+id/textView1"  
  35.         android:layout_centerHorizontal="true"  
  36.         android:layout_marginBottom="36dp" />  
  37.   
  38. </RelativeLayout>  


Activity class

File: MainActivity.java

  1. package example.javatpoint.com.datepicker;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.DatePicker;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     DatePicker picker;  
  12.     Button displayDate;  
  13.     TextView textview1;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         textview1=(TextView)findViewById(R.id.textView1);  
  20.         picker=(DatePicker)findViewById(R.id.datePicker);  
  21.         displayDate=(Button)findViewById(R.id.button1);  
  22.   
  23.         textview1.setText("Current Date: "+getCurrentDate());  
  24.   
  25.         displayDate.setOnClickListener(new View.OnClickListener(){  
  26.             @Override  
  27.             public void onClick(View view) {  
  28.   
  29.                 textview1.setText("Change Date: "+getCurrentDate());  
  30.             }  
  31.   
  32.         });  
  33.   
  34.     }  
  35.     public String getCurrentDate(){  
  36.         StringBuilder builder=new StringBuilder();;  
  37.         builder.append((picker.getMonth() + 1)+"/");//month is 0 based  
  38.         builder.append(picker.getDayOfMonth()+"/");  
  39.         builder.append(picker.getYear());  
  40.         return builder.toString();  
  41.     }  
  42. }  

Output:

android datepicker example 1 android datepicker example 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile