Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar and ActionBar not shown in activity

Tags:

android

I use Material design firstly in my apps and my problem is when I create activity there is no action bar or toolbar but I have Material Navigation drawer why it not implemented automatically like in older versions Layout of activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView4" />

</LinearLayout>

Activity: package fragments;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;


  public class Noname1 extends ActionBarActivity{

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

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

     getMenuInflater().inflate(R.menu.main, menu);
     return super.onCreateOptionsMenu(menu);
 }

} ` My theme:

  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/nliveo_green_colorPrimary</item>
    <item name="colorPrimaryDark">@color/nliveo_green_colorPrimaryDark</item>
    <item name="colorAccent">@color/nliveo_green_colorPrimary</item>
    <item name="android:textColorPrimary">@color/myTextPrimaryColor</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="android:windowBackground">@color/myWindowBackground</item>
</style>
like image 389
Sasha Avatar asked Dec 19 '22 06:12

Sasha


1 Answers

Make a custom_toolbar.xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/nliveo_green_colorPrimary"
    android:layout_height="?android:attr/actionBarSize" />

Include it in your layout file wherever you want like:

<include android:id="@+id/toolBar" layout="@layout/custom_toolbar"/>

in Your onCreate Method:

public class Noname1 extends ActionBarActivity{

   @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.test);
     Toolbar tb = (Toolbar)findViewById(R.id.toolBar);
     setSupportActionBar(tb);
   }
}

And keep your manifest file:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/nliveo_green_colorPrimary</item>
    <item name="colorPrimaryDark">@color/nliveo_green_colorPrimaryDark</item>
    <item name="colorAccent">@color/nliveo_green_colorPrimary</item>
    <item name="android:textColorPrimary">@color/myTextPrimaryColor</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="android:windowBackground">@color/myWindowBackground</item>
</style>

Hope it helps!!!

like image 134
Kostas Drak Avatar answered Jan 15 '23 04:01

Kostas Drak