Ramakanta Chandra Ramakanta Chandra - 4 months ago 25
Android Question

How to make a relative layout to fit for all screen size?

i am comfortable with relative layout so while developing an app, using relative layout i found a problem that the app is not fitting on all screen size. i have developed the entire layout for my app. now can u plz tell me a solution so that i don't have to change much in my layout and my app will fit for all screen. one xml looks like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.medilearn.medilearn.Initiater"
tools:showIn="@layout/activity_initiater"
android:background="#e0e0e0">
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center|right"
android:text="Mediacl e-learning content distribution system "
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:background="#484848"
android:textColor="#ffffff"
android:layout_marginLeft="200dp" />

<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:textIsSelectable="false"
android:textStyle="bold"
android:layout_marginTop="50dp" />

<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="@+id/textView13"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3"
android:layout_marginTop="50dp" />

<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/editText"
android:layout_below="@+id/textView13"
android:layout_alignStart="@+id/textView13"
android:layout_marginTop="25dp" />

<TextView
android:layout_width="300dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="User Code"
android:id="@+id/textView14"
android:layout_below="@+id/editText"
android:layout_alignStart="@+id/editText" />

<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/textView14"
android:layout_alignStart="@+id/textView14"
android:layout_marginTop="25dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Start"
android:id="@+id/SigninButton"
android:layout_below="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_marginTop="50dp"
android:layout_marginLeft="80dp"
android:background="#006090"
android:textColor="#ffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:text="Version : 1.0"
android:id="@+id/textView15"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#484848"
android:textColor="#ffffff"
android:textIsSelectable="false"
android:textAlignment="textStart"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView" />

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Image Feeds will be shown here"
android:id="@+id/textView16"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true"
android:layout_marginRight="300dp"
android:layout_marginBottom="50dp"
android:textColor="#161515" />

<Button
android:layout_width="200dp"
android:layout_height="35dp"
android:text="Reload Content"
android:id="@+id/button7"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/editText2"
android:layout_marginLeft="50dp"
android:background="#006090"
android:textColor="#ffffff"
android:layout_marginBottom="7.5dp" />

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal|top"
android:src="@drawable/medilearn"

android:scaleType="fitCenter"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentStart="true"
android:layout_marginRight="950dp"
android:background="#484848" />

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text=" Media file version:02JUL2016"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView15"
android:layout_toEndOf="@+id/textView15"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:background="#484848"
android:gravity="center" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/textView5"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:background="#484848" />

</RelativeLayout>


hight,and width of the buttons and textview must be same.

Answer

following are all code that matching with all layout , its just demo of your code , please adjust as per your requirement.

Following are your xml file. please copy it and paste on your layout and see the result.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#e0e0e0"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="#484848"
    android:gravity="center"
    android:text="Mediacl e-learning content distribution system "
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Welcome"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textIsSelectable="false"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView13"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView3"
                android:layout_marginTop="10dp"
                android:layout_weight="0.5"
                android:text="Username"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView13"
                android:layout_marginTop="10dp"
                android:layout_weight="0.5"
                android:hint="Your edit text" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView14"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="User Code"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:ems="10"
                android:hint="Your edit text"
                android:inputType="textPassword" />

        </LinearLayout>

        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignEnd="@+id/imageView"
            android:layout_alignRight="@+id/imageView"
            android:background="#484848"
            android:gravity="center"
            android:text="Version : 1.0"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textIsSelectable="false" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Current Image Feeds will be shown here"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#161515" />

        <Button
            android:id="@+id/SigninButton"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="#006090"
            android:text="Start"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/button7"
            android:layout_width="200dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="#006090"
            android:text="Reload Content"
            android:textColor="#ffffff" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="10dp"
            android:background="#484848"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="#484848"
            android:gravity="center"
            android:text="Media file version:02JUL2016"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff" />

    </LinearLayout>
  </ScrollView>
 </RelativeLayout>
Comments