I would like to change the colors of the AlertDialog elements using only xml.
The elements I want to change using xml are:
- Background color of the AlertDialog
- Url text color
- Line color of textInput when focused
- Color of the text on the two AlertDialog buttons
I would like the size of the AlertDialog to remain unchanged.
I tried using the styles: How can I change default dialog button text color in android 5
But they don't work and the size of the AlertDialog changes, it shrinks.
Can you give me a hand?
public void AlertDialog() {
View viewInflated = LayoutInflater.from(MainActivity.this).inflate(R.layout.text_input, null);
final EditText input = (EditText) viewInflated.findViewById(R.id.input);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Url");
builder.setView(viewInflated);
builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
dialog.dismiss();
String text = input.getText().toString();
if (!text.equals("")) pinAppWidget(text);
});
builder.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel());
builder.show();
}
text_input.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<AutoCompleteTextView
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text"
android:imeOptions="actionDone"
android:inputType="text" />
</FrameLayout>
values/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Test" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
<item name="android:statusBarColor">@color/colorPrimary</item>
</style>
<style name="Theme.Test" parent="Base.Theme.Test" />
</resources>
values-night/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Test" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your dark theme here. -->
<!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
<item name="android:statusBarColor">@color/colorPrimary</item>
</style>
</resources>