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.
Related
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.
This question already has answers here:
Set style for TextView programmatically
(13 answers)
Closed 3 years ago.
Here is my style:
<style name="buttonQuestionStyle" parent="#style/Widget.AppCompat.Button.Colored">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">25dp</item>
<item name="android:layout_margin">10dp</item>
<item name="android:background">#color/questionButton</item>
</style>
And here my code:
Button btn = new Button(getActivity());
btn.setText(ojb.getText());
if (Build.VERSION.SDK_INT < 23) {
btn.setTextAppearance(getActivity(), R.style.buttonQuestionStyle);
} else {
btn.setTextAppearance(R.style.buttonQuestionStyle);
}
In the app:
Programmatically button appears like this:
And via layout it worked. Appears like this:
Here is my code in the XML Layout:
<Button
android:text="Question"
style="#style/buttonQuestionStyle" />
So... I dont know why it happens, and how fix it.
You can pass a ContextThemeWrapper in constructor for button and use 3 arguments constructor for Button(context, attributeset, defStyle).
ContextThemeWrapper wrapper = new ContextThemeWrapper(this,R.style.buttonQuestionStyle);
Button btn = new Button(wrapper, null, 0); // note this constructor
btn.setText("some text");
Some info around why you cannot set button's style programmatically, as per the JavaDoc of method setTextAppearance
Sets the text appearance from the specified style resource.
<p>
Use a framework-defined {#code TextAppearance} style like
{#link android.R.style#TextAppearance_Material_Body1 #android:style/TextAppearance.Material.Body1}
or see {#link android.R.styleable#TextAppearance TextAppearance} for the
set of attributes that can be used in a custom style.
#param resId the resource identifier of the style to apply
#attr ref android.R.styleable#TextView_textAppearance
So it deals with only text appearance not other style elements.
Still if you want to apply some style at runtime programmatically you need to
make each and every change separately for example to set background you need to call setBackground and similarly for other cases.
or
Inflate that view programmatically using that particular theme.
I have a quick question!
In my styles.xml file, I have
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:padding">20px</item>
<item name="android:background">#9cd0e8</item>
<item name="android:textColor">#254b7c</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
And in my activity_main.xml, I have
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="#+styles/TextViewStyle"
android:text="Sample Text"/>
What I am trying to do is, in my Android application, on a certain activity I plan to place many TextViews with similar properties. Instead of writing these 'properties' every time with each TextView instance, I grouped them together in a style in styles.xml file and set theme of each of my TextViews to that style.
It works fine and does what I want it to do, but only with APIs above 21! My application's supposed to support devices from API level 15 up. Why is my approach not working with lower APIs?
Please help soon. I need to finish this soon.
EDIT
By 'working', I meant that the attributes I set in my style (padding, color, etc.) appear on the TextViews as they should. In lower APIs however, the TextViews appear as if I had not applied any attribute on them. Plain text appears instead of a styled one.
remove parent from your style
remove android:theme from textView, (why there is + sign?)
instead of theme put this into your textView
style="#style/TextViewStyle"
btw, use dp instead of px ;)
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.
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.