By using this approach we will calculate the sizes of the Views programmatically at runtime and set them. In our example, we can see that the Image actually occupies around 88% of the total width. So, we will calculate the expected image width for the current device screen width and set this new size to the ImageView. Also calculate the margins between these views and set that as well.
Define the Image to the Screen ratio:
// The Image uses 88% of the screen width
private static final float IMAGE_SCREEN_WIDTH_RATIO = 0.88f;
Calculate the ImageView size and Margins:
// Get the actual screen width
int screenWidth = getScreenWidth();
// Calculate the new image size
int imageViewSize = (int) ((float) screenWidth * IMAGE_SCREEN_WIDTH_RATIO);
// Calculate the margin
int margin = (screenWidth - imageViewSize) / 2;
Set this new size to the ImageView and TextViews before the UI is rendered:
// Set the Image size
LinearLayout.LayoutParams params = (LayoutParams) mImageView.getLayoutParams();
// Setting the same size for Width and height
params.height = imageViewSize;
params.width = imageViewSize;
// Set the margin's
params.topMargin = margin;
params.bottomMargin = margin;
mImageView.requestLayout();
// Set the Header Tv margin
params = (LayoutParams) mHeaderTv.getLayoutParams();
params.bottomMargin = margin;
mHeaderTv.requestLayout();
// Set the Sub Header Tv margin
params = (LayoutParams) mSubHeaderTv.getLayoutParams();
params.bottomMargin = margin;
mSubHeaderTv.requestLayout();
int marginBetweenBtns = margin / 2;
// Set the Cancel Btn margin
params = (LayoutParams) mCancelBtn.getLayoutParams();
params.rightMargin = marginBetweenBtns;
mCancelBtn.requestLayout();
// Set the Send Btn margin
params = (LayoutParams) mSendBtn.getLayoutParams();
params.leftMargin = marginBetweenBtns;
mSendBtn.requestLayout();
Here is the XML definition for this layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaaaaa" >
<LinearLayout
android:id="@+id/screen_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/chrome_icon" />
<TextView
android:id="@+id/header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header fontsize - 25"
android:textSize="25sp" />
<TextView
android:id="@+id/subheader_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text fontsize - 17"
android:textSize="17sp" />
<LinearLayout
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/cancel_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffffff"
android:text="Cancel"
android:textColor="#007Aff"
android:textSize="15sp" />
<Button
android:id="@+id/send_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffffff"
android:text="Send"
android:textColor="#007Aff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Here is the screen shot of the same UX design on Android using this approach:
The advantage of using this approach is that the UI will look consitent across all the devices. It will consume the same percentage of screen on all the devices. But to achieve this, we need to relayout the Views when the new sizes are computed.