3

I have a problem with a gap being displayed at the top of a fragment when I do actionBar.hide() (btw I don't see this gap on the simulator, only on a real device). I tried this answer and that one, but nothing works. This fragment is inside MainActivity, which loads different fragments when I click on the menu. It's the only fragment where the actionBar (which is custom) should be hidden, so I cannot set the fullscreen flag on MainActivity onCreate() method. How can I redraw the view so the actionBar empty space disappear?

Here is how it looks now:

enter image description here

This is the mainActivty where I load the fragment:

public void onCreate(Bundle savedInstanceState) {

        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);

        showActionBar();

        if(Build.VERSION.SDK_INT >= 21){
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
        setUpMenu();
}

On click on the menu, I change the fragment accordingly:

@Override
    public void onClick(View view) {
        if (view == itemProfile){
            mActionBar.hide();
            ProfileFragment profile = new ProfileFragment();
            profile.setUser(AppController.getInstance().getLoggedInUser());
            changeFragment(profile);
        }
}

And the custom actionBar

private void showActionBar() {
        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffffff"));
        mActionBar.setBackgroundDrawable(colorDrawable);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mLogo = (ImageView) mCustomView.findViewById(R.id.logo);

        ImageButton menuButton = (ImageButton) mCustomView
                .findViewById(R.id.menu);
        menuButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (resideMenu.isOpened()) {
                    resideMenu.closeMenu();
                } else {
                    resideMenu.openMenu();
                }
            }
        });

        ImageButton mapButton = (ImageButton) mCustomView
                .findViewById(R.id.map);
        mapButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), MapsActivity.class);
                startActivity(i);
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

    }

My theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorPrimaryDark">@android:color/black</item>
    <item name="android:windowActionBarOverlay">true</item>
</style>

My mainActivity layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/main_fragment">
            </FrameLayout>

        </LinearLayout>
    </FrameLayout>
</FrameLayout>

My custom actionBar

<?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/transparent">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent">

        <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="7dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/app_title"
        android:textSize="@dimen/textsize_large"
        android:textColor="@android:color/black"
        android:layout_gravity="center"
        android:visibility="gone"/>

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="7dp"
            android:layout_gravity="center"
            android:src="@drawable/logo"
            android:background="@color/transparent" />

        <ImageButton android:src="@drawable/earth"
            android:background="?attr/actionBarItemBackground"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="20dp"
            android:scaleType="centerInside"
            android:id="@+id/map"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="-15dp"
            android:tint="@color/black"/>

        <ImageButton android:src="@drawable/titlebar_menu_selector"
            android:background="?attr/actionBarItemBackground"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:scaleType="centerInside"
            android:id="@+id/menu"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="-15dp"
            android:layout_gravity="left|center_vertical"
            android:tint="@color/black"/>

    </FrameLayout>

</RelativeLayout>

Any help would be great, I've been fighting with it for days now and I really don't know what to do...

Community
  • 1
  • 1
Kalianey
  • 2,738
  • 6
  • 25
  • 43

3 Answers3

4

If you have created Bottom Navigation Activity and you don't want the hidden ActionBar's blank gap, Check the Host activities layout XML file, delete the line mentioned below

    android:paddingTop="?attr/actionBarSize"
Anurag Bhalekar
  • 830
  • 7
  • 9
2

replace

mActionBar.hide();

with

mActionBar.setVisibility(View.GONE);

You need to add Toolbar to your Activity layout. Find the code snippet below for simple Toolbar Layout.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>

Apply the theme to Activity. Here in this step you need to apply the theme which we have created in step-1 to your activity. This can be done, by using android:theme attribute in your application AndroidManifest.xml.

<activity
        android:name="com.javatechig.sample.MyActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
</activity>

Now you are almost ready. You just need to instantiate the Toolbar and add it to your activity by using setSupportActionBar(Toolbar) method.

 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.widget.Toolbar;

public class MyActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Set a toolbar to replace the action bar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
}

Visit the below link to know more..

Using Toolbar as ActionBar

Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
1

Put this line on your toolbar help me clear the gap, don't know will it works on other components like ur Relative layout. Hope helpful :)

android:fitsSystemWindows="true"

like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"    //add this line
android:background="@color/transparent">
isd isd
  • 11
  • 1