Kush
Kush

Reputation: 175

Android Studio has 2 layout activity files for the same activity. Is it necessary? How do I keep 1 file for all newer SDK verions?

In my project, I have an activity called "marquee". For some reason however, there are 2 files associated with that activity inside a subfolder in "layout". I don't remember making it. Why is it here? I've been wasting time changing code in "activity_marquee.xml", but nothing was changing because my phone would only use "activity_marquee.xml (v26)" that was created somehow. How do I make sure all phones use the same "activity_marquee.xml"? And how would I remove the unnecessary one? If I delete either one, both of them, and even the folder that contains the 2 activities gets removed.

I've tried changing my minSdkVersion and targetSdkVersion (in build.gradle) around to different values hoping that only 1 activity would remain, but the (v26) still stays. I've also tried adding to the AndroidManifest.xml and changing the min, target, and max sdk versions, but both files remain.

https://i.sstatic.net/Jd3HY.png (link to the image because I don't have 10 reputations to post it yet)

build.gradle:

android {
compileSdkVersion 28
defaultConfig {
    applicationId "(removed)"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".MarqueeActivity">
    </activity>
</application>

I'd like to use only 1 layout file for each activity and have it work on all higher level android APIs. I made another app just 2 weeks ago based on a tutorial, and I didn't have to change layouts in 2 files. I'd like the same here if possible.

Upvotes: 7

Views: 4079

Answers (1)

Alexander Hoffmann
Alexander Hoffmann

Reputation: 6039

You need to understand the concept of resource qualifiers. You can provide different resources for different device settings. This is most often used for different image resolutions or translation, for example by having a "values" and a "values-de" (de for German) folder which both include a strings.xml file. You app will always choose the most specific resource that matches to your phones configuration. Say if I have my phone set to German, the app will look for the resource in the values-de folder. Otherwise it will fall back to the default values folder.

If both your layout files are identical, you can simply delete it from your layout-v26 folder. If the folder doesn't contain any other files, you can delete it completely.

If I delete either one, both of them, and even the folder that contains the 2 activities gets removed.

Actually, both files are in different folders. But Android Studio shows them as if they were in the same one if you have the default "Android" view selected in your Project explorer. If you switch the view to "Project", you'll see that they are in different folders "res/layout" and "res/layout-v26".

enter image description here

Upvotes: 7

Related Questions