Android doesn't detect click on EditText - android

I am working on an Android tablet application with a little bit complex forms (layout has a ListView and the layout with the form included so I have to inflate EditText) . My idea is that if I click on a TextView where the date is shown the DataPicker would be shown in dialog.
The problem is that click is not detected. There is no Logs.
Java:
public static EditText datep;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admissions_all);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
View inf = LayoutInflater.from(getApplicationContext()).inflate(R.layout.admissions, null);
datep = (EditText) inf.findViewById(R.id.date_value);
datep.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Date", "You clicked me mate!");
showDatePickerDialog(v);
}
});
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
Log.i("Date", "I want to show datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day= c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
datep.setText(day + "." + month+ "." + year);
}
}
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/general"
android:layout_alignRight="#id/general"
android:layout_below="#+id/general"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Data"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:id="#+id/line"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:background="#333333" />
<EditText
android:id="#+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/line"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/date_desc"
android:ems="9"
android:inputType="date" >
</EditText>
<TextView
android:id="#+id/date_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/date_value"
android:layout_alignBottom="#+id/date_value"
android:layout_alignParentLeft="true"
android:text="Date:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I followed this: http://developer.android.com/guide/topics/ui/controls/pickers.html

You need to set date_value's focusable in touch mode to false. You can do so either by adding android:focusableInTouchMode="false" to your xml or in your code with datep.setFocusableInTouchMode(false);. This should then have your on click event fire. (tested on emulator)
See also: How to do something after user clicks on my EditText
Reference: http://android-developers.blogspot.com/2008/12/touch-mode.html

I didn't see any dateup.setclickable(true);, try to add this. Maybe that's why it's not work , You can also debug it and see , if it call the onlciklistner or not
I saw that you wrote something like this.
datep = (EditText) inf.findViewById(R.id.date_value);
when you have this
setContentView(R.layout.admissions_all);
It means that you editText is in another Activity , but you are setting his onclickListner in another one , so it will not work. Initialize his onlcikListner in the activity, that content view is inf.xml. If all these will not help please add more part of your Code
Regards Hayk Nahapetyan

Could you change the DatePickerFragment to DatePickerDialog. Then do a DatePickerDialog.show();
Something like :
DatePickerDialog datePickerDialog = null;
datep.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Date", "You clicked me mate!");
showDatePickerDialog();
}
});
private void showDatePickerDialog() {
datePickerDialog = new DatePickerDialog(context, new DateSetListener(), year, month, day);
datePickerDialog.show();
}

Related

DatePicker dialog is not showing on single tap

