Trajan
Trajan

Reputation: 1440

Making a toolbar fit the whole of the top of the screen

I am having problems making my toolbar look like a toolbar not just a random rectangle near the top of my screen. Here is a screenshot of what it looks like currently, enter image description here

The toolbar does not seem to fit the screen or even look like a toolbar.

My code is

MainActivity.java: public class MainActivity extends AppCompatActivity {
                      ...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
                      ...

and in activity_main.xml:

    <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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/MainActivity"
    android:clickable="true"
    android:background="@color/BLACK"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="8dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="OFF"
        android:id="@+id/OFF"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="150dp"
        android:textColor="#ffffff"
        android:visibility="visible"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="ON"
        android:textColor="#000000"
        android:id="@+id/ON"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="150dp" />

</RelativeLayout>

and in styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">#3F51B5</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">#303F9F</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">#FF4081</item>
    </style>

</resources>

I really wanted the toolbar to look more like this

enter image description here

I am also not happy with the whiteness of the very top bar where battery level, wifi level etc are displayed. How should I fix this all?

Upvotes: 2

Views: 2757

Answers (3)

Stanojkovic
Stanojkovic

Reputation: 1632

Remove all paddings from your main.xml file. Add this to your styles.xml

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
 </style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

Upvotes: 1

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

Padding on parent RelativeLayout is affecting your toolbar, you should remove

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"

or change the value to

android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"

I hope it helps.

Upvotes: 1

MidasLefko
MidasLefko

Reputation: 4559

Get rid of the right and left padding in your root RelativeLayout

Upvotes: 2

Related Questions