I have three buttons to change themes. On clicking each button my app theme must change dynamically. How to do it programmatically.
To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Here is a nice tutorial on how dynamically apply themes.
and this one too.
Please Visit this link for it.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
By defining an xml theme file:
<?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>
You can also apply styles to all activities of an application:
<application android:theme="#style/CustomTheme">
Or just one activity:
<activity android:theme="#android:style/Theme.Dialog">
Sorry but you can't change styles programmatically.
how to set the Style Attribute Programmatically in Android?
There are certainly other methods to achieve this desired behavior however. You could set onclick listeners for each button, and programmatically change text size, color, background etc of your various view elements
you can use a particular theme for a given xml file.
in graphical layout you can CHANGE the theme of the layout using editing config.
use onclick event to go to next layout and here your theme will be different from the first one.
Vimalatha, to change your background when you click a button just add this code to the onClick function of your button.
myLinearLayout.setBackgroundColor(Color.BLUE);
Assuming that myLinearLayout is your LinearLayout name...
Related
I can able to set theme to button using android:theme attribute in xml. Is there any equivalent programmatical code?
<Button
android:id="#+id/btnPayment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Add Payment"
android:theme="#style/ButtonPeter" // theme..
/>
style.xml
<style name="ButtonPeter" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/fbutton_color_peter_river</item>
<item name="android:textColor">#android:color/white</item>
</style>
Can I able to set theme dynamically using something like this btnPayment.setTheme(R.style.ButtonPeter) ?
I search a lot but all posts are related to set style or create dynamic button and apply style to it but I dont want to do that .I want to set theme to button
How to achieve this?
I think you are confused with style and theme. From this document, it says: theme for the whole activity while style is for view.
A style is a collection of properties that specify the look and format for a View or window.
A theme is a style applied to an entire Activity or application, rather than an individual View.
For this code:
android:theme="#style/ButtonPeter" // theme is used but only valid style attributes for button will be applied
Hope this help.
As I mention here, using TextViewCompat.setTextAppearance should work :-)
So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.
The buttons are being distorted after changing minSdkVersion in the manifest from 10 to 11. If I change it back to 10 the buttons are fine. These buttons all have respective drawables in each of the drawable folders. This is happening to buttons without drawables as well. Buttons that just have text the text gets enlarged quite a bit.
Edit to add xml example:
Here is what the button in xml looks like.
<Button
android:id="#+id/settingsEditInfoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edit_info_btn" />
The text is part of the image. Here is the edit info button drawable.
The minHeight and minWidth of buttons are being set by the default theme somewhere. Setting them both to 1dp solved the problem in this case.
<Button
android:id="#+id/settingsEditInfoBtn"
android:minHeight="1dp"
android:minWidth="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edit_info_btn" />
Or you can set a button style in your application theme that overrides these values for all buttons. In the values/themes.xml:
<style name="MyTheme" parent="android:Theme">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="android:style/Widget.Button">
<item name="android:minHeight">1dp</item>
<item name="android:minWidth">1dp</item>
</style>
Then, don't forget to apply the custom theme to any desired activities in the AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:theme="#style/MyTheme"
</activity>
This is normal. The default font/size/style varies between the two API levels and the text is rendered in the default font corresponding to the API level. By definition the app inherits the default Holographic theme when you set min/Targets Sdk Version to 11 or greater.
If you want to have the size of your choice you should explicitly define your style and use it in your layout.
<style name="CustomTheme">
<item name="textSize"> 14dp </item>
</style>
and use it in your button.
<Button style="#style/CustomTheme"
android:layout_width=".."
../>
I have spinner with a style selection (I set the spinner style in strings.xml) and it's works well if I set the style in main.xml. But I want to know how to set the style programmatically in which the style already defined in strings.xml.
Refer my code below
main.xml:
<Spinner
android:id="#+id/PassengersSpinner1"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_span="3"
android:layout_marginTop="6dp"
style="#style/SpinnerStyle" />
strings.xml:
<style
name="SpinnerStyle"
parent="#android:style/Widget.Spinner">
<item name="android:background">#android:drawable/btn_default</item>
</style>
Now, I want to know that how can I set this strings.xml programmatically without main.xml?
From all my reading, you cannot do it, because the style has to be set before the view is created.
You can do it if you create the view in code (since then you can set the style before creating the view).
If you want to do that, a good example is here. Both the question and the answer give you methods to achieve your goal.
I need button that has a replicated background pattern and normal button text on top - how to specify this in layout XML?
Override android:background on the Button in question. Or, for reuse you could declare your own style, overriding android:background from the base button style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyButtonStyle" parent="Widget.Button">
<item name="android:background">#drawable/fancy_button</item>
</style>
</resources>
In either case, your fancy_button drawable would be an image, or more likely a StateListDrawable.
For the Button instances you wish to apply this to, you'd do:
<Button style="#style/MyButtonStyle" />.
Consider using ImageButton instead of using regular one.