Join two different XML Layouts in one dinamically


Last Updated:

  1. joao.sauer

    joao.sauer Member This Topic's Starter

    Joined:
    Aug 11, 2010
    Messages:
    8
    Likes Received:
    0
    Hi guys,

    Sorry if this is really easy and someone already answered it, but the problem is that I have no idea how to search for it.

    Let's say that my Layout is huge and split it in 2-3 different layouts.
    So, I have 1.xml, 2.xml and 3.xml.

    The 1.xml have a Scrollview that contains 1 linearLayout.
    The 2.xml have just 1 LinearLayout with a lot of items inside of it.
    The 3.xml have just 1 LinearLayout with a lot of items inside of it.

    So, in my onCreate, I need do the:
    setContentView(R.layout.1);

    Now, let's say that I have a flag that if is true I will need to use 2.xml and false I will use the 3.xml.
    My question is:
    How can I get a pointer to 2.xml or 3.xml and join it to the LinearLayout inside of the 1.xml?

    So, in the end I will have one the screen with 1 ScrollView that contains 1 LinearLayout that will contain 1 more LinearLayout that could be from 2.xml or 3.xml.

    It's simple I know, but I have no idea in how to find about it, really sorry about my question.

    Thanks a lot in advanced to answer this simple question.

    Joao
     

    Advertisement
  2. droidotheplains

    droidotheplains Member

    Joined:
    Nov 11, 2010
    Messages:
    15
    Likes Received:
    1
    joao.sauer likes this.
  3. joao.sauer

    joao.sauer Member This Topic's Starter

    Joined:
    Aug 11, 2010
    Messages:
    8
    Likes Received:
    0
    Hi droidotheplains,

    Thanks for your reply!
    But what I'm looking is to merge two separate xml files in the same screen. It's appears that in the example, we are creating the buttons dinamically and adding it to the parentLayout.
    What I need is:
    Find a way to read the xml file and keep it in a variable. For example, the only way that I know to use a xml file is to do:
    setContentView(R.layout.XML_FILE);
    Then, using the ID I can have a pointer to any component of the file and do everything that I want.
    But, How can I have access to the XML without showing it(without using setContentView)?

    Thanks a lot for your help!
    Joao
     
  4. joao.sauer

    joao.sauer Member This Topic's Starter

    Joined:
    Aug 11, 2010
    Messages:
    8
    Likes Received:
    0
    Hello everybody,

    I found the solution

    LayoutInflater is the class that do it!

    example:
    you have main.xml and child.xml

    Code (Text):
    1. setContentView(R.layout.main);
    2. LinearLayout main= (LinearLayout) findViewById(R.id.linearlayout1);
    3.  LayoutInflater li = getLayoutInflater();
    4. ScrollView tmp = (ScrollView) li.inflate(R.layout.child,null);
    5. main.addView(tmp);
    That's it.

    BR,
    Joao
     
  5. droidotheplains

    droidotheplains Member

    Joined:
    Nov 11, 2010
    Messages:
    15
    Likes Received:
    1
    You can still use a similarly configured FrameLayout subclass as the second child of your outer LinearLayout, thus:

    main.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7.  
    8.   <LinearLayout android:id="@+id/LinearLayout01" android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
    9.     <LinearLayout android:id="@+id/LinearLayout02" android:orientation="vertical" android:layout_weight="1" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content">
    10.       <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Frame 2" android:layout_gravity="center"></Button>
    11.     </LinearLayout>
    12.     <org.test.frame.PanelSwitcher android:id="@+id/FrameLayout01" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    13.       <LinearLayout android:id="@+id/LinearLayout03" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">
    14.         <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frame 1" android:id="@+id/Frame1Button"></Button>
    15.       </LinearLayout>
    16.       <LinearLayout android:id="@+id/LinearLayout04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
    17.         <Button android:id="@+id/Frame2Button" android:text="Frame 2" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_width="fill_parent" android:layout_weight="1"></Button>
    18.       </LinearLayout>
    19.     </org.test.frame.PanelSwitcher>
    20.   </LinearLayout>
    21. </LinearLayout>
    I've merely changed the package name in the file I referenced above:

    PanelSwitcher.java:
    Code (Text):
    1.  
    2. /*
    3.  * Copyright (C) 2008 The Android Open Source Project
    4.  *
    5.  * Licensed under the Apache License, Version 2.0 (the "License");
    6.  * you may not use this file except in compliance with the License.
    7.  * You may obtain a copy of the License at
    8.  *
    9.  *      http://www.apache.org/licenses/LICENSE-2.0
    10.  *
    11.  * Unless required by applicable law or agreed to in writing, software
    12.  * distributed under the License is distributed on an "AS IS" BASIS,
    13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14.  * See the License for the specific language governing permissions and
    15.  * limitations under the License.
    16.  */
    17.  
    18. package org.test.frame;
    19.  
    20. import android.view.animation.TranslateAnimation;
    21. import android.view.MotionEvent;
    22. import android.view.View;
    23. import android.view.GestureDetector;
    24. import android.widget.FrameLayout;
    25. import android.content.Context;
    26. import android.util.AttributeSet;
    27. import android.os.Handler;
    28.  
    29. import java.util.Map;
    30.  
    31. class PanelSwitcher extends FrameLayout {
    32.     private static final int MAJOR_MOVE = 60;
    33.     private static final int ANIM_DURATION = 400;
    34.  
    35.     private GestureDetector mGestureDetector;
    36.     private int mCurrentView;
    37.     private View mChild, mHistoryView;
    38.     private View children[];
    39.  
    40.     private int mWidth;
    41.     private TranslateAnimation inLeft;
    42.     private TranslateAnimation outLeft;
    43.  
    44.     private TranslateAnimation inRight;
    45.     private TranslateAnimation outRight;
    46.  
    47.     private static final int NONE  = 1;
    48.     private static final int LEFT  = 2;
    49.     private static final int RIGHT = 3;
    50.     private int mPreviousMove;
    51.  
    52.     public PanelSwitcher(Context context, AttributeSet attrs) {
    53.         super(context, attrs);
    54.         mCurrentView = 0;
    55.         mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
    56.                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    57.                                        float velocityY) {
    58.                     int dx = (int) (e2.getX() - e1.getX());
    59.  
    60.                     // don't accept the fling if it's too short
    61.                     // as it may conflict with a button push
    62.                     if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
    63.                         if (velocityX > 0) {
    64.                             moveRight();
    65.                         } else {
    66.                             moveLeft();
    67.                         }
    68.                         return true;
    69.                     } else {
    70.                         return false;
    71.                     }
    72.                 }
    73.             });
    74.     }
    75.  
    76.     @Override
    77.     public void onSizeChanged(int w, int h, int oldW, int oldH) {
    78.         mWidth = w;
    79.         inLeft   = new TranslateAnimation(mWidth, 0, 0, 0);
    80.         outLeft  = new TranslateAnimation(0, -mWidth, 0, 0);        
    81.         inRight  = new TranslateAnimation(-mWidth, 0, 0, 0);
    82.         outRight = new TranslateAnimation(0, mWidth, 0, 0);
    83.  
    84.         inLeft.setDuration(ANIM_DURATION);
    85.         outLeft.setDuration(ANIM_DURATION);
    86.         inRight.setDuration(ANIM_DURATION);
    87.         outRight.setDuration(ANIM_DURATION);
    88.     }
    89.  
    90.     protected void onFinishInflate() {
    91.         int count = getChildCount();
    92.         children = new View[count];
    93.         for (int i = 0; i < count; ++i) {
    94.             children[i] = getChildAt(i);
    95.             if (i != mCurrentView) {
    96.                 children[i].setVisibility(View.GONE);
    97.             }
    98.         }
    99.     }
    100.  
    101.     @Override
    102.     public boolean onTouchEvent(MotionEvent event) {
    103.         mGestureDetector.onTouchEvent(event);
    104.         return true;
    105.     }
    106.  
    107.     @Override
    108.     public boolean onInterceptTouchEvent(MotionEvent event) {
    109.         return mGestureDetector.onTouchEvent(event);
    110.     }
    111.  
    112.     void moveLeft() {
    113.         //  <--
    114.         if (mCurrentView < children.length - 1 && mPreviousMove != LEFT) {
    115.             children[mCurrentView+1].setVisibility(View.VISIBLE);
    116.             children[mCurrentView+1].startAnimation(inLeft);
    117.             children[mCurrentView].startAnimation(outLeft);
    118.             children[mCurrentView].setVisibility(View.GONE);
    119.  
    120.             mCurrentView++;
    121.             mPreviousMove = LEFT;
    122.         }
    123.     }
    124.  
    125.     void moveRight() {
    126.         //  -->
    127.         if (mCurrentView > 0 && mPreviousMove != RIGHT) {
    128.             children[mCurrentView-1].setVisibility(View.VISIBLE);
    129.             children[mCurrentView-1].startAnimation(inRight);
    130.             children[mCurrentView].startAnimation(outRight);
    131.             children[mCurrentView].setVisibility(View.GONE);
    132.  
    133.             mCurrentView--;
    134.             mPreviousMove = RIGHT;
    135.         }
    136.     }
    137.  
    138.     int getCurrentIndex() {
    139.         return mCurrentView;
    140.     }
    141. }
    And you only have to use one layout.xml. That way you won't have to reconnect widgets dynamically, they can all be connected during your Activity's onCreate().

    TestFrame.java
    Code (Text):
    1. package org.test.frame;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5.  
    6. public class TestFrame extends Activity {
    7.     /** Called when the activity is first created. */
    8.     @Override
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.main);
    12.     }
    13. }
    14.  
     

Share This Page

Loading...