Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.imgur.com/qrPoPHv.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.

like image 332
Kush Avatar asked Jan 04 '19 14:01

Kush


People also ask

What is an activity layout in Android?

Layouts Part of Android Jetpack. A layout defines the structure for a user interface in your app, such as in an activity . All elements in the layout are built using a hierarchy of View and ViewGroup objects.

How do I create an activity in Android Studio?

Position your cursor in your project tree and hover over the directory which contains the MainActivity class we created in the previous chapter. Right-click on it and select New > Activity > Empty Activity. Create an Activity file. Android Studio displays a new window. This window allows you to configure the Activity's name and its layout file.

What files should I look for when learning Android Studio?

Knowing how to find and open up files like this is crucial to understanding Android Studio. The second important part of the app is the activity_main.xml file. This is the layout file, meaning it will handle the design and the appearance of your app. It’s where we’ll add buttons for instance.

What is the use of Android Studio?

It basically acts like a file explorer to show you all of the files involved in your project. If you were to select another activity, a class or a layout file, then it would open up in the big window on the right. Finally, down at the bottom you will have another window where you can see messages, a terminal, an Android Monitor and more.


1 Answers

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

like image 75
Alexander Hoffmann Avatar answered Sep 27 '22 18:09

Alexander Hoffmann