I know you can add an icon image to the app bar, however I was wondering if there is a way to put a larger logo image in the app bar on Android. The picture attached has it behind the app bar but I'm trying to get it inside the app bar. Is this possible?
1) Use latest Android Toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Place your ImageView and Image-->
<!-- mention image size-->
</android.support.v7.widget.Toolbar>
Example code:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:id="#+id/back_menu_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_logo"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#color/title_first_textcolor"
android:textSize="#dimen/abc_text_size_title_material"
android:gravity="left"
/>
</android.support.v7.widget.Toolbar>
2) You can set logo programmatically:
Toolbar tool_bar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(tool_bar);
tool_bar.setLogo(R.drawable.image);
For more information about Toolbar, check this link: http://developer.android.com/reference/android/widget/Toolbar.html
For the latest API xml should be,
<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" />
Activity should be,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setLogo(R.drawable.ic_call_black_24dp);
setSupportActionBar(toolbar);
}
As your image is large and rectangle you can set it in background of your action bar :
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));
actionBar.setBackgroundDrawable(background);
For this you can create one activity say "BaseActivity" and add snippet in onCreate method of it and extend that activity in your all activity where you want to show your change
Or you can have your different square logo like app icon and set it to your action bar icon.
public BaseActivity extends Activity{
#override
public void onCreate(Bundle bundle){
/// add snippet
}
}
yourActivity/yourActivities extends BaseActivity
You need to add imageview inside the toolbar
Related
I am making a Chat Application and so wanted to add both icon as well as back button on my actionbar() in chat activity (like it is in WhatsApp) but I'm not able to set both things...
here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
initControls();
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
Intent intent = getIntent();
String itemname = intent.getStringExtra("itemname");
actionBar.setTitle(itemname);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
}
The problem is that both the things are not getting set at the same time with the above code only back button shows up and if I remove code for back button then icon shows up... How to set both things???
You should not use the default action bar, make your custom toolbar and add an imageview in it.
Something like this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageToolBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginTop="3dp"
android:src="#drawable/profile"
/>
<TextView
android:id="#+id/toolbarText"
style="#style/TextToolBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="#id/imageToolBar"
android:layout_toRightOf="#+id/imageToolBar"
android:text="Your text"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Then in your java file do this:
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView toolbarText = (TextView)toolbar.findViewById(R.id.toolbarText);
ImageView imageView = (ImageView)toolbar.findViewById(R.id.imageToolBar);
//Do your stuff here like set image on imageView
Happy Coding..:)
If you want exactly like WhatsApp then you need to make a custom drawable having both back button and Profile image in same drawable image using Bitmap and Canvas. Then set this drawable image as HomeAsUpIndicator
getSupportActionBar().setHomeAsUpIndicator(R.drawable.custom_back);
I am new in android How achieve custom action bar like given image.
enter image description here
please help me for making this one.
You can simply use a Toolbar for the action bar.
The toolbar is a ViewGroup which you can add any custom view to it.
example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<YourViewHere/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
and in your activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Find the toolbar view inside the activity layout
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Sets the Toolbar to act as the ActionBar for this Activity window.
// Make sure the toolbar exists in the activity and is not null
setSupportActionBar(toolbar);
}
I'm trying to display my toolbar title in the center and to do it I use the method which is given in this answer :-Toolbar Center title
However, when I enable back button in my activity by following code:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
The title of toolbar doesn't show up in the center but slightly off-centered towards the right.
How can I achieve centered title without being affected by the back button or menu bar?
Add a TextView inside the Toolbar & don't forget to set the following attribute inside your TextView.
android:layout_marginRight="?android:attr/actionBarSize"
OR
android:layout_marginEnd="?android:attr/actionBarSize"
code snippet:
<android.support.v7.widget.Toolbar
android:id="#+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#android:color/holo_red_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_marginRight="?android:attr/actionBarSize"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
Refer to this tutorial for more information.
Having a placeholder image the same size as the back arrow and setting it to be invisible when the back arrow is not shown and gone when it's displayed did the trick for me.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:id="#+id/iv_placeholder"
android:layout_width="72dp"
android:layout_height="48dp"
android:src="#drawable/ic_actionbar_hamburger"
android:visibility="invisible"/>
<TextView
android:id="#+id/logo_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="?attr/actionBarSize"
android:gravity="center"
android:text=""
android:textColor="#color/white"
android:textStyle="normal"/>
</android.support.v7.widget.Toolbar>
Just add android:paddingEnd="72dp; to the Toolbar layout.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:contentScrim="#color/colorPrimary"
app:layout_collapseMode="pin"
android:paddingEnd="72dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:title="Title"/>
Just put your content in a child view inside the Toolbar tag in XML, using the following attributes:
android:layout_width="wrap_content"
android:layout_gravity="center"
Offical docs for Toolbar state:
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's LayoutParams indicates a Gravity value of Gravity#CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
This works for me, using androidx.appcompat.widget.Toolbar with a child view.
The reason why the title is not centered when you use a back button as navigation icon, is that navigation icon is represented as AppCompatImageButton and is added to the same layout as your title TextView. Using Arshak's answer is not a bad idea, but ?android:attr/actionBarSize is not a good way to define the end margin. As the action bar height is probably the same size as icon's width, it might work, but might not work on all devices. Could be a good idea to specify this size from material design guidelines.
In my case I was using an imageview inside the toolbar which I didnt want shifting around while navigating between fragments of a activity.
I kept it centered by placing it out the toolbar. I used constraintlayouts
<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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.material.appbar.MaterialToolbar>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/toolbar"
app:layout_constraintBottom_toBottomOf="#id/toolbar"
android:src="#drawable/ic_logo"
tools:ignore="ContentDescription" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
I think the nicest and most up-to-date method is to have full control over the appbar. This way you can change other things from textview location.
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="#string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="20dp"
android:textColor="?attr/colorPrimary"
android:layout_centerInParent="true"
android:layout_marginEnd="20dp"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
You can use this directly inside your activity. However, you may need to change the active toolbar by making such a definition in the activity where you added this toolbar.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Dont set propterties like this
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
Do like this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish(); // to go back finish() will do your work.
//mActionBar.setDisplayHomeAsUpEnabled(true);
//mActionBar.setDisplayShowHomeEnabled(true);
}
});
I have an appCompatActivity with a supportActionBar similar to whatsApp chat screen interface. Having been able to customise the actionbar with all the necessary components, I am not not able to apply padding/margin of any sort on the leftmost up/back button. However, with rest of the items I set up in the toolbar are aligned properly.
Here is how my layout of the activity looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/avatar"
android:layout_width="#dimen/action_bar_avatar_size"
android:layout_height="#dimen/action_bar_avatar_size"/>
<TextView
android:id="#+id/txtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
...
In the activity:
mImageViewAvatar = (ImageView) findViewById(R.id.avatar);
mTextViewUserName = (TextView) findViewById(R.id.txtUserName);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mImageViewAvatar.setBackgroundImage(Contacts.getImage());
mTextViewUserName.setText(recipientId);
I have tried to set my layout_marginLeft to a negative value as well to the image even, it does not move to left. How do I apply margin/padding alignments to only this toolbar? Not the toolbar used in the application.
I guess you're looking for app:contentInsetLeft respectively app:contentInsetStart. Setting these attributes to 0dp will remove the padding - see the following two screenshots:
Without explicity setting contentInset:
Setting contentInset to 0dp
Please note: This won't work if you're using getSupportActionBar().setDisplayHomeAsUpEnabled(true) since the drawable which will be used by the system has a padding which can't be removed. See the following Screenshot:
So if you're trying to achieve the same as WhatsApp you have to use your own "back-button" drawable and add it to your Toolbar layout.
Edit
That's how I would do it:
*.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:id="#+id/layout_back_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_arrow_back" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/avatar" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="StackOverflow"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="20dp" />
</android.support.v7.widget.Toolbar>
Java code
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
getSupportActionBar().setTitle(null);
View view = findViewById(R.id.layout_back_button);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
Result:
Notice that I wrapped the back button drawable and the avatar in an extra layout, which has the selectableItemBackgroundBorderless set as the background. Due that we achieve this ripple effect like in WhatsApp.
I have an xml that I use with so many activities with fragments file but my problem is that I can't display the text I want in the toolbar, I use that xml that way because I have a navigation drawer and I needed to handle somethings so I had to do it that way.
my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StatusActivity"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material" />
</RelativeLayout>
One of my activities:
public class GroupChatActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Groups history");
Aniways.init(this);
if(savedInstanceState == null)
{
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0));
manager.beginTransaction().add(R.id.frame_container, fragment).commit();
}
}
}
as you can see I try to set title to the action bar but it doesn't work.
getSupportActionBar().setDisplayShowTitleEnabled(true);
Setting,
app:title="#string/my_title"
within the declaration of the the android.support.v7.widget.Toolbar, hard codes the title in the toolbar.
To set the title programatically,
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setTitle("my title");
setSupportActionBar(toolbar);
in your activity class.
Try this .. this method works for me..!! hope it may help somebody..!!
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:singleLine="true"
android:text="Toolbar Title"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
EDIT
You can also use this.
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setTitle("Toolbar title");
I spent about a day looking for the cause of the issue. Neither supportActionBar?.title = "SomeTitle" nor supportActionBar?.setDisplayShowTitleEnabled(true) did not work, nor hacks with custom toolbars from answers here looked good.
My activity is using CollapsingToolbarLayout but not displaying any title, nor label from manifest, nor dynamically set one. But the sample ScrollingActivity (New - Activity - ...) displayed the title.
Finally I set up a sample project, copied MyActivity and ScrollingActivity and looked through the diff.
layout_height both of the AppBarLayout and CollapsingToolbarLayout must be set to match_parent or fixed size. Thats all. See working code below.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="180dp"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#id/toolbar"
app:contentScrim="?attr/colorPrimary"
>
<androidx.appcompat.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"
app:layout_collapseMode="pin"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
Try toolbar.setTitle('Groups history');
getActionBar().setTitle("Groups History");
or if you are using AppCompat libraries;
getSupportActionBar().setTitle("Groups History");
I did a custom action bar.
Layout iguepardos_action_bar.xml with this code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blanco"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:singleLine="true"
android:text="Toolbar Title"
android:textColor="#color/naranja"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
In my Class extended AppCompatActivity I had this:
protected void onCreate(Bundle savedInstanceState) {
....
....
getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar); // my custom layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back
View mCustomView = getSupportActionBar().getCustomView(); // Set the view
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control
TitleToolBar.setText("The Title Show"); // Set the Title
}
I actually had to get the toolbar_title to set the text into each different activity:
toolbar = findViewById(R.id.toolbar);
toolbarTitle = findViewById(R.id.toolbar_title); //<----- here
toolbarTitle.setText(getString(R.string.my_activity_toolbar_title));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if your title color is white and the toolbar is also white, you will not differentiate it. So try to change the toolbar color
Given below code worked me:
Toolbar myToolBar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);
getSupportActionBar().setTitle(getString(R.string.toolbar_title_note));
First I tried with getSupportActionBar().setDisplayShowTitleEnabled(true); and then without it. It worked for both of the cases.
These are gradle settings for my project:
minSdkVersion 16
targetSdkVersion 30
compileSdkVersion 30
buildToolsVersion "30.0.3"