I am new to android and i was following following tutorial for Material Design Toolbar :
http://www.android4devs.com/2014/12/how-to-make-material-design-app.
You need to call setContentView
BEFORE adding any View
s such as the Toolbar
.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // <-- call this first
toolbar = (Toolbar) findViewById(R.id.toolbar); // <-- setup views after
setSupportActionBar(toolbar);
setTitle("DDIT_Results");
}
Please see the Official Android Developers blog here: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
To use Toolbar as an Action Bar, first disable the decor-provided Action Bar. The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or its light variant).
Second, create a Toolbar instance, usually via your layout XML:
<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
The height, width, background, and so on are totally up to you; these are just good examples. As Toolbar is just a ViewGroup, you can style and position it however you want.
Then in your Activity or Fragment, set the Toolbar to act as your Action Bar:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
final Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
From this point on, all menu items are displayed in your Toolbar, populated via the standard options menu callbacks.
Before adding toolbar you need to remove action from your style like
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Now define toolbar in res/xml/toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appbar"
android:theme="@style/AppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
Now include this toolbar in our activity layout xml like
<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"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@xml/toolbar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Note : Now while defining toolbar in activity we will use "tool_bar" as id not "appbar" id
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar actionbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(actionbar);
}
}
Now you can see in your activity toolbar
just import
android.support.v7.widget.Toolbar;
not
android.widget.Toolbar;
Make sure to add theme in the activity inside manifests
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
then define this attributes to the theme used above in the style.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
G