Skip to content Skip to sidebar Skip to footer

In Android, Can I Have Different Activities Use The Same Xml File?

I'm creating an android app that will have multiple pages with the same layout, but the only thing that changes will be a string that is displayed on the top (using setText). Can I

Solution 1:

Yes, you can. Just call "setContentView" with the same xml and it will use the same layout. Change whatever you need to programmatically during runtime.

Solution 2:

Use can use the same xml file for different fragments, activities and Presentations https://developer.android.com/reference/android/app/Presentation It is good for unknown number of displays. You can change the fields programmatically and I have tested the same on Android 10.

Test app: It uses single XML for both activities and single XML for both fragments and changes their text programatically.

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.5.20"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app build.gradle

plugins {
    id'com.android.application'id'kotlin-android'id'kotlin-android-extensions'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.example.testingsinglexml"
        minSdkVersion 29
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


    def fragment_version = "1.3.6"
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.testingsinglexml"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.TestingSingleXML"><activityandroid:name=".MainActivity2"></activity><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

MainActivity.kt

package com.example.testingsinglexml

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.add
import androidx.fragment.app.commit


classMainActivity : AppCompatActivity() {
    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<TextView>(R.id.text_to_enter).text = "First Activity"

        findViewById<Button>(R.id.button).setOnClickListener {
            val fp = Intent(applicationContext, MainActivity2::class.java)
            startActivity(fp)
        }

        supportFragmentManager.commit {
            setReorderingAllowed(true)
            add<BlankFragment1>(R.id.fragment_container_view1)
            add<BlankFragment2>(R.id.fragment_container_view2)
        }
    }
}

MainActivity2.kt

package com.example.testingsinglexml

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

classMainActivity2 : AppCompatActivity() {
    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<TextView>(R.id.text_to_enter).text = "Second Activity"
    }
}

BlankFragment1.kt

package com.example.testingsinglexml

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_blank.*

// TODO: Rename parameter arguments, choose names that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERprivateconstval ARG_PARAM1 = "param1"privateconstval ARG_PARAM2 = "param2"/**
 * A simple [Fragment] subclass.
 * Use the [BlankFragment1.newInstance] factory method to
 * create an instance of this fragment.
 */classBlankFragment1 : Fragment() {
    // TODO: Rename and change types of parametersprivatevar param1: String? = nullprivatevar param2: String? = nulloverridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    overridefunonResume() {
        super.onResume()
        SherajText.text = "SHERAJ1"
    }

    overridefunonCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_blank, container, false)
    }

    companionobject {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment BlankFragment.
         */// TODO: Rename and change types and number of parameters@JvmStaticfunnewInstance(param1: String, param2: String) =
            BlankFragment1().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

BlankFragment2.kt

package com.example.testingsinglexml

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_blank.*

// TODO: Rename parameter arguments, choose names that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERprivateconstval ARG_PARAM1 = "param1"privateconstval ARG_PARAM2 = "param2"/**
 * A simple [Fragment] subclass.
 * Use the [BlankFragment2.newInstance] factory method to
 * create an instance of this fragment.
 */classBlankFragment2 : Fragment() {
    // TODO: Rename and change types of parametersprivatevar param1: String? = nullprivatevar param2: String? = nulloverridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    overridefunonResume() {
        super.onResume()
        SherajText.text = "SHERAJ2"
    }

    overridefunonCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_blank, container, false)
    }

    companionobject {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment BlankFragment2.
         */// TODO: Rename and change types and number of parameters@JvmStaticfunnewInstance(param1: String, param2: String) =
            BlankFragment2().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/text_to_enter"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Switch"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/text_to_enter" /><!-- res/layout/example_activity.xml --><androidx.fragment.app.FragmentContainerViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/fragment_container_view1"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /><!-- res/layout/example_activity.xml --><androidx.fragment.app.FragmentContainerViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/fragment_container_view2"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/fragment_container_view1" /></androidx.constraintlayout.widget.ConstraintLayout>

fragment_blank.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".BlankFragment1"><TextViewandroid:id="@+id/SherajText"android:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/hello_blank_fragment1" /></FrameLayout>

enter image description hereenter image description here

Post a Comment for "In Android, Can I Have Different Activities Use The Same Xml File?"