title: Position LinearLayout inside RelativeLayout dynamically
body: <p>I'm trying to position a <code>LinearLayout</code> below another element inside a <code>RelativeLayout</code>.  </p>
  
  <p>This is my xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/bg_android"
      android:weightSum="1"
      android:id="@+id/family_parent"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_margin="10dp"
          android:id="@+id/relativeLayout"&gt;
  
          &lt;ImageView
              android:layout_width="180dp"
              android:layout_height="62dp"
              android:id="@+id/imageView2"
              android:src="@drawable/test_logo"
              android:layout_alignParentTop="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true" /&gt;
  
          &lt;ImageButton
              android:layout_width="30dp"
              android:layout_height="30dp"
              android:id="@+id/menu_btn"
              android:layout_centerVertical="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentEnd="true"
              android:background="@drawable/menu_icon" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And I'm creating a new <code>LinearLayout</code> dynamically thats is working fine. The problem is that I can't seem to place the newly created element below an existing element inside the <code>RelativeLayout</code>.  </p>
  
  <p>Basically, I'm creating a new <code>LinearLayout</code> and I want to position it below the <code>RelativeLayout</code>with id <code>relativeLayout</code>. This is the code I have so far.</p>
  
  <pre><code>// GET ACTIVITY PARENT
  RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);
  
  RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();
  
  parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);
  
  // CREATE CHILD LAYOUTS
  LinearLayout newLLayout = new LinearLayout(getApplicationContext());
  LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
          100, ViewGroup.LayoutParams.MATCH_PARENT);
  
  newLLayout.setLayoutParams(parentParams);
  
  newLLayout.setBackgroundColor(Color.parseColor("#FF0000"));
  
  parent.addView(newLLayout, LLayoutParams);
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>// GET ACTIVITY PARENT
  RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);
  RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();
  
  
  LinearLayout newLLayout = new LinearLayout(getApplicationContext());
  LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
          100, ViewGroup.LayoutParams.MATCH_PARENT);
  
  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT,
          RelativeLayout.LayoutParams.WRAP_CONTENT);
  parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);
  
  newLLayout.setLayoutParams(layoutParams);
  
  parent.addView(newLLayout, LLayoutParams);
  </code></pre>
  
  <p>It's giving this error:  </p>
  
  <pre><code>03-17 05:12:39.831    5477-5477/pt.test_pro_gold E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.test_pro_gold/pt.test_pro_gold.ui.activities.PageFamily}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
              at android.app.ActivityThread.access$600(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
              at pt.test_pro_gold.ui.activities.PageFamily.onCreate(PageFamily.java:46)
              at android.app.Activity.performCreate(Activity.java:5008)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
              at android.app.ActivityThread.access$600(ActivityThread.java:130)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>