Android DatePicker dialog not showing on Android 6 - android

I have a DatePicker in my app that has always worked on both phones and tablets until Android 6. I have a Samsung Galaxy 7 Edge t test on and the dialog is not showing.
I have set the min and target sdk version to 11 and 21 respectively in the manifest.
I understand that i should be using DialogFragment but is there a reason why the code below is not showing on Android 6? The code works fine on 2.3.6, 4.x and 5.x.
The following log is being executed so i know that the .show() method has been called.
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
[EDIT]
Things i have tried are :
Adding a style to the style.xml and the calling a different Dialog constuctor that references that style. This does show a dialog on Android 6, but unfortunately it is not my custom one that uses the my layout with buttons on.
<style name="MyDialogStyle" parent="Theme.AppCompat.Light">
.
dialog = new Dialog(MenuActivity2.this, R.style.MyDialogStyle);
dialog.setContentView(R.layout.datetimepickerunalloc);
Thanks
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;
TextView dialogMessage;
DateTime timeSetOnSpinner;
public void showDateTimeDialog() {
// final Calendar c = Calendar.getInstance();
final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
dialog = new Dialog(MenuActivity2.this);
dialog.setContentView(R.layout.datetimepickerunalloc);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
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());
timeSetOnSpinner = new DateTime(c);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedDate = fmt.print(timeSetOnSpinner);
formattedDate = formattedDate.trim();
//Toast.makeText(MenuActivity2.this, "date = " + formattedDate, Toast.LENGTH_LONG).show();
final Intent intent = new Intent(MenuActivity2.this, ShowUnallocatedCallsActivity.class);
intent.putExtra("unallocdate", formattedDate);
startActivity(intent);
}// end of onClick
});
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));
}
});
dialog.setTitle("Unallocated calls date");
try {
ReSet.performClick();
dialog.show();
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
} catch (Exception e) {
// ignore
}
}// showDateTimeDialog()
.
This is my layout for the custom dialog
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="700dp"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textviewdatetimepickermessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<LinearLayout
android:id="#+id/DateLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<DatePicker
android:id="#+id/DatePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</LinearLayout>
<LinearLayout
android:id="#+id/ControlButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip" >
<Button
android:id="#+id/SetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#android:string/ok" />
<Button
android:id="#+id/ResetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
<!--
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="#+id/CancelDialog"
android:text="#android:string/cancel"
android:layout_weight="1"/>
-->
</LinearLayout>
</LinearLayout>
</ScrollView>
.

I eventually got the dialog showing by using a different constructor that passed in a theme arg.
dialog = new Dialog(MenuActivity2.this, R.style.AppBaseTheme );

Related

Trouble with Spinner: Attempted to finish an input event but the input event receiver has already been disposed

