Implementing a NavigationView drawer - I am able to generate the drawer and see it, but cannot close it with swiping. Additionally, the NavigationItemSelectedListener does not appear to be set up correctly, as I cannot detect click events on the items.
MainActivity.java
private NavigationView navigationView;
private DrawerLayout navDrawerLayout;
private ListView navDrawerList;
private ArrayList<NavDrawerItem> navDrawerItemList;
private ActionBarDrawerToggle navDrawerToggle;
private void setupNavDrawer() {
this.navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Utility.showDebugToast(String.valueOf(item.getItemId()));
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
navDrawerLayout.closeDrawers();
return true;
}
});
this.navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.navDrawerList = (ListView) findViewById(R.id.left_drawer);
this.navDrawerItemList = new ArrayList<>();
this.navDrawerItemList.add(new NavDrawerItem("NAME", NavDrawerItemType.NAME));
this.navDrawerItemList.add(new NavDrawerItem("Create Mesh", NavDrawerItemType.CREATE_MESH));
this.navDrawerList.setAdapter(new NavDrawerAdapter(this, R.layout.drawer_item, R.id.drawer_tab_text, this.navDrawerItemList));
this.navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout, R.string.accept, R.string.accept) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
#Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
this.navDrawerLayout.addDrawerListener(navDrawerToggle);
navDrawerToggle.syncState();
}
activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:background="#drawable/purpose_background" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appbar_grey"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/LobbyTabLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="scrollable"/>
<LinearLayout
android:id="#+id/search_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_scrollFlags="scroll|enterAlways" >
<EditText
android:id="#+id/search_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="3"
android:lines="1"
android:textSize="16sp"
android:hint="#string/search_editText_placeholder"/>
<Button
android:id="#+id/action_search_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="#string/search_button"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_lobby_container">
<LinearLayout
android:id="#+id/lobby_search_area"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Lobby"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/grid_fragment"/>
</LinearLayout>
<com.nhaarman.supertooltips.ToolTipRelativeLayout
android:id="#+id/mesh_list_tooltip"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/dialog_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<include android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/spinner_overlay"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/toggle_fragment_button"
android:background="#color/background_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="20dp"
android:alpha=".9"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#000"
android:alpha="0.7"
/>
</android.support.design.widget.NavigationView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_tab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="76dp"
android:layout_height="76dp"
android:src="#drawable/logo_icon_statusbar"
app:border_color="#FF000000"
android:layout_marginLeft="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginStart="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/drawer_tab_text"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
</LinearLayout>
Why can't I slide the Drawer in and out?
With regards to the drawer closing behaviour, I expect this is to do with the structure of your activity.xml.
As per the guidance here:
To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately.
So this is saying that in your case where you have a content section and one drawer, the DrawerLayout should have two children; first the content, then the drawer. In your xml currently, the DrawerLayout only has one child, everything is in the CoordinatorLayout.
Try moving your NavigationView outside of the CoordinatorLayout so your activity looks something like this:
<DrawerLayout>
<CoordinatorLayout>
content
</CoordinatorLayout>
<NavigationView>
drawer stuff
</NavigationView>
</DrawerLayout>
Related
its image showing value and search bar
it is the image showing when clicking the search bar it will come up the values in list-view
I want to know why this happen and how to avoid the situation please help me thanks in advance
public class ListItem extends AppCompatActivity
{
ListView listView1;
ArrayList<String> listproduct;
ArrayAdapter<String > adapternew;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("Please Select the Item");
SearchView search=(SearchView)findViewById(R.id.Search);
ScrollView scroll=(ScrollView)findViewById(R.id.Scroll);
ListView listView1=(ListView)findViewById(R.id.Listview);
listproduct=new ArrayList<>();
listproduct.add("Stove Top Stuffing");
listproduct.add("Campell's Soup");
listproduct.add("Tide");
listproduct.add("Pampers");
listproduct.add("Pepsi Products");
listproduct.add("Tang");
listproduct.add("Top Ramen");
listproduct.add("Knorr");
listproduct.add("Palmolive");
listproduct.add("Scotch-Brite");
listproduct.add("Bounty Paper Towls");
listproduct.add("Oreo Cookies");
listproduct.add("Quaker Oats");
listproduct.add("Lays Potato Chips");
adapternew = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listproduct);
listView1.setAdapter(adapternew);
SearchManager manager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
search.clearFocus();
if(listproduct.contains(query))
{
adapternew.getFilter().filter(query);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapternew.getFilter().filter(newText);
return false;
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int newposition, long id) {
Toast.makeText(ListItem.this,listproduct.get(newposition)+"",Toast.LENGTH_SHORT).show();
Intent i=new Intent(ListItem.this,AddOrder.class);
i.putExtra("position",listproduct.get(newposition));
//adapternew.notifyDataSetChanged();
startActivity(i);
}
});
}
}
here is the code that is to show the list view item and i want to know whether the value in list view come up when clicking the search bar
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.00" />
<ScrollView
android:id="#+id/Scroll"
android:layout_width="100dp"
android:layout_height="150dp"
tools:layout_editor_absoluteX="-8dp"
tools:layout_editor_absoluteY="396dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/Search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.200">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the xml file of the listview
I think this is the code you are wanting.
No need to use scrollview here because ListView itself is scrollable. LinearLayout with Vertical Orientation will solve your issue
<?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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="2dp" />
<SearchView
android:id="#+id/Search"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"/>
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp">
</ListView>
</LinearLayout>
I think with ConstraintLayout , you can use app:layout_constraintTop_toBottomOf to assign it below the desired view instead of layout_below
<ListView
android:id="#+id/Listview"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/search" <!-- change here -->
app:layout_constraintVertical_bias="0.200">
I am having trouble getting the Toolbar and TabLayout to hide when I scroll up. I have 3 different xml files: 1) Toolbar, 2) TabLayoutFragment (here I have the TabLayout and ViewPager, and I include the toolbar here, and 3) HomeFragment (contains the RecyclerView). I need for the Toolbar to hide when I scroll up, and I have seen in many other posts that most people use a CoordinatorLayout instead of a Relative Layout or Linear Layout, and then they have to add app:layout_scrollFlags="scroll|enterAlways" which I have done, but I still can't get the Toolbar to hide when scrolling the RecyclerView up.
I've also added this to the RecyclerView, but still nothing... app:layout_behavior="#string/appbar_scrolling_view_behavior"
Could someone point out what is missing here?
Toolbar
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_home_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="#font/scriptbl"
android:text="Events"
android:textColor="#color/colorBlack"
android:textSize="60sp" />
<ImageView
android:id="#+id/search"
style="#style/SelectableIconThemeBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:contentDescription="#string/camera"
android:padding="10dp"
android:src="#drawable/icon_search" />
<ImageView
android:id="#+id/filter"
style="#style/SelectableIconThemeBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#+id/search"
android:contentDescription="#string/camera"
android:padding="10dp"
android:src="#drawable/icon10_sort" />
<ImageView
android:id="#+id/options"
style="#style/SelectableIconThemeBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:contentDescription="#string/options"
android:padding="10dp"
android:src="#drawable/icon_short_text" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
TabLayoutFragment
<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:id="#+id/fragment_home_tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="Home"
tools:context=".Fragment.TabLayoutFragment">
<include
android:id="#+id/toolbar_events"
layout="#layout/toolbar_events" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#fff"
app:layout_scrollFlags="scroll|enterAlways"
app:tabIndicatorColor="#000"
app:tabIndicatorFullWidth="false"
app:tabRippleColor="#android:color/transparent"
app:tabSelectedTextColor="#color/colorBlack"
app:tabTextColor="#color/colorBlack" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
HomeFragment
<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:id="#+id/fragment_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:tag="fragment_home"
tools:context=".Fragment.HomeFragment">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<TextView
android:id="#+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="Welcome to Events!"
android:textColor="#d9d9d9"
android:textSize="36sp"
android:visibility="gone" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:gravity="center_horizontal"
android:padding="8dp"
app:drawableStartCompat="#drawable/icon_loc" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/location"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_following"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
You can make it programmatically by adding the addOnScrollListener method.
On the RecyclerView.SCROLL_STATE_DRAGGING method hide the toolbar and TabLayout and on RecyclerView.SCROLL_STATE_IDLE method you can show it again if you want to show them again.
You can try it in such way:
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
// Show your view again.. If you want to show them..
} else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
// Hide your view..
}
super.onScrollStateChanged(recyclerView, newState);
}});
Or You can use recycler view's addOnScrollListener method:
Refer: addOnScrollListener
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
//Scrolling down
// Show your controllers from here
} else if (dy < 0) {
//Scrolling up
// Hide your controllers from here
}
}});
Also, you can add some animations there to show and hide your toolbar and tab layouts.
Place the toolbar and the tablayout within the ScrollView group to get the scrolling part.
<ScrollView>
<Toolbar></Toolbar>
<Tablayout></Tablayout>
</ScrollView>
I would like to have an Activity that stars with the drawer layout open at the beginning. I have tried simply putting
mDrawerLayout.openDrawer(gravity.END());
in various places in my code but regardless the items become unclickable until I close and re-open the drawer.
Has anyone had any luck with this? It is mostly for user experience reasons to add this feature.
My Drawer Layout has a list view in it, which clicks change my RecyclerView in the activity.
Here is my XML file for anyone who might need it.
<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:background="#color/colorPrimary"
android:orientation="vertical">
<!--android:elevation="4dp"-->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The navigation drawer -->
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:alpha="0.8"
android:background="#color/colorPrimary"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:alpha="1"
android:background="?attr/colorAccent"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_below="#+id/my_toolbar2">
<android.support.v7.widget.RecyclerView
android:id="#+id/listview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/colorPrimary"
android:dividerHeight="8dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/ic_add_circle_outline_white_24dp"
android:visibility="visible"
app:elevation="24dp"
app:fabSize="auto"
app:layout_anchor="#+id/listview2"
app:layout_anchorGravity="bottom|end"
app:rippleColor="#FFF" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Try to do like this in your onCreate(Bundle savedInstanceState) of Activity:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.postDelayed(new Runnable() {
#Override
public void run() {
if (mDrawerLayout != null) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
}, 200);
I'm having trouble implementing HomeUpEnabled when using CollapsingToolbarLayout, I can see the icon but noting happens when I press it.
Here is the CollapsingToolbarLayout
avivity.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<ImageView
android:id="#+id/header_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000" />
<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>
Activity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I have declared the activity in my manifest. Any help would be appreciated.
----------------------------EDIT-----------------------------------
THIS IS WHAT FIXED IT:
I don't know if it is correct, but it works.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
And in my xml I changed the background of the toolbar to:
android:background="?android:attr/selectableItemBackground"
to get the ripple effect on the icon.
Hope this helps someone.
Overrited Following method and specify your action in method block with menu id as home :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
You have to set the parent activity name in your manifest.xml
Your code is correct but only displays the back arrow on your toolbar, you have to tell your activity which is the activity to come back to.
<activity android:name="YourActivity" android:parentActivityName="YourParentActivity" />
Ok, Create 3 different xml file for activity_main, app_bar and content_main
app_bar
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/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#color/colorPrimaryDark"
app:contentScrim="?attr/colorPrimaryDark"
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"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabIndicatorHeight="5dp"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/colorPrimaryDark"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.CollapsingToolbarLayout>
content_main
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"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:id="#+id/tabs_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
activity_main
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.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:background="#color/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawerLayout">
<include
layout="#layout/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/mystuff"
app:menu="#menu/drawermenu" />
</android.support.v4.widget.DrawerLayout>
I want to achieve the design like this using BottomSheetDialogFragment. But the problem is the bottom layout get scrolled when i drag. I want the bottom layout always in bottom until the BottomSheetDialogFragment gets dismissed.
Please find the screenshot
Here is my code
bottomsheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--The main content goes over here-->
</LinearLayout>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</FrameLayout>
ModalBottomSheetFragment.java
public class ModalBottomSheetFragment extends BottomSheetDialogFragment {
public ModalBottomSheetFragment() {}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {}
};
#TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View mContentView = View.inflate(getContext(), R.layout.bottomsheet_layout, null);
dialog.setContentView(mContentView);
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior mBehavior = layoutParams.getBehavior();
if (mBehavior != null && mBehavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) mBehavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
int height = getScreenHeight(getActivity());
final double desiredHeight = 0.85 * height;
mContentView.getLayoutParams().height = height;
((BottomSheetBehavior) mBehavior).setPeekHeight((int) desiredHeight);
}
}
public static int getScreenHeight(Context ctx) {
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
return metrics.heightPixels;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button mShowBottomSheetDialog;
private BottomSheetDialogFragment bottomSheetDialogFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowBottomSheetDialog = (Button) findViewById(R.id.showBottomSheet);
mShowBottomSheetDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bottomSheetDialogFragment = new ModalBottomSheetFragment();
bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
});
}
}
use this layout instead of your layout ....
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical">
<!--The main content goes over here-->
</LinearLayout>
</ScrollView>
<!--The footer view-->
<LinearLayout
android:id="#+id/footer_purchase_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt_cancel_purchase"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center_vertical"
android:text="CLOSE"
android:textSize="14sp" />
<TextView
android:id="#+id/txt_item_purchase_action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="right|center_vertical"
android:text="ADD STICKERS"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Note:- Weight divide your Layout into different View which is constant in your layout ......
use this layout:
<?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:background="#android:color/white"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="18sp" />
</FrameLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your content here-->
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="#+id/bottomView"
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Footer"
android:textSize="18sp"/>
</FrameLayout>
</LinearLayout>