Change timepicker background color in dialog
Asked Answered
C

3

5

I've got a time picker in my custom dialog. This is the result :

enter image description here

This is my code:

<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">#FFFFFF</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowNoTitle">true</item>
</style>

final Dialog dialog = new Dialog(SabadKharid_s1.this, R.style.DialogStyler2);
                    dialog.setContentView(R.layout.dialogtime);

                    timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
                    timePicker.setIs24HourView(true);

                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    lp.copyFrom(dialog.getWindow().getAttributes());
                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    dialog.show();
                    dialog.getWindow().setAttributes(lp);

As you can see, the hour and minute textView in title the are white. How can I change their background color?

Cooperate answered 13/4, 2017 at 4:25 Comment(1)
I think the background color of your Dialog is white because <item name="android:background">#FFFFFF</item>. Try change it to another colorGuiana
W
9

This happens if you change the color of 'background' rather than 'windowBackground'. This worked for me:

styles.xml

<style name="DialogTheme" parent="Theme.AppCompat.Dialog">
    <item name="android:windowBackground">#333</item>
    <item name="colorAccent">#2BF</item>
</style>

MainActivity.java

Calendar cal = Calendar.getInstance();
TimePickerDialog tpd = new TimePickerDialog(
        this,
        R.style.DialogTheme,
        this, // must implement TimePickerDialog.OnTimeSetListener
        cal.get(Calendar.HOUR_OF_DAY),
        cal.get(Calendar.MINUTE),
        false);

tpd.show();
Warfeld answered 8/3, 2018 at 9:19 Comment(0)
A
0

background won't work, Try this -

<style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">@color/your_accent</item>
    <item name="android:colorPrimary">@color/your_primary</item>
    <item name="android:colorPrimaryDark">@color/your_primary_dark</item>
</style>
Abran answered 13/4, 2017 at 4:37 Comment(0)
T
0

You can refer the below code to get the timepicker style.

MainActivity.java

import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
    Calendar calendar ;
    Button button;
    TimePickerDialog timePickerDialog ;
    int CalendarHour, CalendarMinute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        calendar = Calendar.getInstance();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CalendarHour = calendar.get(Calendar.HOUR_OF_DAY);
                CalendarMinute = calendar.get(Calendar.MINUTE);
                timePickerDialog = new TimePickerDialog(MainActivity.this, R.style.DialogTheme,new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                        Toast.makeText(MainActivity.this, "Time  Selected"+selectedHour+":"+selectedMinute, Toast.LENGTH_SHORT).show();
                    }
                }, CalendarHour, CalendarMinute, true);//Yes 24 hour time
                timePickerDialog.setTitle("Select Time");
                timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        Toast.makeText(MainActivity.this, "Time Not Selected", Toast.LENGTH_SHORT).show();
                    }
                });
                timePickerDialog.show();
            }
        });
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK HERE TO SHOW TIME PICKER DIALOG WITH MATERIAL DESIGN THEME"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Style.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/purple</item>
    <item name="colorControlNormal">@color/orange</item>
</style>
Tisbe answered 27/4, 2017 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.