I want to show date picker popup window. I have found some examples but I am not getting it properly. I have one edit text and I click on edit text the date picker dialog should pop up and after setting the date, the date should show in edit text in dd/mm/yyyy format in fragments. PLease provide me sample code or good links.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Solution:
Make your
EditText clickable and focusable false
like,Then in your Fragment Class, put below code,
private void getDate() {
Step 1. Disable your edit text
Step 2. Set onClickListener on your Edit Text
Step 3. onClick of your EditText, call the function to open the Date picker Dialogue.
and onDateSelected, the overridden function will return date, you can extract the date, month and year from there and set that text as string in your Edit text.
Let me know if you need help with the code.
I have develop an easy way out. It is one time investment work
Copy the following class into your project...
Then copy the below method into your fragment...
Then call the above method from your EditText listener (both
and
)
Below is an example...
Change
variable to your EditText variable name.
And you are done!
Understanding the code
The above class can be use multiple times in your project and you do not need to think about the inner work of the Helper class. The hard part has been done by the class and you only get a callback whenever a date is selected.
The
method need 3 params.
If you need to change the Date format, you may change the method
You may change 10-05-2018 to 10/05/2018 or anything else. Just read the body of the method and you will understand what to change.
Hope this will help you and many others.
EDIT
Add
android:focusable="false"
within the xml file of the EditText to allow for a single touch and then you can avoid usingsetOnFocusChangeListener
in your java code.Instead of using
Edittext
you can usecustom layout
withTextview
inside it. And upon clicking that layout youy can open datepicker dialog and after selecting date fromdatepicker
updateTextview
with that datexml for layout
Create a new file in
drawable
folder by namebackground_rectangle
and paste the following code inside itNow add the dependency in
app
levelgradle file
Now inside activity implement the
datapickerdialog.ondateselected
methodTry this in the XML file:
Now in Java File:
Now add the method in the above activity.
Add
android:focusable="false"
within the xml file of the EditText to allow for a single touch.