0

I have a BaseLayout, the BaseLayout is a ParentLayout for other layouts. In other words, the other Layouts inherit/include it. The BaseLayout has DrawerLayout (shared for all layouts) but each Activity has a different content.

My BaseLayout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
  <android.support.v4.widget.DrawerLayout
    android:id="@+id/myDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--Main Content-->
    <LinearLayout
      android:id="@+id/rootLinearLayout"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:orientation="vertical">
      <!--Content-->

    <!--must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there !-->

    </LinearLayout>
    <!-- Left Navigation Derawer -->
    <LinearLayout
      android:id="@+id/llLeftDrawer"
      android:orientation="vertical"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:background="?attr/colorAccent"
      android:padding="15dp"
      android:layout_gravity="right">
      <!-- Left Navigation Derawer Content-->
      <TextView
        android:text="Categories"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:gravity="center"
        android:textColor="#FFFFFF"
        fontPath="fonts/MobileFont.ttf" />
      <ListView
        android:id="@+id/lvLeftMenu"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />
    </LinearLayout>
  </android.support.v4.widget.DrawerLayout>
</LinearLayout>

and included in Postlayout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
  <include
    layout="@layout/BaseLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout> 

How can I add PostLayout elements into the BaseLayout LinearLayout id rootLinearLayout?

postLayout element is this:

<LinearLayout
                android:orientation="vertical"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout1">
                <TextView
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtPostTitle"
                    android:gravity="right" />
                <TextView
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtPostDate"
                    android:gravity="right" />
                <android.webkit.WebView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/wbvPostContent" />
            </LinearLayout>
Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
arman
  • 125
  • 1
  • 12
  • 1
    This should help you: https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view You may want to further search for `android dynamically add view`. – Bruno Bieri Jul 01 '17 at 10:20
  • Possible duplicate of [Android - Dynamically Add Views into View](https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – David Rawson Jul 02 '17 at 08:21

3 Answers3

0

First of all, provide id attribute to the included Baselayout in the Postlayout.

Then, you can get the View object from the included view by calling the following from the activity that has the Postlayout :-

View includedView = (View)findViewById(R.id.idThatYouGaveToIncludedLayout);

Then, you can access all Baselayout views from the includedView object and perform the operations that you require.

For example,

LinearLayout rootLinearLayout = (LinearLayout)includedView.findViewById(R.id.rootLinearLayout);

Similar to the above code, you can obtain objects of the other views of your Baselayout.

Rohan Stark
  • 2,346
  • 9
  • 16
  • I need add `postLayout` elements in to the `rootLinearLayout` of his own layout when included from `BaseLayout`, BaseLayout only have `DrawerLayout` .but different content for each activity ,ex `PostDetailsActivity` have 2 textview and one webview. an other activity `MainActivity` have one `RecyclerView` andother activity ... . What I want to do all activity's layout Included `BaseLayout` after than add element for each activity programmatically to his own layout `rootLinearLayout` – arman Jul 01 '17 at 13:33
0

Bruno Bieri get solution is java and i converted to xamarin:

LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View v = inflater.Inflate(Resource.Layout.your_layout, null);

// fill in any details dynamically here
TextView textView = v.FindViewById<TextView>(Resource.Id.a_text_view);
textView.Text="your text";

// insert into main view
ViewGroup insertPoint = FindViewById<ViewGroup>(Resource.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

and Rohan Stark solution good for Access to elements of Included Layout

tanQ Bruno Bieri and Rohan Stark,

arman
  • 125
  • 1
  • 12
0

when you add view as include, can use all view like own main layout views. find the layout as a view and add view to it. like below:

LinearLayout linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);

 linearLayout1.addView(new TextView(context));
Haniyeh Khaksar
  • 774
  • 4
  • 20