Hiding textView below toolbar in coordinator layout - java

I'm working on creating a layout that works as follows:
Toolbar at the top
textView(a title) below top toolbar
RecyclerView
button
where the toolbar is in the main_activity
and the others are in an other xml file
And when I scroll, I want to hide the textView in between top toolbar and recyclerView.

why don't try the following, it might help
myRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener(){
#Override
public void onScrolled(RecyclerView rv, int dx, int dy) {
//boolean hasStarted = state == rv.SCROLL_STATE_DRAGGING; //Scrollbar is MOVING
//boolean hasEnded = state == rv.SCROLL_STATE_IDLE; //ScrollBar is NOT MOVING
//boolean isStopping = state == rv.SCROLL_STATE_SETTLING; //Scrollbar is STOPPING
if(dy > 0){
myTextview.setVisibility(View.VISIBLE);
}
}
});

and this is the main activity
<?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"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center"
app:titleMarginStart="10dp"
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textAutoCorrect"
app:searchBackIcon="#drawable/ic_back_arrow"
app:searchVoiceIcon="#drawable/ic_voice_black"
/>
</FrameLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Related

How do I set the color of the action bar using palette library?

I have a bitmap which I use to display an image from firebase in an imageview. I want both the status bar and the action bar's colors to be adjusted by the vibrant color of the pictures from the database. I searched for how to do this, and have the following code below
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
android.app.ActionBar bar=setBackgrounddrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary)));
getWindow().setStatusBarColor(palette.getVibrantColor(getResources().getColor(R.colorPrimary)));
}
});
But I get an error on the line of code for get action bar saying 'set background drawable cannot be applied to int' When I remove the line of code for setting color of the actionbar only the status bar color is set and not the action bar, how can I set the color of the action bar as well?
As You see, setBackgroundDrawable() receive a param what type is Drawable,
but you pass an Int Color value in, then you got the error.
I do not test it, but You can give it a try with:
setBackgroundDrawable(new ColorDrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary))));
Maybe, it will be easy to use Toolbar?
activity_main.xml:
<?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:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/mainPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="4dp"
/>
</LinearLayout>
app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".activities.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:src="#drawable/logo_vector_dark"
/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Setup toolbar in onCreate of Activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Change color:
Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
toolbar.setBackgroundColor(palette.getVibrantColor(getResources().getColor(R.color.colorAccent)));
}
});

RecyclerView onScrolled not being fired at all

I am planning to create a paginated scroll-to-bottom RecyclerView. But the onScrolled callback isn't being fired at all:
mBooksRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.i("Dale", "scrolled");
}
});
mBooksRecyclerView.setNestedScrollingEnabled(false);
if (Utils.hasContent(books)) {
mBooksRecyclerView.setVisibility(View.VISIBLE);
BookCardViewAdapter adapter = new BookCardViewAdapter(this, books);
final GridLayoutManager gridLayoutManager = new GridLayoutManager(BooksActivity.this, 3);
mBooksRecyclerView.setLayoutManager(gridLayoutManager);
mBooksRecyclerView.setAdapter(adapter);
emptyView.setVisibility(View.GONE);
} else {
emptyView.setVisibility(View.VISIBLE);
mBooksRecyclerView.setVisibility(View.GONE);
}
I also tried removing the RecyclerView from a NestedScrollView and it still doesn't work.
Here is my XML files:
books_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="#dimen/books_category_height"
android:background="#color/gfs_blue">
<android.support.v7.widget.RecyclerView
android:id="#+id/categories_tab"
android:layout_width="match_parent"
android:layout_height="#dimen/books_category_height">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/sub_categories_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_marginTop="20dp">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/books_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/sub_categories_tab"
android:scrollbars="vertical" />
</RelativeLayout>
activity_books.gfs:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
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:background="#color/white"
android:fitsSystemWindows="true"
tools:context=".activities.GFSBooksActivity">
<include
android:id="#+id/appbarlayout"
layout="#layout/appbarlayout"/>
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_below="#id/appbarlayout"
android:background="#color/white"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No content found"
android:textColor="#color/gray"
android:textSize="20dp" />
</RelativeLayout>
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/books_content"
android:layout_below="#+id/appbarlayout"/>
<!--<android.support.v4.widget.NestedScrollView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--app:layout_behavior="#string/appbar_scrolling_view_behavior">-->
<!--</android.support.v4.widget.NestedScrollView>-->
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Initially, it was under a NestedScrollView but since it was mentioned that onScroll wouldn't be called if the RecyclerView is inside a NestedScrollView or a ScrollView, I removed the NestedScrollView.
I restarted my Android Studio and it is now working all along. But the fix probably was removing the RecyclerView inside the NestedScrollView.
You have disabled RecyclerView's scrolling. Hence, you will not receive any events of its scroll. You need to instead add the scroll event of parent of recyclerView which should be NestedScrollView not ScrollView.
Try this
int pastVisibleItem, visibleItemCount, totalItemCount, currentPage = 1, mPage = 1;
private boolean loading = true;
now use this method to implement pagination
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//Log.e("checkkk", "cheeckk");
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisibleItem) >= totalItemCount) {
loading = false;
currentPage++;
// do your stuff here
}
}
}
});

