Android manifest file_Lesson 3
Android manifest file_Lesson 3
This is the required xml file for all the android application and located inside the root
directory:
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Maina2"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EXTERNALIZING RESOURCES
There are many more items which you use to build a good Android application. Apart from
coding for the application, you take care of various other resources like static content that
your code uses, such as bitmaps, colors, layout definitions, user interface strings, animation
instructions, and more. These resources are always maintained separately in various sub-
directories under res/ directory of the project.
• Create a new directory in res/ named in the form -. Here resources_name will be any
of the resources mentioned in the above table, like layout, drawable etc. The qualifier
will specify an individual configuration for which these resources are to be used.
• Save the respective alternative resources in this new directory. The resource files must
be named exactly the same as the default resource files as shown in the below
example, but these files will have content specific to the alternative. For example
though image file name will be same but for high resolution screen, its resolution will
be high.
Below is an example which specifies images for a default screen and alternative images for
high resolution screen.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
values/
strings.xml
Below is another example which specifies layout for a default language and alternative layout
for Arabic language.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
layout-ar/
main.xml
values/
strings.xml
Accessing Resources
During your application development you will need to access defined resources either in your
code, or in your layout XML files. Following section explains how to access your resources
in both the scenarios: -
When your Android application is compiled, a R class gets generated, which contains
resource IDs for all the resources available in your res/ directory. You can use R class to
access that resource using subdirectory and resource name or directly resource ID.
Example 1:
To access res/drawable/myimage.png and set an ImageView you will use following code: -
Example 2:
<resources>
<string name="hello">Hello, World!</string>
</resources>
Now you can set the text on a TextView object with ID msg using a resource ID as follows:-
Example 3:
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
This application code will load this layout for an Activity, in the onCreate() method as
follows:-
Consider the following resource XML res/values/strings.xml file that includes a color
resource and a string resource: -