Michael Jones
Michael Jones

Reputation: 2272

Part of RelativeLayout content going off of screen because of Toolbar Android

very new to Android here! I have been working on a little project, but I have come into a minute problem. Basically, the Android Toolbar, that comes with the Basic Activity, is being calculated in to the match-parent height. Let me elaborate some more.

In my activity_main.xml I have the following piece of code:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

... (fab)

And then in my content_main.xml I have the following (snippet):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/content"
    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="@color/colorPrimary"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.michaeljones.testproject.MainActivity"
    tools:showIn="@layout/activity_main">

    <View
        android:id="@+id/homeBackgroundTop"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true"
        android:background="@color/colorAccent" />

</RelativeLayout>

Now my problem is when I am setting the height of the View (programmatically) in content_main.xml to the height of the Parent (RelativeLayout) my content is going off of the screen. I have set the RelativeLayout to be android:layout_height="match_parent", which I would expect to fill in the rest of the screen. Yet instead, it is also accounting for the Toolbar. When I remove the Toolbar this all works perfectly.

Any idea on how I could make it so the android:layout_height="match_parent" in the RelativeLayout will not account for the Android Toolbar, but instead make it the height of the screen that is not being used (all of it minus the toolbar)?

EDIT

I forgot to mention, the following is how I am grabbing the height of the RelativeLayout:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus == true) {
        RelativeLayout content = (RelativeLayout) main.findViewById(R.id.content);
        int w = content.getWidth();
        int h = content.getHeight();
    }
}

Upvotes: 1

Views: 733

Answers (1)

Bradford2000
Bradford2000

Reputation: 723

I think wrapping what you've got in a RelativeLayout should fix your issue. So something like this.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"/>
        </android.support.design.widget.AppBarLayout>

        <include
            layout="@layout/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar_layout"/>
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floating_dialpad_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_margin="@dimen/connect_main_fab_margin"
        android:background="@color/BackgroundActive"
        android:src="@drawable/icon_keypad"/>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 1

Related Questions