Android: full-width SearchView in ActionBar - android

I want a SearchView to fully take up the full width of the ActionBar (support v7), but even after calling...
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
... I get a blank space to the left:

I solved it by using a Toolbar and setting the following paramters:
<android.support.v7.widget.Toolbar
android:id="#+id/tbToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingLeft="0dp"
android:background="?attr/colorPrimary"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
Inside that you can put a SearchView or use your toolbar items with a menu.xml which has a toolbarview. See the android developer docs.

Related

Navigation Icon's position in Toolbar

Want to change Navigation Icon Position to 8dp left.
I have attach the screenshot
Black Colour line is the Current Position.
Orange Colour line is the position where, I want my Navigation icon to be placed.
Things I have implemented so far are.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center"
android:background="?attr/colorPrimary"
android:clipToPadding="false"
android:gravity="center|start"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:navigationIcon="#drawable/arrow"/>
and
if (actionBar != null) {
// Tried Both
actionBar.setHomeButtonEnabled(true); & actionBar.setHomeButtonEnabled(false);
toolbar.setContentInsetsAbsolute(0, 0);
}
please try adding
android:paddingLeft="-8dp"
or
android:layout_marginLeft="-8dp"
this will work.

How to put tablayout into toolbar and they are in the same line in android developer

I want this effect.The tab is in the toolbar,not under the toolbar
I searched all of the website but can't find a way to do that thing.All document tells me how to add a tab under the toolbar but it's not what i want
I just came up with the answer, and it's simpler than I thougth:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<android.support.design.widget.TabLayout
android:id="#+id/main_tabs"
style="#style/MainTabLayout"
app:tabIndicatorHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/home_toolbar"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
So there we have.
It's not the exactly same design as yours, but you just change whatever you want.
And don't forget to set your toolbar on your fragment or activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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