Reputation: 487
I've got a time picker in my custom dialog. This is the result :
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?
Upvotes: 5
Views: 6734
Reputation: 219
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();
Upvotes: 9
Reputation: 2172
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>
Upvotes: 0
Reputation: 6857
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>
Upvotes: 0