1

I am trying to use the toolbar using the Android Developers Guide for enabling material theming on pre-lollipop devices.

My app theme extends Theme.AppCompat.Light.NoActionBar as so:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">  

and windowActionBar is set to false as so:

<style name="AppTheme" parent="AppBaseTheme">
        <item name="colorPrimary">#EEEEEE</item>
        <item name="windowActionBar">false</item>
</style>  

My MainActivity looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}  

but the toolbar appears in the middle of the screen rather than on top. What am I doing wrong?

activity_main.xml:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="40dp"
        android:background="#EEEEEE" />

</android.support.v4.widget.SwipeRefreshLayout>
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 2
    you forgot to post activity_main.xml ... without its content it is not possible answer to this question – Selvin Mar 13 '15 at 11:00
  • @Selvin Done! Posted the xml. I plan to add more to the XML. I am just starting. – An SO User Mar 13 '15 at 11:02
  • use LinearLayout ... put Toolbar first then the SwipeRefreshLayout (or you wana Toolbar inside SwipeRefreshLayout ?) – Selvin Mar 13 '15 at 11:02
  • @Selvin Oh as a rule of thumb, if I want it at the top, it must be the first view in the layout (inside the parent)? – An SO User Mar 13 '15 at 11:15

1 Answers1

3

Your toolbar has nothing to do in the SwipeRefreshLayout

you have to use a LinearLayout (vertical) surrounding the Toolbar and SwipeRefreshLayout.

<LinearLayout orientation="vertical">
<Toolbar />
<SwipeRefreshLayout>
</SwipeRefreshLayout>
</LinearLayout>
Lukas Olsen
  • 5,294
  • 7
  • 22
  • 28