I want to add custom style to negative and positive buttons I use in alertDialogBuilder. How can this be done? Is it somehow possible to create styles with XML and use it in all alertDialogBuilder buttons? In my case I want all the texts in alertdialog buttons to have dotted underlines.
I have tried to figure how to set colors for buttons but this is not enough.
...
alertDialog.show(); alertDialog.getButton(alertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#FFFFFF"));
The expected results would be to be able to define style that can be easily used in all alertDialogBuilder buttons.
Yes, U can create a style in ur styles.xml file and call this style/theme as wherever u want. U can try with below code
<style name="MyCustomThemeForAlertDialog" parent="#android:style/Theme.Holo.Dialog.NoActionBar">
<item name="colorAccent">#ffffff</item>
<item name="android:textSize">14sp</item>
<!-- U can call another style as your desired style-->
<item name="android:textAppearance">#style/MyMsgBodyTextAppearance</item>
</style>
<style name="MyMsgBodyTextAppearance" parent="#android:style/TextAppearance.Holo.Medium">
<item name="android:textStyle">italic</item>
<item name="android:left">-5dp</item>
<item name="android:right">-5dp</item>
<item name="android:top">-5dp</item>
<item name="android:dashWidth">2dp</item>
<item name="android:dashGap">3dp</item>
<item name="android:width">1dp</item>
</style>
Then call your created theme in code as below
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyCustomThemeForAlertDialog);
builder.setCancelable(false);
builder.setTitle("Title");
builder.setMessage("Do you want to Quit?");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
You can follow this guide to apply style in your alertdialog.
How to style AlertDialogs like a pro
Related
I'm generating EditText fields and wish to set it's style from styles.xml file. But can't manage to do it. I can set some parameters, but I would rather use a style from styles.xml so it would be easier to change later if needed.
styles.xml
<style name="input_fields_single_line">
<item name="android:padding">8dp</item>
<item name="android:background">#drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Java code:
List<EditText> inputFieldList = new ArrayList<EditText>();
public void generateInputFields() {
EditText editTextFieldTitle = new EditText(this);
inputFieldList.add(editTextFieldTitle);
editTextFieldTitle.setHint(R.string.enter_field_title);
editTextFieldTitle.setMaxLines(1);
editTextFieldTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editTextFieldTitle.setFocusableInTouchMode(true);
editTextFieldTitle.requestFocus();
myLayout.addView(editTextFieldTitle);
}
You create a separate style for the EditText like what you are doing but parented with the Widget.EditText like the following:
<style name="MyCustomeEditTextStyle" parent="#android:style/Widget.EditText">
...add as many items as you need
<item name="android:padding">8dp</item>
<item name="android:background">#drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Then call your style inside each edit text in the xml like the following:
<EditText
android:id="#+id/editTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyCustomeEditTextStyle" />
BUT if you are want it to be globally set once and set without the xml because you are generating your EditText programmatically like this:
EditText editTextFieldTitle = new EditText(this);
you can then set your style in the application style like this:
<style name="AppTheme" parent="#android:style/YourParentTheme">
<item name="android:editTextStyle">#style/MyCustomeEditTextStyle</item>
</style>
I found the solution here. Android: set view style programmatically
editTextFieldTitle = new EditText(new ContextThemeWrapper(getBaseContext(),R.style.input_fields_single_line),null,0);
There is a attribute in EditText called styles use that.and if you want to customize it than make own style in drawable , A new XML and link to the editText using styles attribute.
after created this style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/offer_text_background</item>
<item name="colorPrimaryDark">#color/app_background</item>
<item name="android:checkboxStyle">#style/settingsNotificationCategory</item>
</style>
<style name="settingsNotificationCategory">
<item name="android:textSize">30sp</item>
</style>
My box from checkbox is removing:
invisible boxes
Without this style:
visible boxes
I need create chceckbox dynamically in kotlin:
var checkBox = CheckBox(this)
checkBox.text = category
checkBox.setTextColor(resources.getColor(R.color.customText))
checkBox.isChecked = true
notificationCategoryLayout.addView(checkBox)
what's happened?
I tried :
var checkBox = CheckBox(this, null, R.style.settingsNotificationCategory)
checkBox.text = category
checkBox.setTextColor(resources.getColor(R.color.customText))
checkBox.isChecked = true
notificationCategoryLayout.addView(checkBox)
but the effect is the same...
Thanks for help
You are given the checkbox a style that contains only the textSize so it will affect on the style of the checkbox you can do 2 things:
first just set the textsize programatically:
checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
or adjust your style to be like this:
<style name="settingsNotificationCategory" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:textSize">30sp</item>
</style>
to inherit the base style of the checkbox
but when I create checbox in layout:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textColor="#color/customText"
android:theme="#style/settingsNotificationCategory"/>
this is working without
parent="Widget.AppCompat.CompoundButton.CheckBox"
is possible to set theme in code?
I want set textSize via style because I use layouts to different resolution (for large, xlarge, small sizes)
I am confused while using custom alert dialogue styles in my application, due to there being multiple themes in my application. I have three themes in my application, called themeGrey, themeTeal and themePink. I have an alert dialogue style like below
style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/colorAccent</item>
<item name="android:textColor">#color/colorAccent</item>
<item name="android:background">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="textColorAlertDialogListItem">#color/colorAccent</item>
<item name="android:textColorSecondary">#color/colorAccent</item>
</style>
I have used it in my app like below
mProgress = new ProgressDialog((this), R.style.AlertDialogTheme);
Now my question is, how can I define a different style for each theme?
I do not want apply with a conditional. Can I do it by declaring a theme as an item?
Thanks
I have solved removing style from java code like below
mProgress = new ProgressDialog((this));
and delcared alert style in theme like below
<item name="android:alertDialogTheme">#style/AlertDialogThemeTeal</item>
Thanks
How to set Blur background when dialog create?
I found DialogFragment only and AlertDialog but I want to set in Dialogin activity
How to create it or What's library recommend?
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
dialog.setContentView(R.layout.register_policy_dialog);
try this
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
or try this you can apply a custom theme to your dialog like this
Dialog customDialog= new Dialog(MainActivity.this,R.style.mytheme);
Window window = customDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
now create the theme like this
<style name="mytheme" parent="android:style/Theme.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
and also check this AlertDialog with Blur background in possition center
How do we change the background's foreground color when we show a DialogFragment, like when we click on the fab in the evernote or messenger app, I know the way to do it will probably be diffrent but that's the kind of effect I'm looking for
I have found a work around for this problem. Here is the code.
In styles.xml design a custom theme.
styles.xml
<style name="AppTheme.CustomTheme">
<item name="android:windowIsFloating">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
In manifest file use this style on the popup window.
Android Manifest
<activity android:name=".DialogDesign"
android:theme="#style/AppTheme.CustomTheme"
android:screenOrientation="portrait"></activity>
In layout.xml file use 3 different relativeLayouts, one as root, another as faded background and another as pop-up window.
activity_dialog_design.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".DialogDesign">
<RelativeLayout
android:id="#+id/fadedBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7fffffff"
android:layout_margin="0dp"/>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="500dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_centerHorizontal="true"
android:background="#android:color/white"/>
</RelativeLayout>
In activity.java file set the height and width of the faded window.
DialogDesign.java
public class DialogDesign extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_design);
RelativeLayout fadedBg=(RelativeLayout)findViewById(R.id.fadedBg);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = 1800;
params.width = 1100;
this.getWindow().setAttributes(params);
}
}
There, It is done! Add whatever you want to add in the inner most relative layout.
Easy answer
The amount by which the content behind a floating window (such as dialog) goes darker is determined by backgroundDimAmount attribute in your theme:
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="android:backgroundDimAmount">0.6</item>
</style>
It is limited to black color. 0.6 is the default value.
EDIT
The above answer did not work, looks like the value needs to be applied to the dialog theme, not the activity theme.
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="#style/Theme.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.6</item>
</style>
Difficult answer
Achieving a different color would require disabling this system dim and heavily customizing dialog background. I'll leave this to someone else.
It might be some late to answer. but i combined some solutions and gained the proper way to achieve this goal.
first create the following theme in your styles.xml:
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.7</item>
</style>
first enable an attribute named 'backgroundDimEnabled' to make the screen foreground darker when dialogs showing on the screen. then set a float value for amount of darkness.(0 < value < 1)
and use this theme in the onCreateDialog Method in your dialog fragment.
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyCustomTheme);
View addDialogView = getActivity().getLayoutInflater().inflate(R.layout.word_detail_fragment, null);
builder.setView(addDialogView);
return builder.create();
}