Kristiyan Varbanov
Kristiyan Varbanov

Reputation: 2509

How to use more then one xml file in Activity Kotlin

Is there any chance to initialize more then one .xml file and use there elements when i want?

Here is my android code:

   mStepOneView                    = getLayoutInflater().inflate(R.layout.activity_exclusion_length, null);
   mStepTwoView                    = getLayoutInflater().inflate(R.layout.activity_gambling_sites, null);
   mStepThreeView                  = getLayoutInflater().inflate(R.layout.activity_info_sites, null);
   mStepFourView                   = getLayoutInflater().inflate(R.layout.activity_custom_websites, null);
   mStepFiveView                   = getLayoutInflater().inflate(R.layout.activity_activate_self_exclusion, null);

I have a container in which I am replace different step - every step is different .xml layout and i got every element from from these xml files. I want to do this in Kotlin, the problem is that if mStepOneView is inflated i can't get element value from mStepTwoView.

Upvotes: 0

Views: 1504

Answers (2)

Nitish
Nitish

Reputation: 1043

Actually, you are using the wrong approach either you should use fragments (Maybe child if your parent is already a fragment) or you should declare an empty layout inside your main layout and only add your other XML like layout dynamically. you can use below code.

// get your inner relative layout child
RelativeLayout rl = (RelativeLayout) findById(R.id.rl);


// inflate content layout(Other XML file) and add it to the relative 
// layout as a child and update it with different layout (XML) files conditionally 

LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); 

Upvotes: 1

Khaled Qasem
Khaled Qasem

Reputation: 929

I think you can handle it by adding "include" to the main xml keyword and control the visibility of each xml layout. So you will need to inflate an xml layout that contains all the layouts and control which xml layout to show by getting the layout with id and set visibility gone or visible and so on.

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include 
          android:id="@+id/activity_exclusion_length"
          layout="@layout/activity_exclusion_length" />
       <include layout="@layout/activity_gambling_sites" />
       <include layout="@layout/activity_info_sites" />

 <LinearLayout/>

Upvotes: 0

Related Questions