I'd like to achieve the flow like gmail
i've tried this so far
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/story_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/measure_50dp"
app:layout_scrollFlags="scroll|enterAlways" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/measure_15dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Appbar hides while scroll up but doesnt appear on scroll down until its completely scrolled down. I'd like to achieve the flow like gmail, where the recyclerView is match_parent and it appears behind the toolbar when scrolled up, and toolbar hides/unhides on srcroll up and down in a nested scroll view. Any help is appreciated, Thanks!
You have to create nestedScrollview addOnScrollChangedListener like this
float viewScrolled = 0f
nestedScrollview.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver
.OnScrollChangedListener() {
if (viewScrolled < nestedScrollview.getScrollY()) {
viewScrolled = nestedScrollview.getScrollY();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Hide your app bar
}
}
if (viewScrolled > nestedScrollview.getScrollY()) {
viewScrolled = nestedScrollview.getScrollY();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (viewScrolled == 0F) {
// Show your app bar
}
}
}
And also refer this link:-https://medium.com/#tonia.tkachuk/appbarlayout-scroll-behavior-with-layout-scrollflags-2eec41b4366b
Related
Want to move my snackbar above bottomnavigationview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activityRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarView"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size"
android:background="#color/color_primary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</FrameLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomNavigationLayout"
android:layout_below="#+id/appBarLayout" />
<LinearLayout
android:id="#+id/bottomNavigationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<View style="#style/FullDivider" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:itemIconTint="#drawable/selector_bottom_bar_item"
app:itemTextColor="#color/blue"
app:labelVisibilityMode="unlabeled"
app:menu="#menu/main_navigation" />
</LinearLayout>
<com.mandarine.features.security.UnlockAppInputView
android:id="#+id/unlockAppInputView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
In MainActivity write smth like this:
container?.let {
Snackbar.make(it, "No internet connection!", Snackbar.LENGTH_LONG).show()
}
But snackbar also display from bot of screen
Add one Layout above BottomNavigationView
Example:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myLayout"
android:layout_above="#id/bottomNavigationView">
</android.support.design.widget.CoordinatorLayout>
then in Java code use:
final View viewPos = findViewById(R.id.myLayout);
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();
I believe that the problem is in findSuitableParent function. As you can see on 232 line your container used as fallback, but then root view is found. Try to change your container to CoordinatorLayout and it will fix the issue.
Resolve my problem with next solution:
private fun showNetworkMessage(isConnected: Boolean) {
if (!isConnected) {
val snack = Snackbar.make(
findViewById(R.id.container),
this.getText(R.string.warning_no_internet_connection), Snackbar.LENGTH_LONG
)
val params = snack.view.layoutParams as FrameLayout.LayoutParams
params.setMargins(0, 0, 0, this.resources.getDimension(R.dimen.action_bar_size).toInt())
snack.view.layoutParams = params
snack.show()
}
}
Where u can see i didn't use CoordinatorLayout.
I want something like this:
I have the following now:
With the following 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_height="130dp"
android:layout_width="match_parent">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="#+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/book_detail">
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#android:drawable/ic_input_add"
android:layout_margin="16dp"
android:clickable="true"
android:focusable="true"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
But I have some problems:
The Title is on top instead of bottom
There's something like a separator on top
I'm using a coordinator now, but I don't need a collapsing toolbar, so I think that there must be another solution maybe. How can I solve this?
Let's first start by making the toolbar taller. I think what you're looking for is CollapingToolbarLayout
To use the CollapsingToolbarLayout you have to change your ConstraintLayout to CoordinatorLayout.
Then you have to create an AppBarLayout to hold the CollapsingToolbarLayout and the Toolbar.
Then to make the floating button in that place you have to anchor it to the AppBarLayout created earlier like below:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_margin="16dp"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
android:clickable="true"
android:src="#drawable/message"
android:focusable="true"
app:elevation="30dp"/>
That way it will be anchored to the AppBarLayout so it will go up and down as the AppBarLayout goes up or down.
EDIT #1 :
To remove the title from the top and remove the separator you have to set your activity's theme to NoActionBar. In your AndroidManifest.xml file go to your activity and change its code to the following:
<activity
android:name="YOUR_ACTIVITY_PATH.BookDetailActivity"
android:theme="#style/AppTheme.NoActionBar" />
This way the activity will be declared without an action bar which means no separator and no title.
Then to add the title to the bottom you just have to set the title of the CollapsingToolbarLayout to the text you want.
You can set it in the xml with app:title="YOUR_TITLE" attribute or you can set it programmatically by calling the function setTitle("YOUR_TITLE") for your CollapsingToolbarLayout variable .
Here is what the full code could look like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BookDetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:title="YOUR_TITLE"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:id="#+id/book_detail">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_margin="16dp"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
android:clickable="true"
android:src="#drawable/message"
android:focusable="true"
app:elevation="30dp"/>
</android.support.design.widget.CoordinatorLayout>
EDIT 2:
In your activity set your action bar by this :
setSupportActionBar(YOUR_TOOLBAR_VARIABLE) then you can use getSupportActionBar().setDisplayHomeAsUpEnabled(true)
Note that the toolbar should be the id of this one:
android.support.v7.widget.Toolbar so in our case the toolbar with the id toolbar
Use this layout
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="192dp">
<android.support.design.widget.CollapsingToolbarLayout
android:elevation="4dp"
android:id="#+id/collapsing_toolbar"
android:background="#color/primary"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
style="#style/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end" />
The use this class for action of FAB
public class FlexibleSpaceExampleActivity extends AppCompatActivity
implements AppBarLayout.OnOffsetChangedListener {
private static final int PERCENTAGE_TO_SHOW_IMAGE = 20;
private View mFab;
private int mMaxScrollSize;
private boolean mIsImageHidden;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flexible_space);
mFab = findViewById(R.id.flexible_example_fab);
Toolbar toolbar = (Toolbar) findViewById(R.id.flexible_example_toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
onBackPressed();
}
});
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.flexible_example_appbar);
appbar.addOnOffsetChangedListener(this);
}
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (mMaxScrollSize == 0)
mMaxScrollSize = appBarLayout.getTotalScrollRange();
int currentScrollPercentage = (Math.abs(i)) * 100
/ mMaxScrollSize;
if (currentScrollPercentage >= PERCENTAGE_TO_SHOW_IMAGE) {
if (!mIsImageHidden) {
mIsImageHidden = true;
ViewCompat.animate(mFab).scaleY(0).scaleX(0).start();
/**
* Realize your any behavior for FAB here!
**/
}
}
if (currentScrollPercentage < PERCENTAGE_TO_SHOW_IMAGE) {
if (mIsImageHidden) {
mIsImageHidden = false;
ViewCompat.animate(mFab).scaleY(1).scaleX(1).start();
/**
* Realize your any behavior for FAB here!
**/
}
}
}
public static void start(Context c) {
c.startActivity(new Intent(c, FlexibleSpaceExampleActivity.class));
}
}
There are some Github Implementation to develope this view and many other
Collapsing Toolbar with Fab
I wonder why such a strange behavior in the app attribute: elevation = "0dp" and how to solve the problem. I use the androidx library. The toolbar is transparent. Shadow remains. When I write an app: elevation = "0dp", the shadow disappears, but the toolbar buttons, including the "back" buttons, stop responding.
I tried to solve it in other ways: android: elevation = "0dp"
programmatically set getSupportActionBar (). setElevation (0); Does not help.
layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="#color/back_gray">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="#color/transparent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/iv_like"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#android:color/transparent"
android:src="#drawable/heart_outline"
android:visibility="visible"/>
<ImageButton
android:id="#+id/iv_shared"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#android:color/transparent"
android:src="#drawable/shared"
android:visibility="visible" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
.....>
<TextView
........./>
<TextView
........../>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
fragment.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate( R.layout.fragment_data, container, false );
toolbar = (Toolbar) rootView.findViewById( R.id.toolbar );
MainActivity activity = (MainActivity) getActivity();
if ( activity != null ) {
activity.setSupportActionBar( toolbar );
// activity.getSupportActionBar().setElevation(0);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled( true );
activity.getSupportActionBar().setHomeButtonEnabled( true );
activity.getSupportActionBar().setDisplayShowTitleEnabled( false );
toolbar.setNavigationOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
} );
}
}
Try the below code snippet instead of toolbar.setNavigationOnClickListener
#Override public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
hope this will solve your problem.
thanks
This is just because you have used FrameLayout as a parent - and also removed elevation from AppBarLayout, and due to this, Your ScrollView is overlapping your AppBarLayout as it is written below AppBarLayout. change your root to something else, for eg.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:theme="#style/AppTheme.AppBarOverlay">
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appBarLayout">
</ScrollView>
</RelativeLayout>
I have this simple layout:
<RelativeLayout 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.support.design.widget.CoordinatorLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ActionBarThemeOverlay">
<include layout="#layout/include_toolbar_actionbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/theme_primary"
android:padding="#dimen/keyline_1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_search_view"
android:focusable="true"
android:focusableInTouchMode="true">
<com.xxxx.ui.widget.CustomSearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:withBackButton="true" />
</FrameLayout>
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="#+id/methods_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="#dimen/keyline_5">
<com.xxx.ui.widget.ButtonSearchBy
android:id="#+id/search_by_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="#string/search_by_categories"
app:icon="#drawable/ic_by_categories"
app:iconFillColor="#color/grey"
android:visibility="gone"/>
<com.xxx.ui.widget.ButtonSearchBy
android:id="#+id/search_by_recents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="#string/search_by_recents"
app:icon="#drawable/ic_by_recents"
app:iconFillColor="#color/grey"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
In my Activity, I added this code to disable user scroll on directly CoordinatorLayout:
if (mAppBarLayout.getLayoutParams() != null) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior appBarLayoutBehaviour = new AppBarLayout.Behavior();
appBarLayoutBehaviour.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
#Override
public boolean canDrag(#NonNull AppBarLayout appBarLayout) {
return false;
}
});
layoutParams.setBehavior(appBarLayoutBehaviour);
}
This code works correctly, BUT in the "fragment_container" (FrameLayout), I add a RecyclerView, and if the user scrolls into this list then the Coordinator scrolls too...
I want collapse or expand automatically my CoordinatorLayout, and not to have a link with the RecyclerView scrolling.
I tried this too, but it doesn't work properly:
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (mFragmentContainer.getVisibility() == View.VISIBLE &&
event.getAction() == MotionEvent.ACTION_MOVE) {
Rect fragContainerRect = new Rect();
mFragmentContainer.getHitRect(fragContainerRect);
if (fragContainerRect.contains((int) event.getX(), (int) event.getY())) {
LogUtils.LOGD("XXXX", "move");
mFragmentContainer.dispatchTouchEvent(event);
return true;
}
}
return super.dispatchTouchEvent(event);
}
So how can I do to disable the CoordinatorLayout scrolling when the list scrolls?
Thank you very much guys!
I want to create a navigational bottom sheet like that in Google Maps Navigational BottomSheet where we have the list of directions for the route. I am using android's BottomSheetBehaviour to open up the bottomsheet. The issue i am currently facing is that the listview doesn't show up inside the bottom sheet layout when it pops up. The view is simply blank. I also tried to inflate view myself inside a NestedScrollView to get the same result, but that too didn't show up.
This is my bottomsheet 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/bottomSheet1"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:elevation="4dp"
android:background="#color/white"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is a sheet with listview."
android:textSize="20dp"
android:layout_marginTop="10dp"
android:textColor="#color/primaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="This is a secondary text!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/nestedLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<ListView
android:id="#+id/bottomSheetListview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And in my java code,
View bottomSheetView = findViewById(R.id.bottomSheet1);
ListView listView = (ListView) findViewById(R.id.bottomSheetListview);
List<String> listData = getListData(); //returns a simple array list of strings, about 15 items
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
Am I doing anything wrong here? Why doesn't the litview or scrollview show up.
Edited
My Whole Activity
public class BottomSheetActivity extends AppCompatActivity {
private static final String TAG = "==> BottomSheetActivity";
BottomSheetBehavior mBottomSheetBehavior;
Button peek;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_sheet);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.peek = (Button) findViewById(R.id.peek);
peek.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showSheet1();
}
});
}
private List<String> getListData(){
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 15; i++) {
stringList.add("This is string number "+i);
}
return stringList;
}
private void showSheet1(){
if(mBottomSheetBehavior != null){
//hide any previous bottom sheets
mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
//initialize a new sheet
View bottomSheetView = findViewById(R.id.bottomSheet1);
ListView listView = (ListView) bottomSheetView.findViewById(R.id.bottomSheetListview);
listView.setVisibility(View.VISIBLE);
List<String> listData = getListData();
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listData));
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
activity_bottom_sheet.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="com.vedamic.androidtutorial.BottomSheetActivity">
<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_bottom_sheet" />
<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="#android:drawable/ic_dialog_email" />
<include layout="#layout/layout_bottom_sheet_1" />
</android.support.design.widget.CoordinatorLayout>
content_bottom_shee.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.vedamic.androidtutorial.BottomSheetActivity"
tools:showIn="#layout/activity_bottom_sheet">
<Button
android:id="#+id/peek"
android:text="peek 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/expand"
android:text="peek 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/flipboardSheet"
android:text="Flipbard BottomSheets"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/testListView"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I imported your xml and the android studios "Preview" indeed showed that the NestedScrollView with android:layout_height="match_parent" takes the whole space so the ListView hasn't got space left.
When setting the NestedScrollView to android:visibility="gone" the ListView has enough space.
So the only reason now that nothing is shown is maybe because your listData is empty?
EDIT
OK I implemented all info and the style is very messy. First of all the peek 1 button is behind the toolbar.
But I can still click it, so this happens:
Dont make your listview visibility gone at first. Bottom should know what size it has to be. If you need to make its visibility gone, done it after the bottom sheet created.
And try in your bottom sheet content_bottom_shee.xml parent linearlayout
android:fillViewport="true"