show icon in actionbar/toolbar with AppCompat-v7 21 - android

I tried these - but still do not see the icon like before:
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
It seems to work when I use custom toolbar - but that would force me to touch all layouts - is there a better way to do so?

getSupportActionBar().setDisplayShowHomeEnabled(true);
along with
getSupportActionBar().setIcon(R.drawable.ic_launcher);

In modern Android UIs developers should lean more on a visually
distinct color scheme for toolbars than on their application icon. The
use of application icon plus title as a standard layout is discouraged
on API 21 devices and newer.
If you disagree you can try with:
To create the toolbar in XML:
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
In your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
Use the setLogo() method to set the icon.
Code source.

This worked for me:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_logo);
getSupportActionBar().setDisplayShowTitleEnabled(false); //optional
as well as:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_logo); //also displays wide logo
getSupportActionBar().setDisplayShowTitleEnabled(false); //optional

simplest thing to do; just add:
app:navigationIcon="#drawable/ic_action_navigation_menu">
to the <android.support.v7.widget.Toolbar tag
where #drawable/ic_action_navigation_menu is the name of icon

A better way for setting multiple options:
setIcon/setLogo method will only work if you have set DisplayOptions Try this -
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
You can also set options for displaying LOGO(just add constant ActionBar.DISPLAY_USE_LOGO). More information - displayOptions

Try using:
ActionBar ab = getSupportActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setLogo(R.drawable.ic_launcher);

For Actionbar:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
For Toolbar:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);

if you want to set the home or back icon (not logo or static icon) so you can use
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator( getResources().getDrawable(R.drawable.home) );

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
OR make a XML layout call the tool_bar.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:src="#drawable/ic_action_search"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Now in you main activity add this line
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />

If you dont want to set your toolbar as action bar using setSupportActionBar, you can add a logo next to your navigation icon (if you have a back button for example) like this:
toolbar.setLogo();
or in xml
<android.support.v7.widget.Toolbar
....
android:logo="#drawable/logo"
app:logo="#drawable/logo"/>
And even if you have a title set on the Toolbar, the the title will still show.
Ex: The green check in the image below is the logo

Try this:
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
...
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_launcher);
so your icon will be used for Home / back
or
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
...
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayShowHomeEnabled(true);
actionbar.setIcon(R.drawable.ic_launcher);
for static icon

toolbar.setLogo(resize(logo, (int) Float.parseFloat(mContext.getResources().getDimension(R.dimen._120sdp) + ""), (int) Float.parseFloat(mContext.getResources().getDimension(R.dimen._35sdp) + "")));
public Drawable resize(Drawable image, int width, int height)
{
Bitmap b = ((BitmapDrawable) image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, width, height, false);
return new BitmapDrawable(getResources(), bitmapResized);
}

In Xamarin.Android you can use these:
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayUseLogoEnabled(true);
SupportActionBar.SetIcon(Resource.Drawable.ic_launcher);
SupportActionBar.SetDisplayShowTitleEnabled(false);
using Android.Support.V7.App.AppCompatActivity is required.

Try this. For me it worked
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);

In Kotlin I did the following to just show the icon:
supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setIcon(R.drawable.ic_icon_small)

Related

How to fix Android Navigation Icon Size Problem

I'm trying to create a custom toolbar. I changed the navigation icon using setNavigationIcon but the svg vector I provided doesn't look right. Navigation icon does not automatically adjust the size of svg itself? How can I fix this problem?
Here is a example image left side menu icon not display correctly:
Here main_activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_icon);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
Custom toolbar code :
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
>
</androidx.appcompat.widget.Toolbar>

Add Image To Navigation Drawer Toolbar and Set Custom Background

