0

am tying to hide my action bar in runtime when i switch from tab2 to tab1 or tab3 but its leaving a blank space

my Activity: (with a TabLayout and 3 fragments )

enter image description here

switched to another tab , action bar is hidden now but there's a Blank space :

enter image description here

am using actionBar.hide / actionBar.show , any clue how to properly remove this extra space ??? if what am trying to achieve is not possible then please guide me to any alternative way

Codes:

ActionBar actionBar;
........
........
actionBar = getSupportActionBar();
........
........
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

                if (tab == tab2){
                    actionBar.show();


                }else {
                    actionBar.hide();

                }};

Added Xml Codes:

MainActivity which includes content_main

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="MainActivity"
    android:animateLayoutChanges="true">

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

content_main:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
    android:animateLayoutChanges="true">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e4e4e4"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fillViewport="false"
    android:clickable="false" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tabs"/>

</RelativeLayout>
remy boys
  • 2,928
  • 5
  • 36
  • 65

4 Answers4

1

Try this @ where you want to hide/show ActionBar,

requestWindowFeature(Window.FEATURE_ACTION_BAR);

// delaying the hiding of the ActionBar
Handler h = new Handler();
h.post(new Runnable() {     
    @Override
    public void run() {
        getActionBar().hide();//OR
        //getActionBar().show();
    }
});

If Activity

getSupportActionBar().hide(); 
or 
getActionBar().hide();

If Fragment

getActivity().getSupportActionBar().hide();
or
getActivity().getActionbar().hide();
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • hey @Sathish thanks for responding , well `h.post` here post causing an error `cannot resolve method` and `Handler` is showing an error `Handler is abstract cannot be instantiated` and forcing me to implement methods – remy boys Jun 04 '16 at 06:17
  • `Handler h = new Handler() { @Override public void close() { } @Override public void flush() { } @Override public void publish(LogRecord record) { } }; h.post(new Runnable() { @Override public void run() { getActionBar().hide();//OR //getActionBar().show(); } });` – remy boys Jun 04 '16 at 06:17
  • 1
    use import android.os.Handler; – Sathish Kumar J Jun 04 '16 at 06:20
1

Try This

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
Dixit Panchal
  • 3,406
  • 1
  • 11
  • 14
1

In my fragments this is how I hide the ActionBar and it works perfectly fine

// MainActivity is the activity which contains fragment
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

xml

<style name="AppTheme.myNoActionBarTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Then in manifest.xml

<activity android:name=".NoticeboardActivity" android:theme="@style/AppTheme.myNoActionBarTheme"></activity>
Saqib
  • 377
  • 4
  • 7