ViewPagerIndicator not showing up

I am using JakeWharton's ViewPagerIndicator on my landscape app but I think I am messing up with the layout. The ViewPagerIndicator does not show up. I just started learning Android Development so any help is appreciated!
My guess is that it has to do with my Linear Layout...should it be relative? Am I covering up the pager indicator? I know that if I move the indicator code above the view pager, I see the indicator but everything else is white. My fragment's layout is a relative layout with height and width matching the parent.
My app is an activity with fragments.
Edit: I set the indicator height to wrap content but now it floats on the top of the screen. How do I make it go down?
My_Activity.java
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
CirclePageIndicator circleInd = (CirclePageIndicator) findViewById(R.id.circles);
circleInd.setViewPager(pager);
circleInd.setStrokeColor(0xAA000000);
//circleInd.setCurrentItem(mAdapter.getCount() - 1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0: return FirstFragment.newInstance("First");
case 1: return SecondFragment.newInstance("Second");
default: return FirstFragment.newInstance("First");
}
}
#Override
public int getCount() {
return 2;
}
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.example.frontrowqa.myfirstapplication.MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_gravity="bottom" />
</LinearLayout>
And my fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="#+id/PasscodeText"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:hint="Passcode"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/ipAddressText"
android:layout_below="#id/PasscodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/intercomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ipAddressText"
android:hint="Intercom Event Number"
android:backgroundTint="#android:color/black"/>
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
Set your activity xml like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp" />
</LinearLayout>

Android Set FAB Behavior for ListView

I'm currently developing an application which have a ListView and a FAB. Now what i want to do is hide the FAB on the beginning and then show it after my ListView is scroll down. I have my content_main.xml where the ListView is coded, and activity_main.xml where my FAB is.
activity_main.xml
<?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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_up" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/words_list_view"
android:divider="#00000000"
android:listSelector="#drawable/list_selector"
android:layout_weight="1" />
</RelativeLayout>
and in my Main_Activity.java i have onScrollListener where i'am stock, thinking that this is where i can handle the behavior of FAB.
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
int preLast;
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if (lastItem > 20) {
if (preLast != lastItem) {
// SHow FAB Here
}
} else {
// Hide FAB Here
}
}
});
I hope you guys understand my problem. Thank You Very Much! Any Help Would Be Appreciated!

Appbar covers my settings fragment

I'm trying to develop an Android application and I have a problem. I have one Navigation Drawer Activity, and when I click on an item from the Drawer it dynamically adds the fragment to the given activity from java code. This works perfectly with my news Fragment but when I want to add my Settings fragment (which is a PreferenceFragment) there is a slight problem: the appbar covers the top part (content) of my PreferenceFragment. How can I make my PreferenceFragment to stay (appear) below the appbar? Thanks in advance!
This is my activity_main.xml file.
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="#layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main" app:menu="#menu/activity_main_drawer" />
This is my app_bar_main.xml file.
<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:id="#+id/fragment_container"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
This is how I add the Settings fragment to my activity:
fragmentTransaction.add(R.id.fragment_container, settingsFragment).commit();
My SettingsFragment looks like this:
public class SettingsFragment extends PreferenceFragment {
private static final boolean ALWAYS_SIMPLE_PREFS = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_notification);
Preference aboutPref = (Preference) findPreference("aboutKey");
aboutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
// some event handling code
});
Preference notiPref = (Preference) findPreference("notifications");
notiPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
// some event handling code
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
return true;
}
return super.onOptionsItemSelected(item);
}
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
|| !isXLargeTablet(context);
}
}
The solution was to add an empty RelativeLayout element to the app_bar_main.xml layout file, which would look something like this:
<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.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="? attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
</RelativeLayout>

Categories

Resources