Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar.Tab setCustomView() doesn't fill_parent

I'm using ActionBar.Tab setCustomView() method with this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_grey" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Test Tab" 
        android:textColor="@color/background_dark_green"/>

</RelativeLayout>

this is my function setting the ActionBar:

public void setActionBar()
{
    ActionBar actionBar = getSupportActionBar();
    //actionBar.hide();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);        
    //set action bar navigation mode
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
    //set tabs      
    //home tab 
    Tab tab = actionBar.newTab().setText(TAB_HOME).setTabListener(new PicoTabListener<StartFragment>(this, StartFragment.class));       
    tab.setCustomView(R.layout.tab_background);
    actionBar.addTab(tab);
    //events tab
    tab = actionBar.newTab().setText(TAB_EVENTS).setTabListener(new PicoTabListener<EventsFragment>(this, EventsFragment.class));
    actionBar.addTab(tab);      
    //enter event code
    tab = actionBar.newTab().setText(TAB_CODE).setTabListener(new PicoTabListener<EnterCodeFragment>(this, EnterCodeFragment.class));
    actionBar.addTab(tab);      
}

and my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_basic_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_dark_green" >

</RelativeLayout>

The results seams to look like this (The upper left tab with the gray background): enter image description here

How can i make my custom view to fill whole tab and work properly ?

I'm using support package v7 for Android 2.3

like image 883
Asaf Nevo Avatar asked Nov 03 '13 15:11

Asaf Nevo


2 Answers

Just ran into this myself and figured out the solution. You should create a style for the tabview that clears the background and the padding and use it in your theme.

styles.xml:

<style name="Custom.ActionBar.TabView.Empty" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@null</item>
    <item name="android:padding">0dp</item>
</style>

themes.xml:

<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item>
    <item name="actionBarTabStyle">@style/Custom.ActionBar.TabView.Empty</item>
</style>
like image 59
kevinpelgrims Avatar answered Sep 24 '22 05:09

kevinpelgrims


To every created tab object just add layoutParams:

..    
tab.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
actionBar.addTab(tab);
like image 43
Mnio Avatar answered Sep 22 '22 05:09

Mnio