In short, I'm working on a "DurationPickerDialog" that works similarly to how the DatePickerDialog works but works based on the xsd:duration type, so the user specifies the number of years, months, days, etc.
I've also implemented a "fuzzy duration" function that gives me durations as a string like "one month ago". I would really like to be able to update the DurationPickerDialog's title in the same manner that the DatePickerDialog's title is updated, but there seems to be a problem. In the DatePickerDialog, they have it set to be a single line all the time, so that it doesn't get "jumpy." Here's how Android's source does the DatePickerDialog's title.
// Note: before the skim-readers look at this bit, realize that this is NOT my
// code but Android's internal code for the DatePickerDialog.
@Override
public void show() {
super.show();
/* Sometimes the full month is displayed causing the title
* to be very long, in those cases ensure it doesn't wrap to
* 2 lines (as that looks jumpy) and ensure we ellipsize the end.
*/
TextView title = (TextView) findViewById(R.id.alertTitle);
title.setSingleLine();
title.setEllipsize(TruncateAt.END);
}
Unfortunately, I cannot access their R.id.alertTitle
, because it is part of com.android.internal.R
.
I have seen implementations like this stackoverflow post where it would have me modify the Window.FEATURE_CUSTOM_TITLE
attribute, but that doesn't seem to let me modify the title (easily) after that.
There was also another stackoverflow post that mentioned how to change the title at runtime between two different XML layouts, but that also doesn't seem like it would be all too helpful, since the title should be modified every time the duration changes, and creating an XML layout for each duration is obviously not a good idea.
So, since they "cheated" by accessing a value that us mere mortals don't have access to, is there another way that I could go about doing it?
Edit: And through some black magic, it seems that it now does ellipsize the text like I was wanting? Only earlier it wasn't, and now I can't seem to reproduce the problem. So, I suppose while we're at it, can someone explain to me how I might have accomplished this magic?
I would implement a custom dialog by extending the
Dialog
class and creating a custom xml layout for it.You'd need some custom button backgrounds for the plus / minus and top / bottom combos and some button listeners to manipulate the values.
Since you are going for a duration value, you are probably going to need more space than the existing dialog gives you anyway.
This way you can set the title to whatever you like. Let me know if you need a code example.
Example: The dialog class:
In your activity, you call
this.showDialog(DURATION_DIALOG);
// DURATION_DIALOG is just an integer specified at the top of your Activity to identify the dialog for the next peice of code, which handles actually creating the dialog:Finally, you can set your dialog theme as is done above in your
styles.xml
file. For example:Good luck!
If you want to modify the dialog title's TextView (change text, style, behaviour...) without using a custom view, just do it that way: