how to make a date picker in Android? - android

Could we have a DATE picker in Android that is not calling the popup dialog?
Why is useSpinner deprecated in 5.4.0?
I made a test like this, and it resulted in a strange black field and I don't see any dropdown.
var androidDatePicker = Ti.UI.createPicker({
type:Ti.UI.PICKER_TYPE_DATE,
height:Ti.UI.SIZE,
width:Ti.UI.FILL,
minDate:moment().add(10,"minutes").toDate(),
maxDate:moment().add(2,"years").toDate(),
value:moment().toDate()
});
$.main.add(androidDatePicker);

For some purposes it works. I know other developers whose clients also requested a simpler picker.
I implemented it through the theme, by adding approximately the following to the custom themes xml resource file:
<style name="Theme.NoActionBar" parent="#style/Theme.AppCompat.NoActionBar">
<item name="android:dialogTheme">#style/MyDialogTheme</item>
</style>
<style name="MyDialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:datePickerStyle">#style/MyDatePicker</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
</style>
<style name="MyDatePicker" parent="android:Widget.Holo.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>

Related

Theme.MaterialComponents does not change font of dialog title

I am using the material components theming for our app. Now we want a custom font, which I managed to apply almost everywhere with the theme below, which uses the various textAppearance... attributes defined by material components.
This works very well, and the theme is also applied to the AlertDialogs almost everywhere -- message text and buttons have the custom font, buttons have the correct accent colors etc.
Only the dialog title keeps the Roboto font, no matter what.
<!-- externalized font name for easier change -->
<string name="font_regular" translatable="false" tools:ignore="ReferenceType">#font/atma_regular</string>
<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:textColorSecondary">#color/textColorSecondary</item>
<item name="textAppearanceHeadline1">#style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline2">#style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline3">#style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline4">#style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline5">#style/TextAppearance.App.Button</item>
<item name="textAppearanceHeadline6">#style/TextAppearance.App.Button</item>
<item name="textAppearanceSubtitle1">#style/TextAppearance.App.Subtitle1</item>
<item name="textAppearanceSubtitle2">#style/TextAppearance.App.Subtitle2</item>
<item name="textAppearanceBody1">#style/TextAppearance.App.Body1</item>
<item name="textAppearanceBody2">#style/TextAppearance.App.Body2</item>
<item name="textAppearanceButton">#style/TextAppearance.App.Button</item>
<item name="android:textAppearanceLarge">#style/TextAppearance.App.Large</item>
<item name="android:textAppearanceMedium">#style/TextAppearance.App.Medium</item>
<item name="android:textAppearanceSmall">#style/TextAppearance.App.Small</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">#string/font_regular</item>
<item name="android:fontFamily">#string/font_regular</item>
</style>
...
I tried to define an extra theme for the alert dialogs like so:
<style name="Theme.App" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="alertDialogTheme">#style/Theme.App.AlertDialog</item>
...
</style>
<style name="Theme.App.AlertDialog" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="android:windowTitleStyle">#style/TextAppearance.App.DialogWindowTitle</item>
</style>
<style name="TextAppearance.App.DialogWindowTitle" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="fontFamily">#string/font_regular</item>
<item name="android:fontFamily">#string/font_regular</item>
<item name="android:textSize">30sp</item>
</style>
But that resets the colors and fonts everywhere. The only thing that is applied, is the textSize.
It really shouldn't be so hard to achieve this, but I am out of ideas right now. I could apply the font programmatically, but that would be quite ugly.
As you mentioned the AlertDialog created by MaterialAlertDialogBuilder uses the style defined by the textAppearanceSubtitle1 attribute in your app theme.
Otherwise you can change the style used by the AlertDialog using something like:
new MaterialAlertDialogBuilder(MainActivity.this,
R.style.MyTitle_ThemeOverlay_MaterialComponents_MaterialAlertDialog)
.setTitle("Title")
.setMessage("Message......")
.setPositiveButton("ok", null)
.setNegativeButton("Cancel", null)
.show();
In your style you have to customize the materialAlertDialogTitleTextStyle attribute:
<style name="MyTitle_ThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogTitleTextStyle">#style/MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text</item>
</style>
<style name="MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text" parent="#style/MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">#style/MyTitle_TextAppearance.MaterialComponents.Subtitle1</item>
</style>
<style name="MyTitle_TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">....</item>
<item name="android:fontFamily">....</item>
<item name="android:textStyle">.....</item>
</style>
Thanks to your question I realized that I was using the incorrect attribute to assign the title style (android:titleTextAppearance instead of android:windowTitleStyle).
It looks like your issue has been fixed in the meantime, as your exact code seems to work for me as of today.

Android datepicker dialog title pillarboxed

I'm trying to style the datepicker dialog. So far I managed to change the background and button color. But as you can see in the image below, the title is sort of 'pillar-boxed'.
My datepicker dialog with the dark boxes left and right of the title:
My xml code so far (values/styles.xml)
<style name="PickerDialogTheme_dark" parent="Theme.AppCompat.Dialog">
<item name="android:background">#color/colorBackground_bgreen</item>
<item name="android:buttonBarButtonStyle">#style/ButtonColor</item>
</style>
<style name="ButtonColor">
<item name="android:background">#color/colorBackground_bgreen</item>
</style>
In java it is just a regular datepicker dialog constructor with the theme set to PickerDialogTheme_dark and I'm trying to get this to work on pre Lollipop versions.
So hope you guys know how to get rid of this pillar-boxed title. If possible without using fragments and all in xml would be favourable :)
Fixed it finally after searching some more on the internet!
How my final xml code looks now:
<style name="PickerDialogTheme_dark" parent="Theme.AppCompat.Dialog">
<item name="android:gravity">center</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:alertDialogStyle">#style/DialogBackground_dark</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
</style>
<style name="DialogBackground_dark">
<item name="android:topDark">#color/colorAccent_bgreen</item>
<item name="android:centerDark">#color/colorAccent_bgreen</item>
<item name="android:bottomDark">#color/colorAccent_bgreen</item>
<item name="android:bottomMedium">#color/colorAccent_bgreen</item>
</style>
This is how it looks like now

Change the selected day background color

