Android ActionBar missing after extending Appcompatactivity - android

I've recently updated my app extending Appcompatactivity in my Activities. Since then, the Actionbar is gone when I launch an external library Intent.
For example, I'm using the HockeyApp SDK to launch their FeedbackActivity
Here is my code:
FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file));
And here a screenshot (you can see the ActionBar is gone).
It used to work before until I started extending Appcompatactivity.
For the rest of Activities it works. The ActionBar is gone only when I launch an external library Intent.
Any ideas?

First, check your theme it may be like below ("NoActionBar"). Then the action bar is not appearing. If this is your issue. please add an appropriate theme for your application
<application
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
/>
if your theme is not a problem, you can add below content to your XML file. (add this as a first child of your XML file)
<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"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
and add below content to your activity on create method
protected void onCreate(Bundle savedInstanceState) {
.......
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}

The reason is probably that FeedbackManager.showFeedbackActivity(this, Uri.fromFile(file)) opens a new FeedbackActivity.class which is subclass of Activity.class instead of AppCompatActivity.class, so it can not show the ActionBar.Here is a link https://stackoverflow.com/questions/30681918/nullpointerexception-with-actionbar-setdisplayhomeasupenabledboolean-on-a-nu that explains some reasons.

Related

Setting toolbar title not working in onCreate()

I am having issues setting the title of my toolbar, which only seems to work when there is a delay.
For example a simple oncreate() method for my app 'Forecast'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(_toolbar);
setTitle("ABCDEFG");
}
When the app is run, the toolbar has the name of my app, not 'ABCDEFG'.
However, if I put a 200 millisecond delay before setting the title..
Observable.just("ABCDEFG")
.delay(200,TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::setTitle);
My Toolbar title is correctly displayed as "ABCDEFG"
this is the same when it comes to doing anything with the Toolbar, does anyone know what is actually going on here? and how I can solve this without having to put a delay in?
Here is my toolbar xml..
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
Thanks for your help!
EDIT forgot to mention setTitle() simply calls _toolbar.setTitle(title)
If you call setSupportActionBar(Toolbar), then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle("My Title"); to set a custom title.
Also check this link where toolbar.setTitle("My title"); may cause problem like below:- In android app Toolbar.setTitle method has no effect – application name is shown as title
And toolbar is the general form of action bar.
We can have multiple toolbars as layout widget but action is not.
Thus better approach is to use getSupportActionBar().setTitle("My Title");
As maRShmallow stated, I changed _toolbar.setTitle() to getSupportActionBar().setTitle()

What is the alternative to getActionBar()?

When I try to use getActionBar() it says it is deprecated, so that means by going forward we are not allowed to use getActionBar().
Which is the best way to learn how to create Tabs in the current situation?
If you are using appcompat, the correct method is getSupportActionBar()
The reason you should create this, because Fragment Tabs using
ActionBar is now deprecated. This is new way to create your actionbar with
Material Design. to use this you need to add dependencies in your build.gradle file.
compile 'com.android.support:appcompat-v7:21.0.0'
create toolbar app_bar.xml file, you can give the name you want.
<?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:layout_height="wrap_content"
/>
Make sure, you selected theme in styles.xml with
"Theme.AppCompat.NoActionBar"
after that, you need to initialize this toolbar in your mainActivity.xml layout file, for that include this,
<include
android:id="#+id/app_toolbar"
layout="#layout/app_toolbar"/>
then in the MainActivity, you need to make instance of Toolbar toolbar. then, after setContentView()
add following with your
toolbar = (Toolbar) findViewById(R.id.app_toolbar);
setSupportActionBar(toolbar);

How to include a dropdown menu to Android Action Bar

I want to include a drop down menu to android action bar like in the Google Maps app. I don not want to include any third party libraries such as actionbarsherlock since I believe we can do this using android SDK.
You can use a toolbar from the AppCompat library to act as your actionbar and then add a spinner within the toolbar because toolbar acts like a regular layout where you can add views within it.
here is a sample:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="#dimen/triple_height_toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
And finding the spinner within the toolbar is calling the findViewById within the toolbar.
toolbar = (Toolbar) findViewById(R.id.tool_bar);
Spinnertitle = (Spinner)toolbar.findViewById(R.id.toolbar_title);
Link Here is how to add a toolbar in your application

Why was ActionBarActivity deprecated

I installed Android Studio freshly and I begun coding an activity to extend ActionBarActivity and it showed that it was deprecated. So how else do I set up an actionbar for my activity.
Also the Getting Started Training uses the ActionBarActivity without making reference that it has been deprecated.
ActionBar is deprecated ever since Toolbar was introduced. Toolbar can be seen as a 'superset' of any action bar. So the 'old' ActionBar is now an example of a Toolbar. If you want similar functionality, but without deprecation warnings do the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
You need to define the Toolbar in your layout xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
With this new functionality you can create your own custom ActionBar and let Android do the heavy lifting. Just create your own custom view that extends from Toolbar.
Also, you should use AppCompatActivity instead of ActionBarActivity, it was introduced in the latest version of the appcompat library. So dont forget to update gradle
compile 'com.android.support:appcompat-v7:22.1.1'
Here is the answer from the post in Android developers blog:
"ActionBarActivity has been deprecated in favor of the new AppCompatActivity."
You can read more about it there.
This answer give a simple way to Eliminate the error message.
You can see as an add to others'.
When we change the parent Activity class: ActionBarActivity to AppCompatActivity the error message will disappear.
You can click here for more info.

Android Toolbar - How to implement Spinner for navigation mode?

What is currently the correct way to implement the View Control (No. 2 from the below screenshot taken from Android's design guide):
I found this example but when I tried to replicate it, I noticed that methods like:
actionBar.setNavigationMode() are already deprecated.
So how should I implement it? I thought at first that it's a Spinner but I see apparently that it's not exactly the same
and can I still use ActionBar or should I better move to use Toolbar (yes, I am confused...)
As you rightly said, the setNavigationMode() method is now considered passé. To get the spinner in API 21, you need to use the Toolbar in this way:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="#+id/spinner_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar
Add the above code to your Activity's layout. To set up the Toolbar in this Activity, you need to do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
}
Try this. This will work.

Categories

Resources