This question already has answers here:
How to set round corners for a custom Dialog?
(2 answers)
Closed 2 years ago.
I'm trying to customize layout of a DialogFragment my idea was to create a rounded corners dialog with buttons text color set to colorPrimary:
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class ExitDialogFragment extends DialogFragment {
ExitDialogListener listener;
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_exit, null))
.setMessage(R.string.exit_dialog_title)
.setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogPositiveClick(ExitDialogFragment.this);
}
})
.setNegativeButton(R.string.back_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogNegativeClick(ExitDialogFragment.this);
}
});
return builder.create();
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (ExitDialogListener) context;
} catch (Throwable e) {
throw new ClassCastException("Must implement NoticeDialogListener");
}
}
public interface ExitDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
}
The dialog layout (dialog_exit.xml) is defined in this way:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_dialog"
android:orientation="vertical"
android:theme="#style/DialogTheme">
</LinearLayout>
Here I defined DialogTheme (styles.xml):
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="Launcher" parent="AppTheme">
<item name="android:windowBackground">#drawable/launch_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
</style>
<style name="UriEditText" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/text_color_clear</item>
<item name="colorControlActivated">#color/text_color_clear</item>
</style>
</resources>
And here's the dialog background (rounded_dialog.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<corners android:radius="20dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
Nothing is applied. The dialog is just rectangular and buttons color is set to AppTheme colorAccent.
Use CardView as container layout in rounded_dialog.xml.
Here is rounded_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<!--Put a Constraint Layout(or any other container) here with required views-->
</androidx.cardview.widget.CardView>
Related
I have an issue styling the (PreferenceScreen) in my project.
How can I change the colors of of (Title) & (Summary) texts.
No luck after trying for hours with different methods, I must be doing something wrong
SettingsFragment.Java
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.flContent, new SettingsPreference())
.commit();
}
public static class SettingsPreference extends PreferenceFragment {
public SettingsPreference() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
fragment_setings.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/app_background"
android:clickable="true"
android:focusable="true"
tools:context="com.companyv.app.fragments.SettingsFragment">
</FrameLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="pref_key_storage_settings"
android:title="First">
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_auto_delete"
android:summary="Sub 1 summary"
android:title="Sub 1" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_sms_delete_limit"
android:summary="Sub 2 summary"
android:title="Sub 2" />
<Preference
android:dependency="pref_key_auto_delete"
android:key="pref_key_mms_delete_limit"
android:summary="Sub 3 summary"
android:title="Sub 3" />
</PreferenceCategory>
</PreferenceScreen>
I have done this before by using a custom preference theme and setting the
android:textColor(title color) and android:textColorSecondary(summary color) in the custom theme. For example, black title and white summary:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="preferenceTheme">#style/AppPreferenceTheme</item>
</style>
<!-- Custom Preference Theme -->
<style name="AppPreferenceTheme"
parent="#style/PreferenceThemeOverlay.v14.Material">
<item name="android:textColor">#android:color/black</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
You can rename "AppPreferenceTheme" to whatever name you like.
I am trying to apply custom color in AlertDialogue. Its changing text color as well background color in it but not changing SingleChoiceItems text color. You can see it black color in it with page number like below image.
I am using style like below code
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:textColor">#color/TextColorLite</item>
<item name="android:background">#color/colorPrimaryDarkLite</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">?attr/MainAccentColor</item>
<item name="android:buttonBarButtonStyle">#style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">?attr/TextColor</item>
<item name="android:textStyle">bold</item>
</style>
Java Code For Dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.AlertDialogTheme);
builder.setTitle("Go to Page:");
builder.setSingleChoiceItems(mPageOptions, mPageIndx - 1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
mOptionDiag.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
mOptionDiag = builder.create();
mOptionDiag.show();
What I should do for change this black text color ?
Thanks
I have got solved adding this items in style.
<!--For List Text-->
<item name="textColorAlertDialogListItem">#color/TextColorLite</item>
<!--For Radio-->
<item name="android:textColorSecondary">#color/TextColorLite</item>
Thanks
In your resources folder open styles.xml and add a custom theme like for example this:
<style name="radiobuttonstyle" parent="Material.Drawable.RadioButton">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/md_green_900</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/md_green_400</item>
<item name="colorControlNormal">#color/md_green_900</item>
<item name="colorControlActivated">#color/md_green_400</item>
</style>
and in your RadioButton in your layout add it as the theme of your RadioButton like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioyes"
android:theme="#style/radiobuttonstyle"
android:text="Yes"
android:textColorLink="#color/md_light_green_900"
android:textColorHighlight="#color/md_green_900"
android:textColor="#color/md_light_green_900" />
PS: that is my own custom radio button so it should work.
I created multiple alert dialogs in different activities. Now all of sudden the AlertDialog items appear in red !
Here is my code
AlertDialog.Builder builder = new AlertDialog.Builder(this, MyDialogStyle);
builder.setTitle("Select Method :");
final String[] choices = new String[]{"Weight Based", "Body Surface Area"};
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on choice[which]
switch (choices[which]) {
case "Weight": {
create_item(false);
break;
}
case "Body Surface Area": {
create_item(true);
break;
}
}
}
});
builder.show();
styles.xml (v21)
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:colorPrimary">#000000</item>
<item name="android:colorAccent">#000000</item>
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearanceListItemSecondary">#000000</item>
<item name="android:textAppearance">#000000</item>
<item name="android:color">#000000</item>
styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>\
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearance">#000000</item>
</style>
I tried changing the parent style or deleting it, but both give the same bright red result ! any ideas ?
try use this style ,,, hope it'll work for you
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
I am making an Android app using Toolbar with Custom style but when i'm running the app, i'm getting the following error:
Caused by: java.lang.IllegalStateException: This Activity already has
an action bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in
your theme to use a Toolbar instead.
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:textColor">#color/textColorPrimary</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:textColorSecondary">#color/textColorPrimary</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
</style>
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Used for the bottom line when not selected / focused -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:colorBackground">#color/colorBackground</item>
<item name="android:textStyle">bold</item>
<item name="android:endColor">#color/colorPrimary</item>
<!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>
<style name="TabTextAppearance" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:colorBackground">#color/colorBackground</item>
<item name="android:textAllCaps">false</item>
<item name="android:textStyle">bold</item>
</style>
MainActivity:
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_information_category1);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar_information);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new TodayInformationFragment(), "Today");
adapter.addFragment(new ThisWeekInformationFragment(), "This Week");
adapter.addFragment(new ThisMonthInformationFragment(), "This Month");
adapter.addFragment(new AllyearInformationFragment(), "All");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Clearly error says:
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead
Use this in your Styles:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Updated:
Android studio's default example is like this, take a look at the AppTheme.NoActionBar:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
And in your Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And make sure your Toolbar implemented correctly.
For example, with AppbarLayout:
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarmain"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ColorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Just go to your Manifest file and in the specific activity add
android:theme="#style/AppTheme.NoActionBar"/>
in my case that happened because the phone was in dark mode and I didn't provide a dark mode for my app maybe. it works fine after disabling the dark mode.
in my case i was testing on mobile which was in dark theme i got this and the following code done work for me
<style name="Theme.QrCodeScanner"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
set this in both themes.xm and themes.xml(night)
or if you want it for a specific activty then do the following in your manifest in specific activity
android:theme="#style/AppTheme.NoActionBar"/>
I have custom theme with changed primary and accent colors. But AlertDialog is not picking up those colors automatically when I create dialog using:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
This is what I get - dialog with default colors:
And this is what I would like to have - dialog with custom colors:
I can create custom AlertDialog theme and apply it during AlertDialog creation:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme_Dialog);
I would like to skip calling constructor with theme parameter.
Is there a way to force custom colors on all themes without deriving them in styles, including AlertDialog base theme?
Minimup API level I have to support is 15.
My activity code
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public void dialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
My customized style
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
My colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Add this in your the App theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">#style/AppTheme.Dialog</item>
And then,
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);