Skip to main content

Android Datepicker Example

Android Datepicker Example

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="50dp"  
  14.         android:layout_marginTop="36dp"  
  15.         android:text="Current Date:" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_centerHorizontal="true"  
  23.         android:layout_marginBottom="140dp"  
  24.         android:text="Change Date" />  
  25.   
  26.     <DatePicker  
  27.         android:id="@+id/datePicker1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_above="@+id/button1"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginBottom="30dp" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.datepicker2;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.DatePicker;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     DatePicker picker;  
  15.     Button displayDate;  
  16.     TextView textview1;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.         textview1=(TextView)findViewById(R.id.textView1);  
  23.         picker=(DatePicker)findViewById(R.id.datePicker1);  
  24.         displayDate=(Button)findViewById(R.id.button1);  
  25.           
  26.         textview1.setText(getCurrentDate());  
  27.           
  28.         displayDate.setOnClickListener(new OnClickListener(){  
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 textview1.setText(getCurrentDate());  
  32.             }  
  33.               
  34.         });  
  35.     }  
  36.     public String getCurrentDate(){  
  37.         StringBuilder builder=new StringBuilder();  
  38.         builder.append("Current Date: ");  
  39.         builder.append((picker.getMonth() + 1)+"/");//month is 0 based  
  40.         builder.append(picker.getDayOfMonth()+"/");  
  41.         builder.append(picker.getYear());  
  42.         return builder.toString();  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  48.         return true;  
  49.     }  
  50.   
  51. }  

Comments

Popular posts from this blog

Jio

Reliance Jio planning its own  cryptocurrency called JioCoin  elder son Akash Ambani leading the JioCoin project, Reliance Jio plans to build a 50-member team of young professionals to work on blockchain technology, which can also be used to develop applications such as smart contracts and supply chain management logistics

garbage monitoring using arduino code with gsm

#include <SoftwareSerial.h> #include <LiquidCrystal.h> //LiquidCrystal lcd(7, 6, 5, 4, 3, 2); LiquidCrystal lcd(13, 12, 11, 10, 9, 8); SoftwareSerial mySerial(0,1); #define trigPin 2 #define echoPin 3 #define PIR_sensor 4 #define m11 5 #define m12 6 void setup() {    lcd.begin(16, 2);    lcd.print("    Garbage    ");    Serial.println("garbage ");   lcd.setCursor(0,1);   lcd.print("   Open Close    ");   Serial.println(" open close");   delay(3000);   lcd.clear();   //lcd.print(" ");   delay(2000); mySerial.begin(9600); // Setting the baud rate of GSM Module Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);  pinMode(m11, OUTPUT);   pinMode(m12, OUTPUT);   } void loop() {  readPIR();  pingDist();  SendMessage(); } void pingDist ()   {     long duration, distance;...