Search code examples
androidvisual-studioxamarinandroid-activityandroid-manifest

VS - Android - How to apply a style to main activity


Just need to clarify what should I name, where and how exactly in order to apply a custom style to the main activity.

I want to apply a custom style "Theme.Transparent" to the main activity from the CustomStyles.xml file. To do it you should add the following section to the AndroidManifest file:

<activity android:name="???" android:theme="@style/Theme.Transparent"/> // see main question about this

I have no idea what name should be there and how it corresponds to other names in the app (see my main question about this). I found a few places where you specify activity name. I marked these places in the code as 1, 2, 3, 4 (see other questions about each number)

In MainActivity.cs:

namespace CustomName // 1
{
    [Activity(Name = "???" Label = "CustomName", MainLauncher = true, Icon = "@drawable/icon")] // 2
    public class MainActivity : Activity // 3
    {

In AndroidManifest.xml:

<application android:label="CustomName"> // 4
    <activity android:name="ActivityName"> // 5
    </activity>
</application>

Now questions

Main question: Should I add activity section in AndroidManifest.xml for the main activity to apply a custom style to it? What name should main activity have there? If I add the activity section there and give it a name, where else should I use this name to make this style work with this activity? It's mess with names

Code 1) Application namespace name should be the application name literally?

Code 2) [Activity] Name and Label tags, what are they for and how should I name it?

Code 3) public class MainActivity, does this name matter for styling?

Code 4) what is this for?

Code 5) see main question

How is it all connected?

EDITS for Joe LV's answer

AndroidManifest.xml:

<application android:label="Custom Application Name"> // this name does not appear in the phone anyway
</application>

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:style/Theme.Translucent">
    ...
  </style>
</resources>

MainActivity.cs:

namespace CustomName
{
    [Activity(Name = "CustomName.MainActivity", Label = "Custom Activity Name", MainLauncher = true, Icon = "@drawable/icon", Theme = "@styles/Theme.Transparent")] // the phone application receives name from this label

Q1) Error in AndroidManifest.xml "No resource found that matches the given name (at 'theme' with value '@styles/Theme.Transparent')" Answer: remove s from "styles" in "@styles/Theme.Transparent"

Q2) The application in the phone receives the name from main activity label, not from the application label. What is the application label for and why the app itself gets the name of activity?


Solution

  • Code 1) The application name can be changed by changing the label of application in your AndroidManifest.xml, like this: <application android:label="MyName">, so you app's name has changed to "MyName" which you can see in your phone screen and your application namespace still is CustomName.

    Code 2) [Activity] Name and Label tags, both of them can be changed by yourself. The Name is the current Activity's name which can be used to start this activity explicitly, like @Jon has said, it should match your class name, and the Label is the current Activity's title which you can see it at the top of your activity.

    Code 3) The MainActivity's name does no matter to the style. You can custom it in your styles.xml file.

    Code 4) Please refer to Code 1)

    Code 5) Main question. Like @Jon has said, "There is no need to add your own section to AndroidManifest.xml if you are using the [Activity] attribute as it will generate the entry at build time."

    Please put your style into styles.xml file not CustomStyles.xml file, and refer to it by Theme="@style/Theme.Transparent". The activity name should be like this: [Activity Name=package.TypeName], so in your code it should be [Activity Name=CustomName.MainActivity]. Finally, your Activity attribute can be this:

    [Activity(Label = "YourName", MainLauncher = true,Name = "CustomName.MainActivity",Theme ="@style/Theme.Transparent")]

    Update:

    Q2)

    • You app consists of many activities, every Activity has it's label and the label can be seen when this activity is OnResume. This will tell user where they are when they are using your app, so user won't be lost in your app.
    • The Application's label is your app's name, like "Facebook", "Google", etc.