Center icon in Actionbar - android

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.

Related

what is the circle at the right of my toolbar

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

Toolbar title not showing in Center properly if Back button is enabled

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.

android -how to remove margin from top and bottom of custom actionbar

I'm using a custom actionbar for my application . this is the code :
actionBar.setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setIcon(null);
actionBar.setCustomView(mCustomView);
Toolbar parent =(Toolbar) mCustomView.getParent();
parent.setContentInsetsAbsolute(0, 0);
this is the result of this code , I 've margins from top and bottom :
How can I remove this margin ?
i'm using Theme.AppCompat.Light for my application template .
It is recommended to put the Toolbar layout xml in a separate file:
<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/actionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" >
Then include it in your main.xml
<include layout="#layout/view_action_bar" />
Also make sure to : app:contentInsetStart="0dp" this is the main issue for your magins.
In your ActionBar layout file give the android:heigth as match parent for the Parent View. I also had a same issue with it.

Android ActionBar's custom view not filling parent [duplicate]

This question already has answers here:
Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width
(8 answers)
Closed 5 years ago.
Similar to the question here, but has some key differences. Most notably being that the accepted answer did work until the latest support library releases.
Here's the custom view layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFDD0000">
</LinearLayout>
Now, when you try to set just the custom view:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.actionbar);
You end up with a custom layout that doesn't fill the entire width:
To make this more interested, I have a similar setup in a different app:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
This did work, until I updated the support libraries to v21. Once I did, I ended up with the same issue on apps that previously haven't had that issue.
So, my question is, is there a way to make a custom view actionbar fill the width using the latest support library?
Edit After doing some more tests, I found that compiling with targetSdkVersion/compileSdkVersion set to 19 and having v7:19.0.1 (or v7:19.1.0) for the library doesn't have this issue (has the home icon, which is taken care of with the later code), confirming that this is definitely an issue with the latest release.
1
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View view = getLayoutInflater().inflate(R.layout.actionbar_title_back,
null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
actionBar.setCustomView(view, layoutParams);
Toolbar parent = (Toolbar) view.getParent();
parent.setContentInsetsAbsolute(0, 0);
2 Use Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
XML
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:popupBackground="#color/red"
app:popupTheme="#style/PopupTheme"
app:theme="#style/ToolbarTheme" >
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar Title"
android:textColor="#color/white"
android:textSize="17.0sp" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
use
Toolbar parent = (Toolbar) customNav.getParent();
parent.setContentInsetsAbsolute(0, 0);
it will work
add like this in your onCreate method
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
toolbar.setContentInsetsAbsolute(0, 0);
toolbar.getContentInsetEnd();
toolbar.setPadding(0, 0, 0, 0);
In your XML code add these attributes:
<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/re_app_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp">
it will fit all width of the view.

appcompat-v7: Custom view not properly aligned in ActionBar

I am trying to use Appcompat Toolbar based actionBar
Here is my toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingBottom="0dp"
android:background="?attr/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
I am including this in my activity.xml file.
And then in my Activity's OnCreate method, I am setting a custom PagerStrip into ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.pager_strip);
actionBar.setDisplayShowCustomEnabled(true);
tabs = (PagerSlidingTabStrip) actionBar.getCustomView().findViewById(R.id.tabs_strip);
tabs.setViewPager(mPager);
There is some padding below my PagerStrip in ActionBar. I want to remove this padding.
here is a picture showing the issue.
This was working fine with ActionBarSherlock
I had a similar problem: I have migrated to the Toolbar pattern:
<?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_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<my.custom.widget.class
android:id="#+id/tab_bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
</my.custom.widget.class>
</android.support.v7.widget.Toolbar>
but the AppCompat gave me a strange padding around my custom view:
My fix:
Add app:contentInsetStart="0dp" and app:contentInsetEnd="0dp" to the Toolbar attributes; and android:minHeight="?attr/actionBarSize" to the custom widget attributes.
Result:
Not sure that the solution follows the Material design guidelines, but hope it will help to someone.
Toolbar is widget replacement for Actionbar in some cases.
If you want to use Toolbar then you can try:
Toolbar=(Toolbar)findViewbyId(R.id.toolbar);
setSupportActionbar(toolbar);
I used Toolbar in DrawerLayout. My navigation drawer can always Top Toolbar.
P/S: use toolbar in ActionBarActivity

Categories

Resources