I've been trying to stylize a DatePicker -- NOT DatePickerDialog, if that matters -- without any success.
The problem I'm facing is this:
As you can see, the selected day background is black. I've tried to apply the following style:
<style name="DatePickerStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary"> #color/Red </item>
<item name="colorPrimaryDark"> #color/Blue </item>
<item name="colorAccent"> #color/Green </item>
</style>
But I would only get errors like:
No resource found that matches the given name: attr 'colorPrimary'
and
Error retrieving parent for item:
No resource found that matches the given name 'Theme.AppCompat.Light.Dialog'.
I've tried to do it via code, but so far the only thing I've managed to stylize was the header background, by doing something like this:
( ( datePicker.GetChildAt( 0 ) as LinearLayout ).GetChildAt( 0 ) as LinearLayout )
.SetBackgroundResource( Utils.AppColorResourceId );
or, by direct id
datePicker.FindViewById( 16909081 ).SetBackgroundResource( Utils.AppColorResourceId );
Although I'm avoiding the later mostly because I'm afraid the id changes with the device/OS version.
So my question is: How can I change the colour used to mark the selected day?
Is there a way to do it via code? If so, how is it? ( I'd like to change the colour at runtime due to some actions the user can do to change the colour )
EDIT
Styles.xml
...
<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#color/Blue</item>
<item name="android:datePickerStyle">#style/DatePickerStyle</item>
<item name="android:colorAccent">#color/Red</item>
</style>
<style name="DatePickerStyle" parent="#android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">#color/Red</item>
<item name="android:calendarTextColor">#color/Blue</item>
<item name="android:dayOfWeekBackground">#color/Green</item>
<item name="android:yearListSelectorColor">#color/Base</item>
</style>
...
Use below code for changing your selected date color
<style name="MyDatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<!-- this is new -->
<item name="colorAccent">#color/accent</item>
<item name="android:datePickerStyle">#style/MyDatePickerStyle</item>
<item name="android:colorAccent">#color/primDark</item>
</style>
<style name="MyDatePickerStyle" parent="#android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">#color/prim</item>
<item name="android:calendarTextColor">#color/primDark</item>
<item name="android:dayOfWeekBackground">#color/primDark</item>
<item name="android:yearListSelectorColor">#color/accent</item>
<item name="android:datePickerMode">calendar</item>
<item name="android:minDate">01/01/2000</item>
</style>
Override datePickerDialogTheme inside your app's base theme:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
....
<item
name="android:datePickerDialogTheme">#style/MyDatePickerDialogTheme</item>
</style>
here is your MyDatePickerDialogTheme
<style name="MyDatePickerDialogTheme"
parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">#style/MyDatePickerStyle</item>
</style>
We have overridden datePickerStyle with the style MyDatePickerStyle. The choice of parent will once again depend on what your app's base theme is: either Widget.Material.DatePicker or Widget.Material.Light.DatePicker. Define it as per your requirements:
<style name="MyDatePickerStyle" parent="#android:style/Widget.Material.Light.DatePicker">
<item name="android:headerBackground">#color/chosen_header_bg_color</item>
with this you can also have some more customization i listed bellow
dayOfWeekBackground
dayOfWeekTextAppearance
headerMonthTextAppearance
headerDayOfMonthTextAppearance
headerYearTextAppearance
headerSelectedTextColor
yearListItemTextAppearance
yearListSelectorColor
If you don't want this much control (customization), you don't need to override datePickerStyle. colorAccent controls most of the DatePicker's colors. So, overriding just colorAccent inside MyDatePickerDialogTheme should work:
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorAccent">#color/date_picker_accent</item>
<!-- No need to override 'datePickerStyle' -->
<!-- <item name="android:datePickerStyle">#style/MyDatePickerStyle</item> -->

Can't find source of TYPE_SYSTEM_ALERT

For a project I want to create my own window style. I mean something like this:
<style name="MyFloatingWindow">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
To get some ideas how I can implement my ideas I want to look into the style source of andriod but I can't find it. What is the original style of WindowManager.LayoutParams.TYPE_SYSTEM_ALERT?
Or is it possible to create a new layout style with TYPE_SYSTEM_ALERT as parent? What do I have to write as parent?
<style name="MyOverlay" parent="android:***">
</style>
The TYPE_SYSTEM_ALERT simply means that the windows are always on top of application windows. In multiuser systems shows only on the owning user's window. It has nothing to do with the any style of your window or how the window will look like.
There are a lot of pre-defined styles in styles.xml under frameworks\base\core\res\res\values. For example the AlertDialog.
<style name="AlertDialog">
<item name="fullDark">#android:drawable/popup_full_dark</item>
<item name="topDark">#android:drawable/popup_top_dark</item>
<item name="centerDark">#android:drawable/popup_center_dark</item>
<item name="bottomDark">#android:drawable/popup_bottom_dark</item>
<item name="fullBright">#android:drawable/popup_full_bright</item>
<item name="topBright">#android:drawable/popup_top_bright</item>
<item name="centerBright">#android:drawable/popup_center_bright</item>
<item name="bottomBright">#android:drawable/popup_bottom_bright</item>
<item name="bottomMedium">#android:drawable/popup_bottom_medium</item>
<item name="centerMedium">#android:drawable/popup_center_medium</item>
<item name="progressLayout">#android:layout/progress_dialog</item>
<item name="horizontalProgressLayout">#android:layout/alert_dialog_progress</item>
</style>

AlertDialog styling - how to change style (color) of title, message, etc

I've breaking my head over this quite a bit. What I need to do is, change the style of all AlertDialogs in my android application - dialog background needs to be white-ish, and text needs to be black-ish. I tried creating a lot of styles, themes, and applying from the code, manifest, etc, but no success, with regard to the text colors inside the AlertDialog. Right now, I have the simplest of codes, set like this:
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
styles.xml:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:alertDialogStyle">#style/DialogStyle</item>
</style>
<style name="DialogStyle" parent="#android:style/Theme.Dialog">
<!-- changing these background stuff works fine -->
<item name="android:bottomBright">#android:color/white</item>
<item name="android:bottomDark">#android:color/white</item>
<item name="android:bottomMedium">#drawable/dialog_footer_bg</item>
<item name="android:centerBright">#android:color/white</item>
<item name="android:centerDark">#drawable/dialog_body_bg</item>
<item name="android:centerMedium">#android:color/white</item>
<item name="android:fullBright">#color/orange</item>
<item name="android:fullDark">#color/orange</item>
<item name="android:topBright">#color/green</item>
<item name="android:topDark">#drawable/dialog_header_bg</item>
The items listed below don't work (please read the comments I've put above each element):
<!-- panelBackground is not getting set to null, there is something squarish around it -->
<item name="android:panelBackground">#null</item>
<!-- Setting this textColor doesn't seem to have any effect at all. Messages, title, button text color, whatever; nothing changes. -->
<item name="android:textColor">#000000</item>
<!-- Also tried with textAppearance, as follows. Didn't work -->
<item name="android:textAppearance">?android:attr/textColorPrimaryInverse</item>
<!-- Also tried changing textAppearancePrimary, to no avail -->
<item name="android:textColorPrimary">#000000</item>
<!-- Also need to change the dialog title text, tried it as follows, dint work: -->
<item name="android:windowTitleStyle">#style/DialogWindowTitle</item>
</style>
The DialogWindowTitle is defined as follows:
<style name="DialogWindowTitle">
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
So none of these is working. Can anyone tell me what I could be doing wrong, and how can I:
Change text color for messages (content text)
Change title text color
Remove the
panel background
Note: I need to support API 8 (2.2) upwards. Also, I've went through most of the related question here, and google groups, but can't figure out, though I have a feeling its right under my nose!
Edit: adding screenshot:
You need to define a Theme for your AlertDialog and reference it in your Activity's theme. The attribute is alertDialogTheme and not alertDialogStyle. Like this:
<style name="Theme.YourTheme" parent="#android:style/Theme.Holo">
...
<item name="android:alertDialogTheme">#style/YourAlertDialogTheme</item>
</style>
<style name="YourAlertDialogTheme">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowTitleStyle">...</item>
<item name="android:textAppearanceMedium">...</item>
<item name="android:borderlessButtonStyle">...</item>
<item name="android:buttonBarStyle">...</item>
</style>
You'll be able to change color and text appearance for the title, the message and you'll have some control on the background of each area. I wrote a blog post detailing the steps to style an AlertDialog.
Remove the panel background
<item name="android:windowBackground">#color/transparent_color</item>
<color name="transparent_color">#00000000</color>
This is Mystyle:
<style name="ThemeDialogCustom">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowBackground">#color/transparent_color</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Which i have added to the constructor.
Add textColor :
<item name="android:textColor">#ff0000</item>
Here's my Code to theme the alert dialog box:
<style name="alertDialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:background">#color/light_button_text_color</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorPrimary">#android:color/black</item>
<item name="android:textColorSecondary">#android:color/black</item>
<item name="android:titleTextColor" tools:targetApi="m">#android:color/black</item>
</style>
Place this code in styles.xml.
In your java apply this theme as:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.alertDialog);
Output of the code
You have to add the style to the constructor of the dialog
builder = new AlertDialog.Builder(this, R.style.DialogStyle);
I changed color programmatically in this way :
var builder = new AlertDialog.Builder (this);
...
...
...
var dialog = builder.Show ();
int textColorId = Resources.GetIdentifier ("alertTitle", "id", "android");
TextView textColor = dialog.FindViewById<TextView> (textColorId);
textColor?.SetTextColor (Color.DarkRed);
as alertTitle, you can change other data by this way (next example is for titleDivider):
int titleDividerId = Resources.GetIdentifier ("titleDivider", "id", "android");
View titleDivider = dialog.FindViewById (titleDividerId);
titleDivider?.SetBackgroundColor (Color.Red);
this is in C#, but in java it is the same.
It depends how much you want to customize the alert dialog. I have different steps in order to customize the alert dialog. Please visit: https://stackoverflow.com/a/33439849/5475941
Building on #general03's answer, you can use Android's built-in style to customize the dialog quickly. You can find the dialog themes under android.R.style.Theme_DeviceDefault_Dialogxxx.
For example:
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_MinWidth);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_DialogWhenLarge);
Use this in your Style in your values-v21/style.xml
<style name="AlertDialogCustom" parent="#android:style/Theme.Material.Dialog.NoActionBar">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorAccent">#color/cbt_ui_primary_dark</item>
<item name="android:windowTitleStyle">#style/DialogWindowTitle.Sphinx</item>
<item name="android:textColorPrimary">#color/cbt_hints_color</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
</style>
And for pre lollipop devices put it in values/style.xml
<style name="AlertDialogCustom" parent="#android:style/Theme.Material.Dialog.NoActionBar">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorAccent">#color/cbt_ui_primary_dark</item>
<item name="android:windowTitleStyle">#style/DialogWindowTitle.Sphinx</item>
<item name="android:textColorPrimary">#color/cbt_hints_color</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
</style>
<style name="DialogWindowTitle.Sphinx" parent="#style/DialogWindowTitle_Holo">
<item name="android:textAppearance">#style/TextAppearance.Sphinx.DialogWindowTitle</item>
</style>
<style name="TextAppearance.Sphinx.DialogWindowTitle" parent="#android:style/TextAppearance.Holo.DialogWindowTitle">
<item name="android:textColor">#color/dark</item>
<!--<item name="android:fontFamily">sans-serif-condensed</item>-->
<item name="android:textStyle">bold</item>
</style>

Categories

Resources