Expectation:
When user will tap on MaterialTextView component, DatePickerDialog should open.
Whats happening:
User needs to tap twice on MaterialTextView component for opening DatePickerDialog.
MainActivity
protected void onCreate(Bundle SavedInstanceState){
MaterialTextView issueDate = findViewById(R.id.edIssueDate);
issueDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
hideKeyboard(AddNewCoupon.this);
DatePickerDialog datePickerDialog = new DatePickerDialog(AddNewCoupon.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
issueDate.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(month)+"/"+String.valueOf(year));
//newCustomer.hideKeyboard(AddNewCoupon.this);
}
}, year, month, day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
}
hideKeyboard() method:
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
if (imm != null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Layout:
<com.google.android.material.textview.MaterialTextView
android:id="#+id/edIssueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ems="10"
android:focusable="auto"
android:focusableInTouchMode="true"
android:hint="#string/txtVIssueingDtHint"
android:nextFocusDown="#id/edExpDate"
android:onClick="showDatePickerDialogForIssue"
android:paddingLeft="4dp"
android:textColor="#color/design_default_color_on_secondary"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
Dependencies:
dependencies {
...
implementation 'com.google.android.material:material:1.2.0-alpha04'
...
}
Code runs perfectly as DatePickerDialog opens but the problem is user needs to tap twice for opeining it. Where I've made mistake? How can I fix the code?
Change focusable and focusableInTouchMode value to false, It will do the trick
android:focusable="false"
android:focusableInTouchMode="false"
Hope this will help!!
change your textview to
<com.google.android.material.textview.MaterialTextView
android:id="#+id/edIssueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ems="10"
android:hint="#string/txtVIssueingDtHint"
android:onClick="showDatePickerDialogForIssue"
android:paddingLeft="4dp"
android:textColor="#color/design_default_color_on_secondary"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
Remove this option unless it is a required option.
android:focusable="auto"
android:focusableInTouchMode="true"

onclick Edittext, popup a date input dialog fragment

I have created an edit text to capture date. On setOnClickListener of edittext I am displaying the date doalog box. I select the date on this dialog box and the selected date gets set on the edittext.
Everything is working perfectly fine just that the setOnClickListener event is not getting fired when I click on the edittext the first time. I need to click again on the edittext to show the dialog box.
May be there is some property that I need to set for the edittext.
Here is the edittext
<EditText
android:id="#+id/edtDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/dateFormat"
android:inputType="date"
android:longClickable="false"
android:maxLength="10"
android:textAlignment="textStart"
android:textColor="#color/black"
android:textColorHint="#color/colorAccent"
android:textSize="#dimen/font_size" />
Here is the onclicklistener event
dob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
java.util.Calendar today = java.util.Calendar.getInstance();
YY = today.get(java.util.Calendar.YEAR);
MM = today.get(java.util.Calendar.MONTH);
DD = today.get(java.util.Calendar.DAY_OF_MONTH);
Log.e("current DD/MM/YY", DD+"/"+MM+"/"+YY);
DatePickerDialog dialog = new DatePickerDialog(
PremiumCalculation.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
YY, MM, DD);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
Can anyone guide me. Thanks in advance.
Add this two additional attributes to your edittext xml and implement onClick event for it
android:focusableInTouchMode="false"
android:focusable="false"
First You have to create a class named DatePicked or whatever you want and write a code for DataPikcerDaialog popup.
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener{
ImageButton btnDatePicker;
EditText txtDOB;
public DatePicker(View view, EditText txtDOB){
btnDatePicker = (ImageButton) view;
this.txtDOB = txtDOB;
}
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),this, year, month, day);
}
#Override
public void onDateSet(android.widget.DatePicker datePicker, int year, int month, int day) {
String date = day + "/" + (month+1) + "/" + year;
txtDOB.setText(date);
}
}
You can use either EditText or TextView. And if you are using EditText then make sure you disable your EditText.
And in which activity you want to popup DatePickerDialog, you have to call this class like this-
final EditText txtDOB = (EditText) findViewById(R.id.txtDOB);
txtDOB.setFocusable(false); //Disable Your EditText
txtDOB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DatePicker datePicker = new DatePicker(view, txtDOB);
FragmentTransaction ft = getFragmentManager().beginTransaction();
datePicker.show(ft, "Date Picker");
}
});
This will open DatePickerDialog and display selected Date in your EditText.
Try this ..
add this two things ..
android:clickable="true"
android:editable="false"
<EditText
android:id="#+id/Birthday"
custom:font="#string/font_avenir_book"
android:clickable="true"
android:editable="false"
`enter code here` android:hint="#string/birthday"/>
There is a reason, it's happening because when we click on editText for the first time the cursor shift on that and editText gets focusable. try it
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
as
<EditText
android:id="#+id/et_doc_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Document Date*"
android:inputType="text"
android:maxLength="40"
android:digits="1234567890-/"
android:paddingLeft="10dp"
android:singleLine="true"
android:textCursorDrawable="#null"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black_color" />

How to get TimePickerDialog on edittext appears at first click

How to get TimePickerDialog on edittext appears at first click? It takes twice click to get the dialog appears (the first click shows keyboard).
Gradle : minSdkVersion 9 and targetSdkVersion 23
Layout :
<EditText
android:id="#+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
Component :
txtTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(Pulse7DatePickerDialogActivity.this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
txtTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
}
});
add android:focusableInTouchMode="false" in layout declaration
<EditText
android:id="#+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:layout_weight="1">
</EditText>

TimePicker not updating from user input

