Android TabLayout tabPaddingTop and tabPaddingBottom not being removed
Please refer to the above issue as well.
Even since i updated my design library to "23.2.0", Tab layout is all messed up.
The below image is my Tab Layout.
Xml Part :-
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white"
app:tabIndicatorHeight="#dimen/dp2"
app:tabMode="fixed"
app:tabSelectedTextColor="#android:color/white"
app:tabTextAppearance="#style/MyCustomTabTextAppearance" />
styles xml :-
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/color_156084</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">#dimen/sp14</item>
<item name="android:textColor">#android:color/white</item>
<item name="textAllCaps">false</item>
</style>
I have set padding to -1dp and even did tabGravity to fill but nothing is working.
This code used to work in earlier versions but now if i am downgrading it, i am getting a no class def found error on TintManager.
Setting different values or layout params did not work, so the only solution I got was to add the following, after you add the tabs to your tab layout,
final ViewGroup test = (ViewGroup)(tabs.getChildAt(0));//tabs is your Tablayout
int tabLen = test.getChildCount();
for (int i = 0; i < tabLen; i++) {
View v = test.getChildAt(i);
v.setPadding(0, 0, 0, 0);
}
Try adding below attributes to TabLayout:
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
Hope it'll work.
Instead of dealing with TabLayout subviews, you can access TabLayout.Tab view directly and set the padding to 0 just like the code below.
for (int i = 0; i < tabLayout.getTabCount(); i++) {
View tabView = tabLayout.getTabAt(i).view;
tabView.setPadding(0, 0, 0, 0);
}
I'm doing this in the onCreateView callback and it works perfectly.
Just pass your tabLayout to this method and it's done!
private void setTabLayoutMatchParent(TabLayout tabLayout) {
final ViewGroup tabLayoutChild = (ViewGroup)(tabLayout.getChildAt(0));
int tabLen = tabLayoutChild.getChildCount();
for (int j = 0; j < tabLen; j++) {
View v = tabLayoutChild.getChildAt(j);
v.setPadding(0, 0, 0, 0);
}
}
I was stuck for hours until I found the method tab.setCustomView(idRes). Changing from inflated to this worked for me. Alernatively, if you are inflating you can use TabLayout.TabView as root viewgroup.
Apart from this, I am not sure if it is helpful but I used minWidth to the custom view layout.
This is what I wanted
There are attributes in TabLayout : app:tabPaddingStart and app:tabPaddingEnd
You can set -1 to both of them in order to remove padding start and end in custom view of TabLayout.
<com.google.android.material.tabs.TabLayout
android:id="#+id/tlEmojis"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:tabMaxWidth="#dimen/dimen_48dp"
app:tabMode="scrollable" />
Try adding TabLayout in LinearLayout like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF" />
</LinearLayout>
In Styles.xml add:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
In my case the problem was giving fixed height to the custom layout that i use for tabs.
Using match_parent fixed my issue.
Related
I have a LinearLayout with that has multiple TextViews and want to set up a default global color for that layout only without having to add a textColor field inside each TextView. Also, if it's possible, would it also be possible to override the color value by adding it inside the TextView? i.e. If I set blue as a default color and black for a single TextView, would the blue change to black?
To set the default global TextView colors, first you can create your own theme in AndroidManifest.xml file for the following items:
textColorPrimary - for Large texts
textColorSecondary - for Medium texts
textColorTertiary - for Small texts
textColorHint - for Hint texts
For example, in AndroidManifest.xml:
<style name="TextViewTheme" parent="android:Widget.TextView">
<!-- Set the default global color for TextViews to Holo Blue Dark -->
<item name="android:textColorPrimary">#android:color/holo_blue_dark</item>
<item name="android:textColorSecondary">#android:color/holo_blue_dark</item>
<item name="android:textColorTertiary">#android:color/holo_blue_dark</item>
<item name="android:textColorHint">#android:color/holo_blue_dark</item>
</style>
Next, set the theme style on your LinearLayout. You can also override the default for a single TextView to black color, like the following which set the first TextView Hint text color to black in activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/TextViewTheme">
<TextView
android:id="#+id/phone_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/phone_tv"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black" />
<TextView
android:id="#+id/email_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/email_tv" />
</LinearLayout>
Hope this helps!
You can override default text colors for the entire application by setting textColorPrimary and textColorSecondary in your parent in styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="textColorPrimary">#color/black</item>
<item name="textColorSecondary">#color/grey</item>
</style>
If your TextViews are indeed very many to the extent that calling setTextColor() on each of them would be a herculean task, why not use a view that supports an adapter (i.e ListView, RecyclerView etc). Your TextViews would show up the exact same way as you intend them to appear with the LinearLayout.
While using an adapter, you can set up a model TextView layout and set a global textColor for all the TextViews. You can override this global textcolor in your adapter by using simple if and else statements.
I hope this helps.. Merry coding!
This code will work even if you add or remove TextViews from your layout. Just put it in your activity's onCreate();
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTextColor(Color.BLACK);
}
}
change the color to what you like.
If you want after this code you can change the color for any specific TextView.
Pretty late answer but this does the trick.
Inside style.xml lately changed to themes.xml
<style name="ErrorStyle">
<item name="android:textColor">#FF0000</item>
</style>
Then inside your xml layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ErrorStyle">
.. All of your TextView
</LinearLayout>
Using this approach we can use other xml variation such as night mode, etc... and we still have te possibility to override the internal TextViews.
I am trying to resize notification tab at the start of tab layout as in image,
Till now I am able to bring all three tabs as in image except the width. My progress till now is as follow,
So my question is, how do I resize individual items in a tab layout?
I searched on many pages and tried reading the documentation but didn't find anything useful.
Thank you in advance.
This is late but better late than never. Also I can't see how Parth Patel answer is even related to this question.
Have a look at this, you will find what you are looking for.
LinearLayout layout = ((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(YOUR_TAB_NUMBER));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.weight = YOUR_WEIGHT; // e.g. 0.5f
layout.setLayoutParams(layoutParams);
You can use try this TabLayout.
layout.xml file
<android.support.design.widget.TabLayout
android:id="#+id/tabCauses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white"
app:tabTextAppearance="#style/MyCustomTextAppearance" />
style.xml file
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:background">#FF26221E</item>
<item name="android:fontFamily">#font/montserrat_semibold_1</item>
</style>
I am working on tabs like the youtube app and would like to highlight the first page on load of the activity so i used the following code
viewpager.setCurrentItem(0);//which does not highlight first tab onstarting the tab activity
And i used drawable as background for the icons in tablayout . I set the background of drawable on create of fragment by getting each child of tablayout . I set the text color in
<item name="tabTextAppearance">#style/TabTextAppearance</item>
</style>
<style name="TabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">#color/tab_text_selector</item>
<item name="android:textAllCaps">false</item>
</style>
One of the selector files
< ?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/favorite_selected" />
<item android:state_focused="true" android:drawable="#drawable/favorite_selected" />
<item android:state_pressed="true" android:drawable="#drawable/favorite_selected" />
<item android:drawable="#drawable/favorites" />
</selector>
There are two other drawables and i am using custom layout for tabs
How can i show the first item highlighted when the app loads in view pager
It's a bit tricky.
You should set background in this way:
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tabLayout"
app:tabBackground="#drawable/tab_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I.e. change android:background to app:tabBackground
Now once you press on the tab - it get's proper background drawable.
To apply selector to icons in TabLayout:
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_i_selector);
}
UPD:
After going back and forth, together with #Preethi we've solved it:
tabLayout.getTabAt(0).getCustomView().setSelected(true);
Important note. #Preethi is using setCustomView() method to set the Views for the Tabs.
Another possible workaround would be to avoid custom views and set TextColor&Icons this way:
tabLayout.setTabTextColors(getColor(R.color.normal_color), getColor(R.color.selected_color));
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.icon_selector);
}
Are you trying to select the first item inside the viewpager, or the first tab?
For the latter, try
tabLayout.getTabAt(0).select();
instead of
viewpager.setCurrentItem(0);
I have set android:textAllCaps="false" in my android.support.design.widget.TabLayout thought it is showing the Tab Title in All caps only.
How can I remove all caps?
UPDATE FOR DESIGN LIBRARY 23.2.0+
The original answer doesn't work with design library 23.2.0 or later. Thanks for #fahmad6 pointed out in comment, in case someone missed that comment, I'll put it here. You need to set both textAllCaps and android:textAllCaps to false to disable all capitalize setting.
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
ORIGINAL ANSWER
By default, tabs are created by TabLayout sets the textAllCaps property to be true, you have to define a style making this flag false.
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
#Paresh Mayani answer is correct however you can create only tab style
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
And use it using
<android.support.design.widget.TabLayout
app:tabTextAppearance="#style/MyCustomTextAppearance"
.../>
use this attribute app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
It will work.
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:tabGravity="fill"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabIndicatorColor="#color/colorPrimary"
app:tabMode="fixed"
app:tabPaddingStart="0dp" />
https://stackoverflow.com/a/34678235/1025379
<android.support.design.widget.TabLayout
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
/>
In my case two variants work:
1) By Bogdan (susemi99):
<android.support.design.widget.TabLayout
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
/>
2) By Paresh Mayani. I wanted to have android:textAllCaps="false" and android:textSize="15sp" simultaneously, so his old method works.
In styles.xml write (parent may vary, for instance, "#android:style/TextAppearance.Widget.TabWidget", "TextAppearance.Design.Tab"):
<style name="TabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/color_blue</item>
<item name="tabSelectedTextColor">#color/color_blue</item>
<item name="tabTextColor">#color/black</item>
<item name="tabTextAppearance">#style/TabLayoutTextAppearance</item>
</style>
<style name="TabLayoutTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">15sp</item>
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
Apply this style in layout:
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TabLayout"
/>
For those who can't get working other answers.
Defining a style is working fine when you have single line tab text.
If you take a close look into the TabLayout, you'll see that it's using a field design_tab_text_size_2line when the tabs has more than one line.
The only way I could find to effect this field is to override it in your dimen file.
So put this in your values/dimens.xml
<dimen name="design_tab_text_size_2line" tools:override="true">10sp</dimen>
Hope it helps.
This works for me in just one line
<android.support.design.widget.TabLayout
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"/>
Here is simple solution....Enjoy
for (int tabIndex = 0; tabIndex <tabLayout.getTabCount() ; tabIndex++) {
TextView tabTextView = (TextView)(((LinearLayout)((LinearLayout)tabLayout.getChildAt(0)).getChildAt(tabIndex)).getChildAt(1));
tabTextView.setAllCaps(false);
}
Change: <item name="android:textAllCaps">false</item>
With: <item name="textAllCaps">false</item>
This Worked For Me...
<style name="TabLayoutStyle" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/TabTextAppearance</item>
</style>
<style name="TabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
In versions priror to 14, you need to set (as commented by Paresh Mayani):
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
But, in case of android version is equal or greater than 14, you need to set:
<item name="android:textAllCaps">false</item>
So, if you need to be compatible with versions before and after 14, you also need to create a folder values-v14, and a file styles.xml in that folder with the content:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textAllCaps">false</item>
</style>
</resources>
Try following method and you can implement all the methods of TextView in TabLayout
private void setCustomTab() {
ViewGroup vg = (ViewGroup) mTabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(ResourcesCompat.getFont(this,R.font.montserrat_medium));
((TextView) tabViewChild).setAllCaps(false);
}
}
}
}
Hope it helps.
Here the Simple solution to Avoid Capitalize and change font size , font family on TabLayout design in Android 100% Working
Add following style on res/values/stye.xml
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">#font/poppins_semi_bold</item>
</style>
Call this style in your tab layout as app:tabTextAppearance="#style/MyCustomTextAppearance"
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#FFF"
app:tabTextAppearance="#style/MyCustomTextAppearance"
app:tabIndicatorHeight="3dp"
android:layout_marginBottom="#dimen/_5sdp"
app:tabSelectedTextColor="#color/text_app_color"
app:tabTextColor="#color/text_app_color_1"
app:tabMode="auto" />
</com.google.android.material.appbar.AppBarLayout>
You can also do this in your Java code. If you are using a SlidingTabLayout look at this sample:
protected TextView createDefaultTabView(Context context){
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);//see line 38 above change the value their in TAB_VIEW_TEXT_SIZE_SP.
textView.setTypeface(Typeface.DEFAULT);//From DEFAULT_BOLD
textView.setTextColor(Color.parseColor("#536DFE"));//Text color of the words in the tabs. Indigo A200
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
textView.setAllCaps(true);
}
int padding = (int)(TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding);
return textView;
}
Notice that textView.setAllCaps() has true as the perimeter:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
textView.setAllCaps(true);
}
When I changed this to (false) it solved the problem for me:
textView.setAllCaps(false);
Also my string resource file that I use for the tabs looks like this:
<string name="tab_title">Title with capital and smaller case</string>
However if it had all caps like >TITLE WITH ALL CAPS< you would still of course get all caps in your tabs.
I made no other changes.
It is noteworthy that you can set textView.setAllCaps(false) too, but this made no difference in my case. I just commented out textView.setAllCaps(true).
Changing attributes in XML file doesn't work in Android 11 (SDK 30). Here is code that I use to setup tabs individually using a tab label. It's safer than setting new styles for all text fields in the tabs or relying on the existing tab layout, because the current tab design can be changed by Android. This method assumes that a tab text has been set before the function below is called. Second parameter in the call, txt is a tab label.
private fun setTabStyle(tabs: TabLayout, txt: String) {
val av = ArrayList<View?>()
tabs.findViewsWithText(av, txt, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION )
if (av.count() > 0) {
val avt = ArrayList<View?>()
(av[0] as? ViewGroup)?.let {
for ( i in 0 until it.childCount) {
val tv = it.getChildAt(i) as? TextView
tv?.let {t ->
if (tv.text == txt) {
t.isAllCaps = false
t.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11.toFloat())
}
}
}
}
}
}
Long story short (programmatically)...
/**
* #param view Target TabLayout view
* #param caps Present the text caps style
*/
public static void setAllCaps(View view, boolean caps) {
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
setAllCaps(((ViewGroup) view).getChildAt(i),caps);
} else if (view instanceof TextView) ((TextView) view).setAllCaps(caps);
}
Call setAllCaps like this :
tabLayout.addTab(tabLayout.newTab().setText("Recent"));
tabLayout.addTab(tabLayout.newTab().setText("New"));
setAllCaps(tabLayout,false);
It'll work if you only add one of these calls.
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextAppearance="#style/TextAppearance.AppCompat.Medium"
app:tabTextAppearance="#style/TextAppearance.AppCompat.Small"
Just add this to your custom style file
<item name="android:textStyle">normal</item>
I read all the above solutions and all lead to this.
I want to display custom search in actionbar (I'm using ActionBarSherlock for that).
I got that:
But I want make custom layout (edittext field) to occupy the entire available width.
I've implemented custom layout as suggested here.
There is my custom layout search.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|fill_horizontal" >
<EditText
android:id="#+id/search_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#drawable/bg_search_edit_text"
android:imeOptions="actionSearch"
android:inputType="text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="#drawable/ic_search_arrow" />
</FrameLayout>
</LinearLayout>
And in MyActivity:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
actionBar.setCustomView(v);
How can I make custom layout to occupy all the available width of actionBar?
Help, please.
There is a trick for this. All you have to do is to use RelativeLayout instead of LinearLayout as the main container. It's important to have android:layout_gravity="fill_horizontal" set for it. That should do it.
I struggled with this myself, and tried Tomik's answer.
However, this didn't made the layout to full available width on start, only when you add something to the view.
You'll need to set the LayoutParams.FILL_PARENT when adding the view:
//I'm using actionbarsherlock, but it's the same.
LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getSupportActionBar().setCustomView(overlay, layout);
This way it completely fills the available space. (You may need to use Tomik's solution too).
This is how it worked for me (from above answers it was showing both default title and my custom view also).
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// actionBar.setCustomView(view); //last view item must set to android:layout_alignParentRight="true" if few views are there
actionBar.setCustomView(view, layout); // layout param width=fill/match parent
actionBar.setDisplayShowCustomEnabled(true);//must other wise its not showing custom view.
What I noticed is that both setCustomView(view) and setCustomView(view,params) the view width=match/fill parent. setDisplayShowCustomEnabled (boolean showCustom)
The answers from Tomik and Peterdk work when you want your custom view to occupy the entire action bar, even hiding the native title.
But if you want your custom view to live side-by-side with the title (and fill all remaining space after the title is displayed), then may I refer you to the excellent answer from user Android-Developer here:
https://stackoverflow.com/a/16517395/614880
His code at bottom worked perfectly for me.
For example, you can define a layout file which contains a EditText element.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textFilter" >
</EditText>
you can do
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
There is an example in the launcher app of Android (that I've made a library out of it, here), inside the class that handles wallpapers-picking ("WallpaperPickerActivity") .
The example shows that you need to set a customized theme for this to work. Sadly, this worked for me only using the normal framework, and not the one of the support library.
Here're the themes:
styles.xml
<style name="Theme.WallpaperPicker" parent="Theme.WallpaperCropper">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowShowWallpaper">true</item>
</style>
<style name="Theme.WallpaperCropper" parent="#android:style/Theme.DeviceDefault">
<item name="android:actionBarStyle">#style/WallpaperCropperActionBar</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="WallpaperCropperActionBar" parent="#android:style/Widget.DeviceDefault.ActionBar">
<item name="android:displayOptions">showCustom</item>
<item name="android:background">#88000000</item>
</style>
value-v19/styles.xml
<style name="Theme.WallpaperCropper" parent="#android:style/Theme.DeviceDefault">
<item name="android:actionBarStyle">#style/WallpaperCropperActionBar</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="Theme" parent="#android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
EDIT: there is a better way to do it, which works on the support library too. Just add this line of code instead of what I've written above:
getSupportActionBar().setDisplayShowCustomEnabled(true);