Why was ActionBarActivity deprecated - android

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.

Related

Android ActionBar missing after extending Appcompatactivity

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.

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

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.

setSupportProgressBarIndeterminateVisibility raising java.lang.NullPointerException when used with Android 5.0 SDK (API 21)

My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setSupportProgressBarIndeterminateVisibility(true);
}
I cannot grasp what's wrong with my code and why I am getting java.lang.NullPointerException raised by the setSupportProgressBarIndeterminateVisibility().
My gradle dependencies contains:
compile 'com.android.support:appcompat-v7:21.0.0'
Does anybody know how to use the indeterminate progress bar in the support.v7 action bar with the API 21?
You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.
Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.toolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
//Make progress bar appear when you need it
progressBar.setVisibility(View.VISIBLE);
//Make progress bar disappear
progressBar.setVisibility(View.INVISIBLE);
}
Hope this helps :)
EDIT: replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.
Per this comment on Chris Banes' (the author of AppCompat) AppCompat v21 announcement post:
Either way, progress bar's are not supported on Toolbar anymore.
On why:
Because Toolbar is a highly focused widget. If you want a ProgressBar, add it to the Toolbar yourself (it's just a ViewGroup).
I'd expect more information as the official documentation gets updated here shortly.

Categories

Resources