I add toolbar to my layout:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray">
<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>
</android.support.design.widget.AppBarLayout>
and I initialized it in mainActivity:
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
but at my toolbar was appeared a circle at the right of my toolbar! I also have removed logo and title from toolbar but I still see this circle and it wont be disappear!!
What is it? and how can I remove that?
thank you
Given you said that removing the ProgressBar from the layout doesn't hide it, and because of the position, it could be a menu. Are you overriding onCreateOptionsMenu()?
Also, as I said in my comment, try to use "Layout inspector" to find out what this view is. See developer.android.com/studio/debug/layout-inspector.html
Try with programmatic Progress Bar view visible and gone
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setVisibility(View.VISIBLE);
ProgressBar progressBar = (ProgressBar) toolbar.findViewById(R.id.progress_spinner);
progressBar.setVisibility(View.GONE);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Can you please add what you have written in this style ??
#style/AppTheme.AppBarOverlay
Related
I want to insert a centered icon in Actionbar, but i have no idea what code that i need. anyone can help me?
This is my code :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
getSupportActionBar().setTitle("");
Thank you
step 1: create a custom layout in R.layout. directory, as you desire and then set the view
to action bar
step 2: Set the custom layout.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View customView = getLayoutInflater().inflate(R.layout.customActionBar, null);
actionBar.setCustomView(customView );
From your xml add ToolBar as below and set Icon to ImageView inside it.
<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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"/>
</android.support.v7.widget.Toolbar>
Set toolbar as support action bar as you are doing.
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 want to show title of a toolbar in center so i am using custom textview to show title like this :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Title"
android:textColor="#android:color/white"
android:textSize="25sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
Java Code :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView title = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
title.setText("Title");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
Result :
Title text not showing in center properly if I am enabling back button of toolbar.
and things are working fine if back button of toolbar is not enabled i.e.
if
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
Result :
Now , I want to show title in center with Back button enabled of toolbar also. How to do this ? Any help
1) You need to remove this
title.setText("Title");
because already in xml file.
2) Use
getSupportActionBar().setDisplayShowTitleEnabled(false);
3) Set layout gravity for TextView
android:layout_gravity="center"
Edit:
Also change height and width of TextView to wrap_content.
I guess the simplest solution to this question is to change both the width and height of your TextView to wrap_content. Hope it helps.
is it possible to set a subtitle to the CollapsingToolbarLayout like it is possible with a normal toolbar? As far as I know there is no method to do that programmatically. Also, how do I set the white back arrow to the toolbar? Using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
does not display anything, also adding
app:navigationIcon="#drawable/abc_ic_ab_back_mtrl_am_alpha"
to the toolbar doesn't display it either:
For the subtitle question, I had a similar issue and posted the code I used to fix it here:
https://stackoverflow.com/a/31529101/834692
Hopefully you might also find this useful.
For the back icon, you need to set the toolbar first and then call setDisplayHomeAsUpEnabled():
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_white);
There are some ways to have subtitle in a CollapsingToolbarLayout.
Have a TextView inside your CollapsingToolbarLayout, but then only title would be your Toolbar's title when CollapsingToolbarLayout is collapsed, no third-party library is needed.
Use my library: collapsingtoolbarlayout-subtitle.
.
Use it like you would on any CollapsingToolbarLayout, and add subtitle attribute to it:
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout
android:id="#+id/subtitlecollapsingtoolbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:subtitle="CollapsingToolbarLayout"
app:title="Subtitle">
<!-- collapsing toolbar content goes here -->
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"/>
</com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- content goes here -->
</android.support.design.widget.CoordinatorLayout>
Oh and about that back arrow on Toolbar, I usually set it by using ?homeAsUpIndicator in xml:
<android.support.v7.widget.Toolbar
...
app:navigationIcon="?homeAsUpIndicator"/>
Hope it helps.
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"