How to programmatically set style attribute in a view
Generally you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.
There is also such a thing as a StateListDrawable
which lets you define different drawables for each state the your Button
can be in, whether focused, selected, pressed, disabled and so on.
For example, to get your button to change colour when it's pressed, you could define an XML file called res/drawable/my_button.xml
directory like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
You can then apply this selector to a Button
by setting the property android:background="@drawable/my_button"
.
how to set the Style Attribute Programmatically in Android?
Dynamic style change is not currently supported. You must set the style before the view is create (in xml).
Android: set view style programmatically
Technically you can apply styles programmatically, with custom views anyway:
private MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context, null, R.style.LightStyle);
}
}
The one argument constructor is the one used when you instantiate views programmatically.
So chain this constructor to the super that takes a style parameter.
RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));
Or as @Dori pointed out simply:
RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));
Now in Kotlin:
class MyRelativeLayout @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = R.style.LightStyle,
) : RelativeLayout(context, attributeSet, defStyleAttr)
or
val rl = RelativeLayout(ContextThemeWrapper(activity, R.style.LightStyle))
Programmatically change one attribute on custom style
What about group these TextView in only one Layout? Then change it programmatically.
In my example I group all of TextViews in only one LinearLayout.
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
int childCount = ll.getChildCount();
for (int i=0; i<childCount; i++){
((TextView)ll.getChildAt(i)).setTextSize(20);
}
Be sure that you only have TextViews in your layout.
Add Style after creating a View programmatically
You can't apply a style after constructing a view. The correct way to do this is to use the 4-argument constructor on Android 5.0+ or to create a theme attribute that references your style and use the 3-argument constructor.
// Works on versions prior to Android 5.0
RelativeLayout rl = new RelativeLayout(this, null, R.attr.myRelativeLayoutStyle);
// Works on Android 5.0 and above
RelativeLayout r2 = new RelativeLayout(this, null, 0, R.style.MyRelativeLayout);
res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myRelativeLayoutStyle" format="reference" />
...
res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyRelativeLayout">
...
</style>
...
res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="...">
<item name="myRelativeLayoutStyle">@style/MyRelativeLayout</item>
...
Android - How to programmatically set the button style in a Linearlayout?
Hope I'm not too late to join the party :)
Well, Styles can be applied to a View
at initialization phase. For example:
LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
How can I set a textview's style programmatically?
You may find this answer by Benjamin Piette handy. To change this into working code for a TextView just change it up a bit:
TextView tv = new TextView (new ContextThemeWrapper(this, R.style.mystyle), null, 0);
EDIT: to set other things like margins, height & width and others, use LayoutParams. To set the params throrin19's answer can be helpful.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
tv.setLayoutParams(params);
Related Topics
Android Respond to Url in Intent
Android Studio 3.5 Refactor Issue
How to Load Script from Assets Index.Android.Bundle on Windows
Resize a Large Bitmap File to Scaled Output File on Android
Open Facebook Page from Android App
Download Binary File from Okhttp
Android - Listen For Incoming Sms Messages
Disable Back Button in Android
How to Capture the "Virtual Keyboard Show/Hide" Event in Android
Set Color of Textview Span in Android
How to Programmatically Set Style Attribute in a View
How to Create a Circular Imageview in Android
Capture Image from Camera and Display in Activity
How to Open a Url in Android'S Web Browser from My Application