Wubbalubbadubdub
Wubbalubbadubdub

Reputation: 2475

AppcompatActivity with toolbar not showing toolbar after apprunning but shows on xml preview android studio

I have already worked with AppcompatActivity and integrate android.support.v7.widget.Toolbar . But for the very first time I am having a unknown problem . my toolbar is showing in xml preview , but after running the app the toolbar disappeared . here is my code and style file .here are three files MainActivity , layout file and style file from values folder `public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}`

and in layout file , I removed my code just write the toolbar part for clarity , and is style file Apptheme.base theme is the parent theme which is inherited by values-v21/style.xml

    <?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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <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">

            <!--<RelativeLayout
                android:id="@+id/rl_toolbar_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <ImageView
                    android:id="@+id/tv_home_header"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:drawablePadding="10dp"
                    android:src="@mipmap/ic_launcher" />
            </RelativeLayout>-->


        </android.support.v7.widget.Toolbar>

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

</RelativeLayout>

in values/style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.base">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
</style>

<style name="Button" parent="AppTheme.base">
    <item name="android:textColor">#ffffff</item>
    <item name="android:padding">0dp</item>
    <item name="android:minWidth">88dp</item>
    <item name="android:minHeight">36dp</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:elevation">1dp</item>
    <item name="android:translationZ">1dp</item>
    <item name="android:background">@drawable/primary_round</item>
</style>

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.base">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
</style>

Upvotes: 0

Views: 1314

Answers (1)

Alfred Cao
Alfred Cao

Reputation: 31

I encountered that problem too and mine got solved by fixing the declaration of the activity in the Manifest. Remove the theme line if it is there:

<activity
    ...
    android:theme="@style/AppTheme.NoActionBar"

Hope this helps.

Upvotes: 3

Related Questions