Attributes from style files having no effects on views - java

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.

Related

How to set EditText style from styles.xml in Android?

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.

Why boxes are invisible in checkbox?

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)

Set View style dynamically

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.

Custom Alert Style with Multiple Theme

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

Applying some styles to control

As you know in html/css you may apply some classes (and hence some styles) to the tag. It is cery convenient and i would like to have tgis opportunity in android development. Is there a way to apply some styles to one control?
Yes you can implement styles in your values/styles.xml
you can do something like :
<style name="MyMainStyle">
<item name="android:layout_width">250dp</item>
<item name="android:layout_height">60dp</item>
</style>
<style name="MySecondaryStyle" parent="MyMainStyle">
<item name="android:paddingLeft">20dp</item>
</style>
As you can see you can also have parent styles. Then you can use the style in your layout.xml like so :
<Button
style="#style/MyMainStyle"
android:id="#+id/myButton"
/>

Categories

Resources