I am having following layout in file mylayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/yellow"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:id="@+id/Layout5"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Layout6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="top">
<ImageView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/green"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="5dp"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="@+id/imageView">
<Button
android:id="@+id/btn1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/text_btn_1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/text_btn_2"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
With this layout when i am doing setContentView(R.layout.mylayout) for my activity, which has transparent theme, my layout is getting displayed in the center of the screen.
I want to change this layout positioning dynamically, so center(default) and bottom(by using following code) display is working.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
But somehow i am not able to figure out how to display this layout dynamically to the top of the screen.
Any help will be appreciated.
I found the following fix for this issue. I added filler Liner layout in the end under Relative layout.
To display in center don't do anything.
To display in top use following code. Just move filler layout to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);
To display in bottom use following code. i.e. move layout6 to bottom.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);