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.
Related
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 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
I'm trying to dynamically create a TextView and then set a style which I previously defined in XML.
This is the XML which I defined in styles.xml:
<style name="box_area">
<item name="android:layout_width">30dp</item>
<item name="android:layout_height">30dp</item>
<item name="android:background">#android:color/holo_red_dark</item>
</style>
Java Code:
TextView tv = new TextView(this, null, R.style.box_area);
I don't know which is the reason, but the style is not being applied.
Thank you
Add parent="android:Widget.TextView" for the given style.Then it should work fine.
Code after editing should look like this
<style name="box_area" parent="android:Widget.TextView>
<item name="android:layout_width">30dp</item>
<item name="android:layout_height">30dp</item>
<item name="android:background">#android:color/holo_red_dark</item>
Hope it helps.
In your style.xml add parent
android:Widget.TextView
to implement style like this code:
<style name="box_area” parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
Add below line into your textview's property, it will solve your problem.
style="#style/box_area"
Something like below
<TextView
style="#style/box_area"
........
/>
The height and width of View will be processed only by Layout Params which you define for Simple View Component's parent layout. If you are creating your view programmatically, you must get instance of your component's parent layout either by findViewById or instantiating your parent ViewGroup. Then assign the LayoutParam as a parameter at same time you call addView to add text view to it's parent.
also, you can add view to it's parent, then measure it by calling measure method.
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
I am using some style files in my Android application, I have for example:
<style name="message">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:gravity">center_vertical</item>
<item name="android:layout_weight">1</item>
</style>
And then I create some components using these styles by doing:
TextView tv = new TextView(new ContextThemeWrapper(this, R.style.message));
My problem is that I noticed some attributes having no effect on my components, in this case, layout_weight is not working and I am obliged to specify it in my code as below:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
params.weight = 1;
tv.setLayoutParams(params);
If you have an idea, please let me know.
Thank you.
The problem relies on the fact that android:layout_weight works only with certain LayoutParams as you've experienced.
As a general best practice in regards with styles on Android, it is advisable not to include layout specific attributes into your styles, as your styled view can be reused and sit in different layouts.