I need to add an image to the navigation drawer toolbar/actionbar. A small round image that appears on the right of the menu icon and left of the activity title.
I also need to change the background of the toolbar/actionbar to my own custom image.
Is this possible and if yes how do I accomplish this in my android app?
For anyone confused, OP is using Android Studio's NavigationView Activity and not an Empty Activity.
Image to the right of the Hamburger menu icon and to the left of the Title is achieved like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false); // hide built-in Title
// Setting background using a drawable
Drawable toolbarBackground = getResources().getDrawable(R.drawable.ic_menu_gallery);
getSupportActionBar().setBackgroundDrawable(toolbarBackground);
}
Now that we've hidden the title, we're going to add our own title TextView and icon ImageView to the toolbar, this would be your app_bar_main.xml layout (res/layout).
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/my_custom_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_share"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Custom Title"
android:id="#+id/my_custom_title"
android:layout_toRightOf="#id/menu_icon"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Now, you can reference my_custom_title and my_custom_icon and add whatever title and icon you want (you may have to tinker with the layout, I'm just pointing you in the right direction).
TextView myCustomTitleTV = (TextView) findViewById(R.id.my_custom_title);
myCustomTitleTV.setText("My Custom Title");

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

Manipulating the Toolbar's title and other elements

I'm using the AppCompat toolbar, and for the most part its great. However, dealing with the title has been a nightmare. It's set to the app name by default, and I cannot find a way to hide it.
I was trying to hide it so that I can add my own textview so that I could properly align the title text to the top currently its left-aligned but vertically centered. I could not figure out how to position it where it would be normally if I didn't make my toolbar height longer than usual.
How have you guys been managing the toolbar? How can I align the title text to be at the top position where the title's normally are?
Edit: I've added a screenshot of what it looks like. I'm trying to keep my project private for now so I erased any identifying elements. But notice that the D (which is the title) is not aligned to the top.
If you have set your toolbar as action bar setSupportActionBar(toolbar);
then you can sipmly disable the title by using
getSupportActionBar().setDisplayShowTitleEnabled(false);
and add a textview as your Title in your toolbar layout as toolbar is also a view. See following
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/material_blue"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/ToolbarCustomIconColor" >
<TextView
android:id="#+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="#string/app_name"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
This way you can arrange your title anywhere and customize accordingly.
You are using ToolBar from support v7 AppCompat
after that set that toolbar as action bar and then set action bar title for simple title or you want to set your custom layout inside toolbar then same as we are doing in action bar hence.. now your toolbar work as a actionbar..
See below example from ActionBarActivity..
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Your Title");
Now if you do anything with action bar related methods it is ultimately affected in ToolBar.
to change Toolbar title try this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Text");
for custom layout for Toolbar try this
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar); //replace with your custom toolbar title

Toolbar - add the up button

I am trying to use the Toolbar instead of the ActionBar, but I can't figure out how to add the up button to return to the previous activity.
I couldn't find any method that could relate to it.
How do I add the up button?
I guess what you are looking for is something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_detail);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Or in case of using in Fragment:
Toolbar toolbar = (Toolbar) view.findViewById(R.id.app_bar_detail);
((ActionBarActivity) getActivity()).setSupportActionBar(toolbar);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This will show up your Action Bar inside of your toolbar, but don't worry everything will fit together well. The last you have to do if you dont want any shadow under your action bar or any background of it is change your theme in vaules/styles.xml.
<style name="AppThmeme.Base" parent="Theme.AppCompat.NoActionBar">
If you want to do this in XML, you can use...
<android.support.v7.widget.Toolbar
app:navigationIcon="?homeAsUpIndicator"
...
If you're wondering why clicking on up button doesn't work with fragments, you need to set up a navigation listener as well, not sure why Google hasn't enabled it by default:
protected fun setupToolbar(toolbar: Toolbar) {
(activity as AppCompatActivity).run {
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener { onBackPressed() }
}
}
In case when previous activity is always same for a given activity then up/back button can be achieved with the help of parentActivityName attribute. It can be mentioned in AndroidManifest.xml file as shown below:
<activity android:name=".DetailActivity" android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Let's say DetailActivity was opened from MainActivity. So when you are on DetailActivity then tool bar will automatically show a left pointing arrow (Refer screenshot):
When we click on left pointing arrow then MainActivity gets displayed.
Calling getSupportActionBar().setHomeButtonEnabled(true); should still work I think, as long as you have already called setSupportActionBar(toolbar);
You can add your own 'up' button in toolbar, after all it is just a ViewGroup.
You can customize toolbar as much as you want, in your toolbar.xml, or wherever you have defined android.support.v7.widget.Toolbar in your layout add your 'up' button like given below :
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/color_toolbar"
android:layout_width="match_parent">
<ImageButton
android:id="#+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="uphandler"
android:src="#drawable/backbutton"
android:layout_gravity="end"/>
</android.support.v7.widget.Toolbar>
Now, define uphandler function in your activity to listen to this up button :
public void uphandler(View v){
this.finish(); // This will kill current activity, and if previous activity is still opened in background, it will come in front.
}
You have to add these line back button show automatically
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
on back, button click automatically back to activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
if you are using navigation component, then adding up button in fragment would be like this:
in your fragment.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.recipeitem.RecipeDetailsFragment">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
android:elevation="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
.
.
then in your fragment you would do this
inside onViewCreated(..)
val navController = findNavController()
val appBarConfiguration = AppBarConfiguration(navController.graph)
viewBinding.toolbar.setupWithNavController(navController, appBarConfiguration)
if you need to add title then use this in onResume
viewBinding.toolbar.title = "my title"
if you are using newer version of android studio:
first declare toolbar of androidx not android
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent"
app:popupTheme="#style/Theme.Sunshine.PopupOverlay" />
second, get a reference to the toolbar in Activity.java and use following code;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar ab= getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);

Categories

Resources