I've an app that uses a custom dateTime Picker. when the user selects a time and clicks the SET button, it gets the time set on the spinner. My app checks the time and if it's not suitable based on some criteria i show the spinner again.
The problem is when the user changes the time on the spinner a second or subsequent time, the initial time is still set. How can i get the time from the spinner reflected in the dateTime object?
I've tried calling TPic.getCurrentHour etc and also refreshDrawableState on the TPic object but it still doesn't work. Any ideas?
Button ShowDTPicker;
Button ShowDatePicker;
Button ShowTimePicker;
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TimePicker TPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;
public void showDateTimeDialog(){
//final Calendar c = Calendar.getInstance();
final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
dialog = new Dialog(NfcscannerActivity.this);
dialog.setContentView(R.layout.datetimepicker);
switcher = (ViewSwitcher) dialog.findViewById(R.id.DateTimePickerVS);
TPic = (TimePicker) dialog.findViewById(R.id.TimePicker);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
TPic.setIs24HourView(true);
ShowDatePicker = ((Button) dialog.findViewById(R.id.SwitchToDate));
ShowDatePicker.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switcher.showPrevious();
ShowDatePicker.setEnabled(false);
ShowTimePicker.setEnabled(true);
}
});
ShowTimePicker = ((Button) dialog.findViewById(R.id.SwitchToTime));
ShowTimePicker.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switcher.showNext();
ShowDatePicker.setEnabled(true);
ShowTimePicker.setEnabled(false);
}
});
Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(), TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour() + " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);
................
................
...............
//check if time is suitable, if not call showDateTimeDialog() again
}
});
ReSet = ((Button) dialog.findViewById(R.id.ResetDateTime));
ReSet.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DPic.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
TPic.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
TPic.setCurrentMinute(c.get(Calendar.MINUTE));
}
});
Cancel = ((Button) dialog.findViewById(R.id.CancelDialog));
Cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.setTitle("Please enter time of last logout");
try{
ReSet.performClick();
dialog.show();
}catch(Exception e){
//ignore
}
Log.e(TAG,"Just executed dialog.show() and at the end of showDateTimeDialog method");
}//showDateTimeDialog()
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_TIME_DIALOG_ID:
return dialog;
}
return null;
}
.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
android:padding="5dip"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DateTimePicker"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/ViewSwitchButtons"
android:layout_marginBottom="5dip">
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SwitchToDate"
android:text="Set date"
android:enabled="false"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SwitchToTime"
android:text="Set time"
android:layout_weight="1"/>
</LinearLayout>
<ViewSwitcher
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DateTimePickerVS"
android:layout_below="#+id/ViewSwitchButtons"
android:outAnimation="#android:anim/fade_out"
android:inAnimation="#android:anim/fade_in">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/TimeLayout"
android:fillViewport="true">
<TimePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/TimePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/DateLayout"
android:fillViewport="true">
<DatePicker
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/DatePicker"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"/>
</LinearLayout>
</ViewSwitcher>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/ControlButtons"
android:layout_below="#+id/DateTimePicker"
android:paddingTop="185dip">
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/SetDateTime"
android:text="#android:string/ok"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/ResetDateTime"
android:text="Reset"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/CancelDialog"
android:text="#android:string/cancel"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
Android: How to get the time from a TimePicker when it is typed in
It seems like that DatePicker or TimePicker updates own values when its focus is gone. Use clearFocus() method in OnClickListener of set button.
I think the code would be like this:
Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DPic.clearFocus();
TPic.clearFocus();
Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth(),
TPic.getCurrentHour(), TPic.getCurrentMinute());
Log.e(TAG, "TPic hour and minute = " + TPic.getCurrentHour()
+ " " + TPic.getCurrentMinute());
timeSetOnSpinner = new DateTime(c);
}
});
Please note that ShowDTPicker, ShowDatePicker, ShowTimePicker, Set, ReSet, Cancel, DPic, TPic, Date are not following Java naming conventions. Names with a capitalized first letter are for classes or interfaces (and Date class is also exists!). If they are public class variables, each of their first letter should be lowercase. Also use mSwitcher instead of switcher since it is private class variable.
for kotlin you can test this code.
var timePicker : TimePicker = findViewById(R.id.timePicker)
timePicker.setOnTimeChangedListener(object:TimePicker.OnTimeChangedListener {
override fun onTimeChanged(p0: TimePicker?, hour: Int,minute: Int) {
// get hour and minute
hour_set =hour
minute_set =minute
} })
I'm having the same issue (with asp.net though)... i know that it's due to the fact that the new selected time is not been considered during the postback so the updates doesn't even occure ... i also know that i had the same issue with the date picker and solve it saving the values in hiddenfields taht i'm retrieving in the code behind .
so, basically it's the same here, we need javascript function to store th values in hiddenfield to be retrieved later
this function could be triggered with OnClientClick action on your submit button.
But i'm having issues finding how to get the values form the timePicker in javascript i did not found no accurate documentation.
i hope this helped,keep us tuned please if you find any solution.

