I would want to have an actionable that has a custom background, and two icons. One for the app icon and the other replacing the text name of the app.
Currently , for some reason , the only thing that is displayed is text and an invisible margin at the top :
I'm using support library (v7) ,my minSdk is 9.
manifest :
...
<activity android:theme="#style/Theme.AppCompat.Light" android:name=".myPagerActivity" />
...
You are using the AppCompat version 21 and it is normal.
The Action Bar follows the material design guidelines and uses a Toolbar.
As you can read here:
The use of application icon plus title as a standard layout is
discouraged on API 21 devices and newer.
It is the reason because you are displaying only the test in your Activity.
You can customize your Toolbar. Something like:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar">
<!-- your custom views here -->
</android.support.v7.widget.Toolbar>
In your AppCompatActivity:
Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(actionBar);
//Customize your views here...
Finally you have to use a appcompat theme like .NoActionBar
Maybe this could help you:
Replace your ActionBar by a Toolbar.Toolbar are easy to customize.
You should probably define a custom theme and style the action bar.
Check https://developer.android.com/training/basics/actionbar/styling.html
You can change almost all the attributes associated with the action bar.
Related
On my main activity, where there is no way to go back, I'd like to remove the app "home" button in the actionbar.
I think it is confusing for the user, who still can quit the app with the OS back button.
Browsing stackoverflow, I saw a whole lot of people asking this, and not a single answer worked for me. Here is the list :
Removing left arrow from the actionbar in android?
To remove the back arrow button from Action Bar
Remove default home/up button in action mode
how to hide up button in actionbar
How to remove v7 toolbar back button
Setting HomeAsUpEnabled(true) but hide back button actionbar
The answers were usually redondant and could be summarized to following :
ActionBar supportActionBar = getSupportActionBar(); //I target some low API
if(supportActionBar != null) {
supportActionBar.setDisplayShowHomeEnabled(false);
supportActionBar.setDisplayHomeAsUpEnabled(false);
supportActionBar.setHomeButtonEnabled(false);
supportActionBar.setHomeAsUpIndicator(null);
}
I tried every combination of those and nothing worked, whether I try with the default ActionBar or with an XML-declared compat-v7 ToolBar with setSupportActionBar().
I also couldn't see any question adapted for the now recommanded App Bar, and an answer with it would be even greater.
Here is my activity manifest :
<activity
android:name=".activity.WelcomeActivity"
android:logo="#drawable/ic_launcher"
android:label="#string/long_app_name"
android:launchMode="singleTop"/>
So, is it even possible to remove this annoying useless arrow nowadays ?
Create a new style in styles.xml:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Set this style to your activity in Manifest file. To add the Toolbar, add this to your layout file:
<android.support.v7.widget.Toolbar
android:background="#color/colorPrimary"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
and then in your Java class:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
This will remove the top left arrow.
simply put getSupportActionBar().setDisplayHomeAsUpEnabled(false)in onCreate of the activity.
For an app I'm building, I used the Design Support Library.
I created an app that uses a TabLayout. A little feature of the app is that when the user changes tabs, the color of both the TabLayout and the Toolbar change to a corresponding color. We're also using the new NavigationView, to present the user with a Material design DrawerLayout.
However, changing the color of both the TabLayout and the Toolbar is no problem at all, we do it in the lines of the following;
String hexColor = String.format("#%06X", (0xFFFFFF & getResources().getColor(R.color.primaryColor)));
getBaseActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(hexColor)));
mTabLayout.setBackgroundColor(getResources().getColor(primaryColor));
This is all good, and working like it's supposed to. The problem appears when I try to set the Status bar background. Since the material design specification tells us that the NavigationDrawer should get some special treatment, below image shows how the status bar is actually a form of translucent. The Navigation Drawer falls over my main fragment while the navigation bar is above both, being a translucent bar.
My first thoughts were that it's just a bar with a #7000 hexadecimal value or something, but I couldn't be further from the truth. Doing that doesn't change the color of the actual color it should "darken".
After that I just tried setting the darker version of the required color as the background for the status bar like so;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getBaseActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.primaryColor));
}
But after doing this, the complete functionality breaks. My view will look like this:
In essence, what can I do to fix this behaviour? I'd really like to change it so that the translucency feature still does what it needs to do according to the Material design spec, but also change the status bar's background color.
I think that there is always a translucent shadow behind it and you can not remove it. So you can try to play around with your colors alpha channel to achieve an acceptable result or think about a workaround solution like this:
As far as I see you just want that the status bar color is slightly different when the navigation drawer is open, right? So why not adding just a empty View with the desired color and the same height as statusbar in your NavigationDrawer layout to achieve the same effect?
Btw, there is a R.dimen.status_bar_height height specification.
Activity layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/drawerlayout_main" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
Activty theme, set statusBarColor to transparent
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
NavigationView extends ScrimInsetsFrameLayout, it will draw some color at the statusbar position
I used How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
answer of Suyash (I also added a toolbar, maybe incorrectly) to put Navigation Drawer over the "action bar".
For API level 21 instead of "action bar" I used toolbar, and it works fine.
But for API 19 this is not working:
if(Build.VERSION.SDK_INT > 19) {
final Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
Do you have idea how I can put NavigationDrawer over "actionbar" (or toolbar) for API level 19?
If you use Toolbar then you should be able to view the exact same Toolbar in any API.
For doing that you should have a XML in res/layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"/>
And in your main layout you should include it:
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
Also you should set your style as No Action Bar on your styles.xml
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorAccent">#color/accent</item>
</style>
But for API 21 you should have another styles.xml:
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primaryDark</item>
<item name="android:colorAccent">#color/accent</item>
</style>
And finally in your Main Activity
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
And finally to any thing you want to do to the toolbar, obtain it and treat it like the old Action Bar:
getSupportActionBar().setHomeAsUpEnabled(true);
Material Design for Pre-Lollipop Devices :
All of your themes (that want an Action Bar/Toolbar) must inherit
from Theme.AppCompat. There are variants available, including Light
and NoActionBar.
When inflating anything to be displayed on the action
bar (such as a SpinnerAdapter for list navigation in the toolbar),
make sure you use the action bar’s themed context, retrieved via
getSupportActionBar().getThemedContext().
Android Support Library 22.1 :
AppCompat allows you to use android:theme for Toolbars (deprecating
the app:theme used previously) and, even better, brings android:theme
support to all views on API 11+ devices.
First , you need to add com.android.support:appcombat-v7:25.3.0 as a dependency . Then import android.support.v7.widget.Toolbar to the activity that you want to add the toolbar. With this way you can implement the toolbar for api 19 .
AndroidX
If you don't want to use android support library. Here is a better work through now available for this problem
androidx.appcompat.widget.Toolbar
Migrating to AndroidX Class Mappings available through this official link
Further you can also directly refractor your whole project to AndroidX from Android Studio:
From Menu you can do
Refactor > Migrate to AndroidX
More information here
Further, for me the Refractor > Migrate to AndroidX feature did not convert my Toolbars in xml to AndroidX so, I found former link really helpful.
So I created my app to have tabs on the action bar which direct to three fragment windows. I decided to change the theme of the app to "#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" and suddenely my app started crashing with a nullpointer exception. After sometime I realized that the theme change, which disabled the action bar might have caused this.
Is there a way to implement tabbed layout without an action bar? A custom action bar? I badly want to use that theme. OR is there a way to customize the action bar: change the color or add custom icons and search function to make it more visually appealing?
Thank you!
Is there a way to implement tabbed layout without an action bar?
Use ViewPager and any one of several tabbed indicators, such as PagerTabStrip, the TabPageIndicator from the ViewPagerIndicator library, PagerSlidingTabStrip, etc.
Or, use FragmentTabHost.
is there a way to customize the action bar: change the color or add custom icons and search function to make it more visually appealing?
You are responsible for your own icons, so if you do not like your icons, talk to yourself about having yourself come up with better ones.
You can change the color of the action bar via a custom theme, such as one you might set up with Jeff Gilfelt's Action Bar Style Generator. Or, switch to using the appcompat-v7 edition of the action bar (with ActionBarActivity) and you can use a simpler custom theme where you just set some tint values.
as you have not provided code here i think you are using API 21 or less.. so you should change theme of your android application.
change styles.xml file as follows
<resources>
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>
in my project i'm using AppCompat for my application work in all version of android but this theme of that by default is not good for my application and i want to change that.
my manifest is :
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="21" />
and i dont want to change that. in this below screen shot of application how to change CYAN color in bottom of ActionBarTab and ListView selected items?
or can change parent theme for all changes?
Have a look at the Android Action Bar Style Generator
Have a look at these links :-
Styling Action Bar
Customizing overall app style
Android Action Bar Style Generator