Friday 5 April 2013

Stretching a View to fill the space between two other views

Stretching a View to fill the space between two other views


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- view1 goes on top -->
    <TextView
        android:id="@+id/view1"
        android:background="@drawable/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="relative_layout_1_top"/>

    <!-- view2 goes on the bottom -->
    <TextView
        android:id="@+id/view2"
        android:background="@drawable/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="relative_layout_1_bottom"/>

    <!-- view3 stretches betweeen view1 and view2 -->
    <TextView
        android:id="@+id/view3"
        android:background="@drawable/yellow"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_above="@id/view2"
        android:layout_below="@id/view1"
        android:text="relative_layout_1_center"/>

</RelativeLayout>

Using two layout xml file for one Activity

Using two layout xml file for one Activity


public class Test extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout layoutMain = new LinearLayout(this);
    layoutMain.setOrientation(LinearLayout.HORIZONTAL);
    setContentView(layoutMain);
    LayoutInflater inflate = (LayoutInflatergetSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout layoutLeft = (RelativeLayoutinflate.inflate(
        R.layout.main, null);
    RelativeLayout layoutRight = (RelativeLayoutinflate.inflate(
        R.layout.row, null);

    RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutMain.addView(layoutLeft, 100100);
    layoutMain.addView(layoutRight, relParam);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/left"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/view1" android:background="@drawable/icon"
    android:layout_width="fill_parent"
    android:layout_height="50px" android:text="1" />
  <TextView android:id="@+id/view2"
    android:background="@drawable/icon"
    android:layout_width="fill_parent"
    android:layout_height="50px" android:layout_below="@id/view1"
    android:text="2" />
</RelativeLayout>

//row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/right"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">


  <TextView android:id="@+id/right_view1"
    android:background="@drawable/icon" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="3" />
  <TextView android:id="@+id/right_view2"
    android:background="@drawable/icon"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/right_view1" android:text="4" />
</RelativeLayout>