Nath5
Nath5

Reputation: 1655

Android Toolbar Covering Content

I am using the new toolbar and setting it as the supportActionBar. My problem is that it is covering my content.

Here is the code I am using to setup the toolbar.

 public void setupNavBar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);

        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
    }
}

The Styles:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="ToolBarStyle" parent="">
    <item name="android:elevation">5dp</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

and the layout:

<RelativeLayout 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"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/material_blue_grey_800"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:id="@+id/toolbar_actionbar"

    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#478"
    android:text="@string/hello_world" />

I thought that by calling setSupportActionbar the system would take care of positioning content below the toolbar view but that is not what I am seeing.

What is the correct way of using the toolbar. I have some activities where I wan the toolbar to be transparent with the content behind it it and others where I want it to be opaque with the content below.

Thanks

Upvotes: 8

Views: 7121

Answers (3)

Muriz
Muriz

Reputation: 1

I figured out why the content covered by action bar. if you use newest Kotlin version, you will have this code when first time create Android project:

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
        insets
    }

Dont delete them, and your content will not covered by action bar if you use action bar in your app. I have tried give padding in content based on action bar size but is not perfectly under action bar

Upvotes: 0

Pete
Pete

Reputation: 51

I just encountered this same issue. I fixed it by adding these two lines in the context_main.xml file:

app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.yourappname.MainActivity"

Like this:

<?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="@color/blue"
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.yourappname.MainActivity"
tools:showIn="@layout/app_bar_main">

Upvotes: 2

mohab elmahdy
mohab elmahdy

Reputation: 173

use below in textview

    <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#478"
android:text="@string/hello_world"
android:layout_below="@+id/toolbar_actionbar"
/>

Upvotes: 3

Related Questions