Android - Hide date field in datepickerdialog

How to hide/remove date field in datepickerdialog.
Actually my requirement is to use the native date picker dialog but it shows up with day, month, and year. But as per my requirement i need only month and year. So i want to hide or remove the day field from the view.
Any help will be much usefull.
I don't recommend using Reflection to do this kind of thing.
There is a simpler and more pretty way to do so:
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
Be aware that .getDatePicker() method from DatePickerDialog works on API LEVEL >= 11.
Private DatePickerDialog createDialogWithoutDateField(){
DatePickerDialog dpd = new DatePickerDialog(_activity, ExpiryDateSetListener,cyear,cmonth, cday);
try{
Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
return dpd;
}
Use the above code for hiding the day field in DatePickerDialog.
Refer this LINK
The source code to DatePicker and DatePickerDialog are available for you to clone, refactor into your own package, and modify to suit.
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
This works properly
private Calendar mCalendar;
mCalendar = Calendar.getInstance();// Initialize Calendar
dialog_date_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
android:id="#+id/date_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<Button
android:id="#+id/date_time_set"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:text="Set"
android:textColor="#color/colorWhite" /></LinearLayout>
DialogMethod called on Click:
private void ExpiryDialog() {
System.out.println("Inside Dialog Box");
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_date_picker);
dialog.show();
final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
date_time_set = (Button) dialog.findViewById(R.id.date_time_set);
datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), null);
LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
ll2.getChildAt(1).setVisibility(View.INVISIBLE);
date_time_set.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view){
dialog.dismiss();
Integer month = datePicker.getMonth()+1;
Integer year = datePicker.getYear();
expMonth = (month.toString().length() == 1 ? "0"+month.toString():month.toString());
expYear = year.toString();
etExpiryDate.setText(expMonth + "/" + expYear);
}
});
}
This code works and hides the day in a DatePicker, so I can show Expiry Date for Cards. Enjoy!!
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
txtMonth.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
datePickerDialog.show();
refer this link Disable day selection on datepicker example
I am a little too late but posting the edited answer provided by #ULHAS PATIL above that works in case someone needs the same thing!
private Calendar mCalendar;
mCalendar = Calendar.getInstance();// Initialize Calendar
The xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/DarkGrey"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
android:id="#+id/date_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:calendarViewShown="false"
android:theme="#style/DialogTheme"
android:datePickerMode="spinner" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_gravity="end"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_gravity="start"
android:layout_marginVertical="20dp"
android:layout_marginStart="50dp"
android:background="#android:color/transparent"
android:gravity="center"
android:text="#string/cancel"
android:textAllCaps="false"
android:textColor="#color/grey"
android:textSize="20sp" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/ok"
android:layout_width="48dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginVertical="20dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="#id/cancel"
android:background="#android:color/transparent"
android:fontFamily="#font/kontora_extra_bold"
android:gravity="center"
android:text="#string/ok"
android:textAllCaps="false"
android:textColor="#color/CoralRed"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
There should be index 0 to hide the date filled, in ll2.getChildAt(0).setVisibility(View.GONE); and visibility set to View.GONE so the space for the date field is not occupied.
private void createDialogWithoutDateField() {
Calendar mCalendar;
mCalendar = Calendar.getInstance();
System.out.println("Inside Dialog Box");
final Dialog dialog = new Dialog(requireContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.date_picker_layout);
dialog.show();
final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
AppCompatButton okBtn = (AppCompatButton) dialog.findViewById(R.id.ok);
AppCompatButton cancelBtn = (AppCompatButton) dialog.findViewById(R.id.cancel);
datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), null);
LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
ll2.getChildAt(0).setVisibility(View.GONE);
okBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
Integer month = datePicker.getMonth() + 1;
Integer year = datePicker.getYear();
String expYear = year.toString();
tvMonthYear.setText(month+ " " + expYear);
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
I don't think you need to go that deep unless you wanna customize it..
there is the :
mDatePicker.getDatePicker().setCalendarViewShown(false);
which is a very legitimate call to remove the calendar segment.

Categories

Resources