What is the alternative to getActionBar()? - android

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);

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.

Is it possible to automatically include Toolbar in window decor?

I have just started migration to material theme using support library 23.1.
I have looked up the guides and all revolve around the following procedure..
1.Use theme without ActionBar provided by decor. I.e.
<style name="MyTheme" parent="Theme.AppCompat">
...
</style>
2.Put a Toolbar widget somewhere in activity layout xml:
<android.support.v7.widget.Toolbar
android:id=”#+id/myToolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
3.In code set this toolbar as an action bar:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
The problem is that some of my activities use FrameLayout as a root layout and as a consequence a toolbar is placed on top of the screen and overlays existing content and therefore hides it.
One workaround would be to create vertically oriented LinearLayout and put Toolbar and FrameLayout as it's children. But, this ads complexity, another level of Views and affects performance.
My first question would be how does mechanism of setSupportActionBar(toolbar) works? Does it takes the toolbar out of its place as set in xml layout and reinserts it at the top of containing layout. Or it just leaves it there where it is?
And the main point, is it possible to use the new Toolbar as decor provided ActionBar? Leave my code unchanged and it will automagically use Toolbar instead of ActionBar? Is it possible to achieve this?
EDIT - UPDATE:
If I leave my code unchanged as if I want to leave the old ActionBar the menu in not instantiated.
EDIT - UPDATE
As far as I could gather there is no way to persuade android to create Toolbar instead of action bar. This sadly means refactoring of all activities and adding another outer enclosing Layout thereby needlessly increasing complexity.
First question: How does the setSupportActionBar(toolbar) works?
What the above statement does is make the toolbar act like an ActionBar. By default the toolbar does not have action bar capabilities, and to make the toolbar act like an action bar, you need this statement. The followin is the official documentation of this method.
Set a Toolbar to act as the ActionBar for this Activity window.
When set to a non-null value the getActionBar() method will return an
ActionBar object that can be used to control the given toolbar as if
it were a traditional window decor action bar. The toolbar's menu will
be populated with the Activity's options menu and the navigation
button will be wired through the standard home menu select action.
setSupportActionBar replaces the decor action bar with the toolbar that you are supplying as a view in the activity. This means that unlike action bar, toolbar will live as a view in your activity. It doesn't take the toolbar out of the layout and reinserts it at the top of containing layout. This means that you can have the toolbar anywhere you want in the screen, not necessarily at the top.
Second Question: Leave my code unchanged and it will automagically use Toolbar instead of ActionBar?
No, you can't do this. Toolbar has to be present in your layout, (either via xml or inflated via code). This is the whole purpose of the toolbar. With action bar you don't have this control. Toolbar allows you to modify it as much as possible and you can a different one in different activity. So how do you do it? What's the best practice?
Well, it's recommended that all activities have an accompanying fragment. So you could have a common layout for all activities (like the one below). Now, you could just inflate the fragment in the FrameLayout.
<LinearLayout 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"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
of course you don't have to add the toolBar if you don't want to (but you will lost some of the cool navigation patterns the toolbar is involved at).
Just make your activity extend a theme with toolbar and you will find it there where its supposed to be.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Then in your activity you will be able to get ActionBar instance as usual:
ActionBar actionBar = getSupportActionBar();

Disable navigation icon riple effect in toolbar

Not sure what you call the round effect when you click to go back to previous activity in my case (arrow back icon) but i like to disable that.
This is the code that puts it on the toolbar, but i cant seem to find (if there is) an option to disable that round effect when clicked.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Like this round effect buttons produce in toolbar's when pressed. I presume a Material design effect.
Thanks
By changing in your theme you can achieve that. Remove <item name="android:background">#color/toolbar</item> from your theme defined in style.
Follow this stack-overflow link.
Use this in your layout
<android.support.v7.widget.Toolbar
android:id="#+id/yourToolbarId"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/yourId"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_width="wrap_content"
app:layout_height="wrap_content"
app:minHeight="?attr/actionBarHeight"
app:minWidth="?attr/actionBarHeight"
android:src="#drawable/ic_arrow_back"/>
</android.support.v7.widget.Toolbar>
and getSupportActionBar().setDisplayShowHomeEnabled(false); in conjunction
and dont use this
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Also you have to manually set your click listener to perform specific action you need when it is pressed.
Use false instead of true
getSupportActionBar().setDisplayShowHomeEnabled(false);

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.

Categories

Resources