4

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>
13
  • check this chris.banes.me/2014/10/17/appcompat-v21 Commented Feb 16, 2015 at 16:19
  • @Raghunandan it means I should add it manually but when I have added it neither home button not shown nor title
    – Sasha
    Commented Feb 16, 2015 at 16:22
  • yes you need to. You can customize the toolbar. also you can set toolbar to act as actionbar with setSupportActionBar(toolbar);. Further check the google io sched app 2014 Commented Feb 16, 2015 at 16:24
  • what is minSdkVersion of your project? Is it <11?
    – Apurva
    Commented Feb 16, 2015 at 16:28
  • 1
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); if you are using support library and if not then remove Support
    – Apurva
    Commented Feb 16, 2015 at 17:03

3 Answers 3

7

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

2
  • how to set actionBar.setDisplayHomeAsUpEnabled(true) in toolbar I cant
    – Sasha
    Commented Feb 16, 2015 at 16:38
  • Depedending on your desired API levels, try to extend AppCompatActivity, as it provides a ToolBar automatically.
    – MiStr
    Commented Dec 3, 2015 at 21:13
2

You should include toolbar in your layout. Something like this:

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

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

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

Hope it helps you!!

2

Write getSupportActionBar().setDisplayHomeAsUpEnabled(true); if you are using support library and if not then remove Support just write getActionBar().setDisplayHomeAsUpEnabled(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.