C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android DatePicker ExampleAndroid 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 ExampleLet's see the simple example of datepicker widget in android. activity_main.xmlFile: activity_main.xml Activity classFile: MainActivity.java package example.TheDeveloperBlog.com.datepicker; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; public class MainActivity extends AppCompatActivity { DatePicker picker; Button displayDate; TextView textview1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1=(TextView)findViewById(R.id.textView1); picker=(DatePicker)findViewById(R.id.datePicker); displayDate=(Button)findViewById(R.id.button1); textview1.setText("Current Date: "+getCurrentDate()); displayDate.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { textview1.setText("Change Date: "+getCurrentDate()); } }); } public String getCurrentDate(){ StringBuilder builder=new StringBuilder();; builder.append((picker.getMonth() + 1)+"/");//month is 0 based builder.append(picker.getDayOfMonth()+"/"); builder.append(picker.getYear()); return builder.toString(); } } Output:
Next TopicAndroid Timepicker Example
|