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.
Related
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.
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()
Im dealing with a strange toolbar behavior in which if I set a logo with
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
The Toolbar components (in my case the logo itself and the title) get disaligned from the standard gravity start behavior.
This is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/grey_900"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"/>
The toolbar has a SearchView at the right and no "home button", only title.
Graphic explanation:
1) Behavior without calling setLogo(...)
2) Behavior when calling setLogo(...)
3) What I'm trying to achieve
I've already tried playing with gravity but nothing happened.
Also, please, I know that Toolbar is a ViewGroup and I can customize it, but I'm looking for a clean code, also this should be a default behavior and but it isn't working so I would like to know if I'm doing something wrong.
How can I fix this? Thanks in advance!
getSupportActionBar().setTitle("Home");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
If you use getSupportActionBar , No need to use toolbar again.
How can I add this labels with arrow below a toolbar?
By that I want to make a transition - with the help of an animation - from one activity to the other.
Thanks in advance!
You can create a custom toolbar. First create your toolbar_layout.xml and place whatever you want (including your label and arrows) into that layout. Then add the following to your activity_layout.xml:
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
And finally add the following to your activity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.