1

I have an activity that has a support action bar, below that a sliding tab layout, below that a listview of images for the corresponding tab. I just want to hide the support action bar not the sliding tab strip and I am not able to hide it.

Here is my activity:

import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;


public class NewGalleriActivity extends AppCompatActivity implements ViewTreeObserver.OnScrollChangedListener {

    Toolbar toolbar;
    ViewPager viewPager;
    NewGalleriPagerAdapter viewPagerAdapter;
    SlidingTabLayout slidingTabLayout;
    CharSequence Titles[] = {"Wildlife", "Architecture", "Black & White", "Close Up", "Night"};
    int numOfTabs = 5;

    private float mActionBarHeight;
    private ActionBar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_new_galleri);

        final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
                new int[]{android.R.attr.actionBarSize});
        mActionBarHeight = styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();


        //Initialise views
        viewPager = (ViewPager) findViewById(R.id.pager);
        slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
        toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        mActionBar = getSupportActionBar();

        int defaultValue = 0;
        int page = getIntent().getIntExtra("ARG_PAGE", defaultValue);
        viewPagerAdapter = new NewGalleriPagerAdapter(getSupportFragmentManager(), Titles, numOfTabs);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(page);
        slidingTabLayout.setDistributeEvenly(true);
        slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return Color.parseColor("#FAC80A");
            }
        });
        slidingTabLayout.setViewPager(viewPager);

        if (android.os.Build.VERSION.SDK_INT >= 21)
        {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.parseColor("#263238"));
        }

        FloatingActionButton button = (FloatingActionButton) findViewById(R.id.ddd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewGalleriActivity.this, ImageGridActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.parent).getViewTreeObserver().addOnScrollChangedListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onScrollChanged() {
        float y = findViewById(R.id.parent).getScrollY();
        if(y >= mActionBarHeight && mActionBar.isShowing()) {
            mActionBar.hide();
        } else if(y==0 && !mActionBar.isShowing()) {
            mActionBar.show();
        }
    }
}

Here is my layout for the activity:

<?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:background="@drawable/black"
    android:orientation="vertical">

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

    <com.pipipzz.simpleapp.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#231F20" />

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@drawable/drop_shadows" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/ddd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="24dp"
                android:layout_marginBottom="10dp"
                android:src="@drawable/fab_icons"
                app:borderWidth="@null"
                app:elevation="4dp"
                app:fabSize="normal" />

        </FrameLayout>

    </ScrollView>

</LinearLayout>

Here is my layout for the toolbar:

<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/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFC80A"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <ImageView
            android:id="@+id/tool_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/tool_logo"
            android:gravity="center_horizontal"
            android:src="@drawable/logo_tool" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/tool_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="8dp"
        android:contentDescription="@string/tool_search"
        android:src="@drawable/search" />

</android.support.v7.widget.Toolbar>

Edit: Here is my layout for fragment that is shown in the tabs.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#231F20"
    tools:context="com.pipipzz.simpleapp.ArchitectureFragment">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:scrollingCache="true"
        android:smoothScrollbar="true" />

</FrameLayout>

Any help would be highly appreciated.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
  • refer http://stackoverflow.com/questions/26773245/show-and-hide-action-bar-while-scrolling-view-pager-content and http://stackoverflow.com/questions/31290337/how-to-hide-actionbar-while-scrolling-listview-in-android – sasikumar Aug 01 '15 at 10:41
  • If I use AppBarLayout and NestedScrollView in my layout, as given in the second answer to this [question](http://stackoverflow.com/questions/31290337/how-to-hide-actionbar-while-scrolling-listview-in-android), do I need to do anything in my activity programmatically or not? – Amit Tiwari Aug 01 '15 at 11:07
  • @AmitTiwari, can you please check https://github.com/wasabeef/awesome-android-ui link, hope it will help you. – Hiren Patel Aug 01 '15 at 13:39

1 Answers1

1

Please have a look at this library Android-ObservableScrollView
It has a lot of examples and it will be much easier to use it in your case. There are several examples of using it with Toolbar, that you are currently using in your code.

It would be better if you prodived all source code to run it, maybe you have issue somewhere else.

  1. First about your code. Don't use findViewById whenever you want, it is really heavy method. So when you are doing this each time onScrollViewChanged it makes your app laggy.
  2. Find your scroll view only once in onCreate method

    mMainScrollView = findViewById(R.id.parent); mMainScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {@Override public void onScrollChanged() { int scrollY = rootScrollView.getScrollY(); //for verticalScrollView // Hide or show toolbar here } });

  3. Why not directly to call hide on toolbar without getting support action bar ? Try do to it directly on toolbar.

  4. If you call hide method directly on ActionBar/Toolbar does it work ?

  5. Try to do something like this

To hide toolbar

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

And show it again

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

CROSP
  • 4,499
  • 4
  • 38
  • 89
  • I saw that, but I got very confused. There are so many files and I can't seem to understand which one to choose and how to use it properly in my case. So, I was thinking of doing it without any libraries and using support libraries only. – Amit Tiwari Aug 01 '15 at 11:10
  • It is quite easy, you only need to hide toolbar on scroll down and show it on when user srolls up ? – CROSP Aug 01 '15 at 11:24
  • ya, that's what I want and I have coded accordingly(see onScrollChanged() in the activity) but, unfortunately, it is not working. If you could help me find out what's wrong in my code, it would be much helpful because, I don't want to use a library for what I think is doable using standard support libraries only. – Amit Tiwari Aug 01 '15 at 11:34
  • @AmitTiwari I have just edited my answer ,please have a look – CROSP Aug 01 '15 at 12:00
  • I can use hide() only with ActionBar, not toolbar. So I can't directly set it to show or hide. I have to do it using ActionBar. – Amit Tiwari Aug 01 '15 at 12:25
  • I removed every code related to scrolling and I just used `getSupportActionBar().hide()` and my action bar is getting hidden. What I don't understand now, is that why is it not hiding the action bar in `onScrollChanged()`? Is the logic of this method correct? – Amit Tiwari Aug 01 '15 at 12:47
  • I can hide the action bar, but the code is not working. Either the logic in `onScrollChanged()` is faulty or I have misplaced the `ScrollView` in the layout. I have also added the layout of the fragment that is being shown in the sliding tabs. – Amit Tiwari Aug 02 '15 at 07:47