Reputation: 4898
I have the following activity which specifies the theme as an attribute
[Activity(Label = "PermissionsActivity", Theme = "@android:style/Theme.Translucent.NoTitleBar")]
public class PermissionsActivity: Activity
This works well, but how can I apply the same at runtime? Maybe by calling SetTheme
in OnCreate
. I can see SetTheme accepts a resource id integer. I'm having a hard time finding the corresponding Xamarin.Android constant for the aforementioned theme. Please help
Upvotes: 0
Views: 1136
Reputation: 16519
When you add some theme in any folder under the resources of an android project what Visual studio does is it creates a corresponding int value inside the ResourceDesigner.cs file under the resources folder.
Now in Runtime when you need to add these to your code they are available as follows:
Resource.Style.YourResourceName
Resource.Dimen.YourResourceName
Resource.String.YourResourceName
Resource.Drawable.YourResourceName
Resource.Mipmap.YourResourceName
, And so on and so forth.Note: These properties are always an integer.
In your case since it is a theme(which is basically a style)
Hence you can get it like this in an Activity:
this.SetTheme(Resource.Style.MyTheme);
And in a Fragment something like this :
this.Activity.SetTheme(Resource.Style.MyTheme);
Hope this helps,
Revert in case of queries.
Upvotes: 0
Reputation:
Add this code in your onCreate Method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
Upvotes: 0
Reputation: 17412
Add your theme in style.xml
file under Resource folder than access it from resource as int
<style name="MyTheme" parent="Theme.Translucent.NoTitleBar">
</style>
Setting in activity
this.SetTheme(Resource.Style.MyTheme);
Upvotes: 1