• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Adding ActionBar in Android Stuido 2.0

Noamm91

Lurker
Apr 18, 2016
7
1
This is my first time (trying to) work with ActionBar in android app but can't figure it out.

My goal is to have , in the end , simple actionBar with Overflow menu , Icon and a title.

This is my simple code so far:

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

publicclassMainActivityextendsAppCompatActivity{

@override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(true);
//actionBar.setLogo(R.mipmap.ic_launcher);
}
}

The relevant xml code where the toolbar is defind is:

<android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@Style/ThemeOverlay.AppCompat.ActionBar" />

In the new project wizard I chose the Minimum SDK to be API 14 (Android 4.0) because I've realized that there are some issues with supporting ActionBar in API 11 (Android 3.0) and below and I don't care about not targeting to those devices and don't want any unnecessary headache.

Anyway , the code as it written above (with the lines in comment) making the app crash.

If I also comment the line with the .SetDisaplyUseLogoEnables(true) then the app doesn't crash.

The reason I've added toolbar line is because I searched online for solution before posting here and someone said to try and do so (I don't really understand why) so I tried and it has no effect whatsoever.

Also I've looked in this link http://developer.android.com/training/appbar/setting-up.html And followed all the instructions but I get an error when I write the line setSupportActionBar(myToolbar); (It doesn't compile)

I would be happy if you can give some background about working with ActionBar in Android Studio 2.0 as most of the information about this in the internet seem not up to date with current Android Studio version.

Thankyou very much, Noam
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones