Is it possible remove home button from actionbar - android

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.

Related

Changing navigation back button icon dynamically doesn't work

I need to set navigation back button dynamically
when I do this `
viewBinding.toolbar.navigationIcon?.setColorFilter(Color.parseColor(theme.primaryTextColor), PorterDuff.Mode.MULTIPLY)
it doesn't effect changing color or if I do
viewBinding.toolbar.setNavigationIcon(R.drawable.ic_back) in MainActivity
where ic_back is white back button it doesn't work it shows back button
or if I add this app:navigationIcon="#drawable/ic_back" in xml it doesn't work
but when I set app:theme="#style/ToolbarColoredBackArrow"
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
it works why? I should do this without using theme
If the toolbar is your support action bar, then this definitely works :
toolbar.setNavigationIcon(R.drawable.ic_back);
Or whatever you want to set the icon to, just that it has to be a drawable file.

Android API 15. I have toolbars in my activites but they are not shown in XML

Very Novice Android Programmer here. I have this small program where i have my main activity and whenever the user clicks an option, a new activity will open. I am trying to change the color of the top toolbar/actionbar for each different activity. I have tried changing the color through Java code within the activity class in the onCreate() method,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.gradient));
but the program would always crash whenever I would switch to my activity.
I have looked in the XML file of my activity and the toolbar/action code does not show up anywhere, but it does in my main activity, app_bar_main.xml. I'm wondering why the actionbar shows up in activies if it does not show up in the XML file for the activity. How to change the color of the actionbar for newely added non main activities?
I'm guessing you're using app theme with action bar. In res/styles.xml change your theme to NoActionBar version:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Now ActionBar shouldn't be visible. Add your Toolbar XML code to other activity layouts.
Try this, it will work:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

Default title bar displayed before my custom title bar appears

I' developing an android app and I need to use my custom title bar using a picture and two buttons. The thing is, immediately when I launch my app, during 1 or 2 seconds before my custom title bar appears, there is the ugly default one with "my application" displayed. The minimum targeted API is 15.
All the answers found on stack overflow didn't work, or succeed to make it disappear but was doing the same to my custom title bar.
Here is how I call it from my activity:
supportRequestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.topbarclassic);
Since my first view is a fragment I dont call SetContentView
And this is my custom styles.xml:
<resources>
<style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowNoTitle">false</item>
</style>
</resources>
Once again my custom title bar works properly. I just need to get rid of the default one displayed quickly when the app starts. Thanks a lot!
if you use this style the activity will load without an action bar
<style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then you should really be using a toolbar to set the action bar. For example :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
</RelativeLayout>
Then in your activity you can set the action bar like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Dont forget to set your them in your AndroidManifest.xml
<activity
android:name=".path.MyActivity"
android:theme="#style/theme"/>
If found a workaround for my problem using Modge's link about splashscreen.
It doesn't solve the problem itself but remains a good workaround.
I created a small activity in charge of the splash screen, avoiding the white first screen to stay for too long. Then this splash redirect on my main activity.
This is also useful in my case since I can start some connection process during the splash screen. You can follow this example: http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/

ActionBar displays only text

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.

How to align the actionbar tabs to the right?

I added the action bar tabs programmatically. I do not know how to align the action bar tabs to the right.
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// instantiate tabs
ActionBar.Tab pageTab= bar.newTab().setText(getString(R.string.page))
.setIcon(R.drawable.ic_page);
bar.addTab(pageTab);
// other settings
bar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayShowHomeEnabled(true);
// remove the activity title to make space for tabs
bar.setDisplayShowTitleEnabled(false);
Action bar tabs appear on the left. The right is used for your options menu and any items from it that you promote to be toolbar buttons.
This has been a while since the original question, but... I needed a solution for the same problem due to visual appearance of the Activity. It turns out that if you append white spaces to either the title or subtitle, the tabs will be moved to the right, so you can do this:
bar.setTitle("Some titile\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
This will shift the whole tab bar to accommodate the space for the white spaces. I must say that this is not an elegant solution but works if you don't have any actions in the action bar. (I didn't check what happens when actions are present.)
I was able to align the actionbar right by customizing my application theme
In my AndroidManifest.xml, I added a android:theme property:
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/CustomActionBarTheme">
...
I then added a new themes.xml file to my project, and added :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarTabBarStyle">#style/MyActionBarTabBar</item>
</style>
<style name="MyActionBarTabBar">
<item name="android:gravity">right</item>
</style>
</resources>
The tabs should now be right aligned.
EDIT:
It works only if your actionbar is displayed on 2 lines (in portrait orientation)

Categories

Resources