James H
James H

Reputation: 580

Android Studio Layout. won't match parent

Everything looks fine in my previews but when I open the app in an emulator the layout doesn't match parent for length or width. Everything is where it should be within the layout it just doesn't stretch overall with the screen. This is happening in every layout in the app(I'm using fragments). To explain better heres pix:

** What is in the preview(and is what i want) enter image description here

** What I have

enter image description here

here is the XML.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/hlineand"/>

    <TextView
        android:id="@+id/userTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="User"
        android:textAlignment="center"
        android:textAppearance="@android:style/TextAppearance.Material.Small"
        android:textColor="@android:color/darker_gray"
        android:textSize="30dp"/>

    <TextView
        android:id="@+id/updateTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:text="----"
        android:textAlignment="center"/>

    <EditText
        android:id="@+id/emailEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:hint="e-mail"
        android:inputType="textEmailAddress"
        android:layout_below="@+id/userTextView"
        android:layout_centerHorizontal="true"/>

    <EditText
        android:id="@+id/passwordEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/emailEditText"
        android:layout_below="@+id/emailEditText"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/passwordEditText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Login"/>
</RelativeLayout>

This is my content main.xml. Like the first commenter mentioned, it was in there. It worked great when I put in match parent & match parent but not its resizing to some like width 344px & height 590px. Any way I can keep that as match parent??

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.a2.reach.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="344dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Upvotes: 1

Views: 3484

Answers (4)

Sem Kaminskyi
Sem Kaminskyi

Reputation: 11

in my variant change size parent Layout - fixed my problem

    <FrameLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </FrameLayout>

Upvotes: -1

James H
James H

Reputation: 580

Fixed the content main XML by changing the width height to 0dp instead of match parent & adding the 4 constraints on the bottom of the frame layout. Thank you guys and thank you as well to @Du.fantasy for pointing me in the right direction so fast

<FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

Upvotes: 3

Uzair Qaiser
Uzair Qaiser

Reputation: 156

As per my knowledge, In your first XML, Your are giving orientation="vertcial" to relative layout. If you want that it should work then you should use Linear layout and use orientation for that layout.

Try using like this in you content XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_blood_g_activity"
android:layout_width="match_parent"
android:orientation="vertical"
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">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mature Diabetic Patient"
    android:layout_gravity="center"
    android:textSize="15dp"/>


<View
    android:layout_width="wrap_content"
    android:layout_height="30dp"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Your current Blood Sugar"/>
    <View
        android:layout_width="20dp"
        android:layout_height="60dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cbs"
        android:inputType="number"
        android:hint="mg/dl"/>

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Your Target Blood Sugar"/>
    <View
        android:layout_width="20dp"
        android:layout_height="60dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tbs"
        android:inputType="number"
        android:hint="mg/dl"/>

</LinearLayout>
 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Humalog/Novalog"
        android:id="@+id/btnhnbs"
        android:layout_gravity="center"/>
  </LinearLayout>

Upvotes: 0

Om Infowave Developers
Om Infowave Developers

Reputation: 1565

change the FrameLayout to match_parent from fix width and height

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.a2.reach.MainActivity"
        tools:showIn="@layout/app_bar_main">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </android.support.constraint.ConstraintLayout>

Upvotes: 1

Related Questions