As the title says, I get this error while interacting with a Spinner. I've noticed that there are many post about this argument, but every one is different from each other (answers too). Unfortunately, I didn't find a solution, so I'm asking here.
Here is a screenshot of the Spinner:
As you can see, the first Spinner is ok, but the second has two problems:
First one, it doesn't show values
Second one, when I tap the spinner, nothing happen. If I tap again the spinner, I get the error "Attempted to finish an input event but the input event receiver has already been disposed."
Maybe the two things are connected somehow...
Here is the code:
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Integer[] radiusArray = new Integer[]{500,700,1000,1500,2000,2500,3000,4000,5000};
Integer[] geofenceRadius = new Integer[]{100,200,300,400,500};
try {
final Spinner spinnerRA = (Spinner) findViewById(R.id.search_radius);
final Spinner spinnerGR = (Spinner) findViewById(R.id.geofence_radius);
ArrayAdapter<Integer> adapterRA = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, radiusArray);
spinnerRA.setAdapter(adapterRA);
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
//Getting from preference files, saved settings, if any
//1000 and 100 are default settings
final SharedPreferences sharedPref = getSharedPreferences("Settings",Context.MODE_PRIVATE);
String temp = getResources().getString(R.string.search_radius);
int savedRadius = sharedPref.getInt(temp, 1000);
temp = getResources().getString(R.string.geofence_radius);
int savedGeofence = sharedPref.getInt(temp, 100);
//Show selected value for spinner, or default value
int i;
for(i=0; i<radiusArray.length; i++){
if(radiusArray[i].equals(savedRadius)){
break;
}
}
spinnerRA.setSelection(i);
for(i=0; i<geofenceRadius.length; i++){
if(geofenceRadius[i].equals(savedGeofence)){
break;
}
}
spinnerGR.setSelection(i);
Button Save = (Button) findViewById(R.id.save_settings_button);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer searchRadius = (Integer)spinnerRA.getSelectedItem();
Integer geofenceRadius = (Integer)spinnerGR.getSelectedItem();
//Saving new value of search_radius
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.search_radius),searchRadius);
editor.putInt(getString(R.string.geofence_radius),geofenceRadius);
editor.putBoolean(getString(R.string.initialized),true);
editor.commit();
CharSequence text = "Changes saved succesfully!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}
And here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textSize="14pt"
android:layout_marginTop="8pt"
android:gravity="center"/>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5pt"
android:id="#+id/second_gray_line"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Initial search radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/search_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Location radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/geofence_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/save_settings_button"
android:text="Save Settings"
android:textSize="10pt"
android:layout_gravity="center"
android:layout_marginTop="50pt"/>
</LinearLayout>
I think it's a really stupid error, but I can't figure out what causes it. Could you help me? Thank you!
I solved and, as I said, the error was really stupid... I made a mistake here, in the second adapter:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
This must be changed in:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerGR.setAdapter(adapterGR);
It was just an oversight. I was setting the second adapter using spinnerRA instead of spinnerGR. Now it works!

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 doesn't detect click on EditText

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();
}

How to use xml timepicker?

I have dialog button and timepicker created in the same xml code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout android:id="#+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:id="#+id/dialog_ok" android:text="OK" android:layout_width="fill_parent" android:layout_weight="1"></Button>
<Button android:layout_height="wrap_content" android:text="Cancel" android:id="#+id/dialog_cancel" android:layout_width="fill_parent" android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
And then i created dialog in java code and also bring in the dialog and timepicker from xml code as showen below:
public void TimePickerDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.sign_in_dialog);
/////////////////////updated code////////////////////
TimePicker timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
timePicker1.setIs24HourView(true);
/////////////////////////////////
dialog.setTitle("Quick Mute");
//Allow the dialog to be cancelable
dialog.setCancelable(true);
// Here we add functionality to our dialog box's content. In this example it's the two buttons
//reference the button
Button okButton = (Button) dialog.findViewById(R.id.dialog_ok);
//Create a click listener
okButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {
}
});
//reference the button
Button cancelButton = (Button) dialog.findViewById(R.id.dialog_cancel);
//Create a click listener
cancelButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {
//Close the dialog</span>
dialog.dismiss();
}
});
//finally show the dialog
dialog.show();
}
The problem i have is how do i get access to the timepicker that was created in xml code so that i can make the timepicker 24 hour timepicker? ... and also how do i get the time from the timepicker that is in xml code?
Just like its usually done:
TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker1);
and as usual this only works after dialog.setContentView is called. Then, to get the time picked, call:
timePicker.getCurrentHour();
timePicker.getCurrentMinute();
You can't set the 24 hours mode in the XML, use MyTimePicker.setIs24HourView(boolean) instead.
See this example on how to get the time.
These are the steps to get the instance of a TimePicker:
final Dialog mTimeDialog = new Dialog(this);
final RelativeLayout mTimeDialogView = (RelativeLayout)getLayoutInflater().inflate(R.layout.time_dialog, null);
final TimePicker mTimePicker = (TimePicker) mTimeDialogView.findViewById(R.id.TimePicker);
mTimePicker.setIs24HourView(true);

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