How can I change a button style dynamically in Android? [duplicate] - android

This question already has answers here:
How to programmatically set style attribute in a view
(11 answers)
Closed 8 years ago.
I want to change the style of a button dynamically, i.e. in Java code, something like:
((Button)findViewById(id)).setStyle("#styles/foo")
<resources>
<style name="foo">
<item name="android:adjustViewBounds">true</item>
<item name="android:maxHeight">100px</item>
<item name="android:maxWidth">200px</item>
</style>
</resources>
I have not seen nothing like setStyle, so:
do I have to change every single property or I can change the whole style?

To assign a style like this
<style name="ButtonHOLO" parent="android:Widget.Button">
<item name="android:background">#drawable/btn_default_holo_dark</item>
<item name="android:minHeight">#dimen/calc_btn_h</item>
<item name="android:minWidth">#dimen/calc_btn_w</item>
<item name="android:textColor">#ffffff</item>
</style>
to a button dynamically you need to use both setBackgroundResource() and setTextAppearance() functions. E.g.:
btn.setBackgroundResource(R.drawable.btn_default_holo_dark);
btn.setTextAppearance(context, R.style.ButtonHOLO);
where
btn_default_holo_dark
is a name of .xml file which describes a selector for your button.

The easiest way I found to circumvent this obvious flaw was to make two buttons. Make one of them Visibility.gone. Then simply change Visibility from the other one to gone and activate the first one by Visibility.visible.
I don't really like that solution, but it's faster and saner than the alternatives I found so far.

Related

xamarin android alignParentRight style

can you tell me how can I add alignParentRight into my xml style?
<style name="My_Style">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">75dp</item>
<item name="android:background">#color/Gray_75</item>
**<item name="android:alignParentRight">true</item>**
</style>
yes but I need to add it programatically for dynamic and I will add only style and thats all.
Generally you can't do that programmatically. What we can do is:
Create two different styles, one with <item name="android:alignParentRight">true</item> and the other one not, apply them to the control when it is needed. Since in which scenario of your styles will be used is not clear in your question, maybe you can look into State List.
I personally think it is more straight if we directly set the Layout Parameters in code behind, for example:
var parameters = btn.LayoutParameters as RelativeLayout.LayoutParams;
parameters.AddRule(LayoutRules.AlignParentRight);
The btn in this code refer to a Button control.

Change font/typeface when button is pressed

I already how to change the style of an element with selector but I found nothing about the typeface...
Is it possible to do with ? Or is there another way?
Sadly, right now it is not possible.
There is a ticket for that in the Android code repo :
https://code.google.com/p/android/issues/detail?id=8941
Your best option is to manage it yourself in ontouch listeners (and yes this is ugly) or implement these new selectors yourself.
Typeface can easily be an element of your style... If you're using default android styles, then the idea would be to extend whatever style you're implementing and just change the elements you need. like the following style element, taken from the android styles and themes documentation:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Then apply this style in your selector, just like you already know how to do.
The other option is, of course, to do it in code, but the selector is much cleaner
When button is clicked,Use below code to change font of the text(custom typeface).
Put your font under main->assets->fonts directory.
//change font when button pressed
val typeFace = Typeface.createFromAsset(activity!!.assets,"fonts/sf_semi_bold.ttf")
buttonAbout.setTypeface(typeFace)
TextView.setTypeFace() Use Ctrl+Space to show a giant list of classes for TextView or anything in general on.

What style attribute has to be changed for spinner dropdownitem?

I would like to change the color when a item of the spinner drop down is clicked.
I searched all the way through themes.xml, styles.xml and attr.xml but yet I can find which attribute has to be set to achieve this.
In attr.xml there is
<attr name="spinnerDropDownItemStyle" format="reference" />
In themes.xml in default themse there is
<style name="Theme">
...
<item name="spinnerDropDownItemStyle">#android:style/Widget.DropDownItem.Spinner</item>
<item name="spinnerItemStyle">#android:style/Widget.TextView.SpinnerItem</item>
...
When I follow this in style.xml I cant find any entry where the background drawable (with selectors for pressed and focused) is assigned (I guess there must be somewhere a refference to a xml file with selectors inside, like for button).
In short: I cant find what has to be set how in order to change the click and focus appearance of spinner dropdown items.
The Background alone can be changed by
<style name="CustomSpinnerAppearance0" parent="android:style/Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/spinner_default0</item>
<item name="android:dropDownSelector">#ffc0c0c0</item>
<item name="android:popupBackground">#ffc0c0c0</item>
</style>
But even when I set a selector or transition as popupBackground nothing changes in the click behaviour.
Any ideas?
Thanks.
Meanwhile I found the solution.
To close this question:
I still dont know, why it has no influence on the behaviour when I set the dropDownSelector.
But changing the attribute
<item name="android:listChoiceBackgroundIndicator"></item>
does the trick.

How to change style from code? Want to achieve skins

How to change a style from code?
I got a style used all across my app, for all buttons. If the user changes the skin of the app, the background of this style should change.
<style name="ActionBtn">
<item name="android:layout_width">#dimen/action_btn_width</item>
<item name="android:layout_height">#dimen/action_btn_height</item>
<item name="android:background">#drawable/btn_frame_bgstate</item>
<item name="android:padding">#dimen/action_btn_padding</item>
<item name="android:layout_margin">#dimen/action_btn_margin</item>
</style>
So far the only idea I got is to make a custom button that itself chooses its background on creation.
I have not found any good, generic way for skinning android apps yet, but if I could change styles from code, that would do the trick.
All suggestions welcome!
1) Create different themes for your skins.
2) Set those themes programatically using following code in your onCreate method.
setTheme(resid);
resid is the id of your theme.

android : Textview Set Style in runtime [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
android : set textView style at runtime
i just wanna know how i can implement style in runtime i saw some posts here but i couldnt find anything properly
i create a new Textview , like this
TextView title = TextView(this, null, R.style.TitleSep);
this its my style xml.
<style name="TitleSep">
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:lineSpacingMultiplier">1.1</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#ffff0000</item>
<item name="android:padding">2dip</item>
</style>
Nothing change at all, i saw the api and should be change the style instead i got the default style.
Any advice.
Cheers
Ron
You can define the TextView in separated layout file (like Rajath DSouza suggested). And then load the view from layout file (inflate it) dynamically.
For example:
TextView title = (TextView) activity.getViewInflate().inflate(R.layout.styled_textview, null, null);
If you can use the layout file, do the following - it's simpler.
<TextView
style="#style/TitleSep"
android:text="hello" />
UPDATE: (from comment)
Why don't create the textview at compile time, but make it invisible, and set it to visible if and when you need to show it. The reason I suggest this roundabout method is because setting the style at compile time seems simpler.

Categories

Resources