How can I provide a divider for BottomNavigationView in Android? I need this tiny line between Home and Settings.
You can create a background for the BottomNavigationView as is suggested here. If you already have a background and since BottomNavigationView is a FrameLayout, you could add a divider as follows:
navigation_divider.xml
<shape
android:shape="rectangle">
<size android:width="2dp" />
<solid android:color="#E91E63" />
</shape>
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu">
<View
android:layout_width="2dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#drawable/navigation_divider" />
</com.google.android.material.bottomnavigation.BottomNavigationView>
<fragment
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
You will, of course, need an even number of selections in the menu.
Related
I want to insert fixed footer bottom of the screen and above should be content in scroll view I used below code but it's throwing exception ScrollView can host only one direct child
<layout
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">
<data>
<variable
name="mainActivity"
type="com.example.footer.MainActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
<TextView
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Try is, it might be what you're looking for
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- ScrollView can only contain 1 view inside-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/footer"
app:layout_constraintTop_toTopOf="parent">
<!-- Use LinearLayoutCompat or ConstraintLayout or layout any-->
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- do some thing -->
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
<TextView
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The main issue is that the constraints are not set correctly and changes to ScrollView layout_height and id are needed.
ScrollView requires an id android:id="#+id/scrollView"
and bottom constraint to be top of TextView layout_constraintBottom_toTopOf="#+id/footer"
TextView needs a top constraint to be bottom of ScrollView layout_constraintTop_toBottomOf="#+id/scrollView"
Another issue was the height of ScrollView needs to use "match_constraint" which is set like this in the code view of the layout: android:layout_height="0dp"
When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.
Here is the final layout updated with above suggestions:
<layout
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">
<data>
<variable
name="mainActivity"
type="com.example.footer.MainActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/footer">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</ScrollView>
<TextView
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
My RecyclerView doesn't animate when scrolling. I've narrowed down the problem to the NestedScrollView that the RecyclerView is in. When the RecyclerView is outside the NestedScrollView it animates fine. But when it's in the NestedScrollView it doesn't. How can I animate the RecyclerView while keeping it in the NestedScrollView?
Here's the relevant layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mainToolbar">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#android:color/transparent"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/searchToolbar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:background="#A106A1"
android:elevation="2dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways">
<!-- dummy to catch focus -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:animateLayoutChanges="true"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="1dp" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here's the animation applied in the RecyclerView adapter:
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) { //binds the data to the view
holder.container.setAnimation(AnimationUtils.loadAnimation(holder.container.getContext(),R.anim.fade_out));
holder.nameTextView.setText(labels[position]);
holder.packageNameTextView.setText(packageNames[position]);
holder.iconImageView.setImageDrawable(drawables[position]);
}
Update: Here it is using the CollapsingToolbarLayout instead of NestedScrollView. How do I make the toolbar scroll?
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mainToolbar">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#android:color/transparent"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/searchToolbar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:background="#A106A1"
android:elevation="2dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll">
<!-- dummy to catch focus -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:animateLayoutChanges="true"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
It is not a good way to put RecyclerView inside NestedScrollView. If you want to make SearchBar above RecyclerView, you can check this.
You can also check this refference.
I checked your xml and modified it. It is working on my side. Please modify it according to your UI requirements.
N.B.: Check after populating data on recyclerview. First it may seem it is not working due to no data on recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:fitsSystemWindows="true"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar_scrolling">
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:animateLayoutChanges="true"
android:background="#drawable/rounded_bg"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In activity:
getSupportActionBar().setBackgroundDrawable(getResources()
.getDrawable(R.drawable.rounded_bg_action_bar));
rounded_bg_action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp" />
</shape>
rounded_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomLeftRadius="100dp"
android:bottomRightRadius="100dp" />
</shape>
I am trying to use Bottom Navigation Activity. I use the default 3 menu items with their fragments.
My problem is, I am seeing a gap between the ActionBar and the fragment. In the activity_main, I set the height to match_parent.
I tried to see if anyone else has this problem but could not come across.
In the activity_main.xml file, I have the code that comes with a creation of a new activity:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="#navigation/mobile_navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_dashboard" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/title_notifications" />
</menu>
mobile_navigation.xml
<navigation 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/mobile_navigation"
app:startDestination="#+id/navigation_home">
<fragment
android:id="#+id/navigation_home"
android:name="com.zakuto.myapplication.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/navigation_dashboard"
android:name="com.zakuto.myapplication.ui.dashboard.DashboardFragment"
android:label="#string/title_dashboard"
tools:layout="#layout/fragment_dashboard" />
<fragment
android:id="#+id/navigation_notifications"
android:name="com.zakuto.myapplication.ui.notifications.NotificationsFragment"
android:label="#string/title_notifications"
tools:layout="#layout/fragment_notifications" />
</navigation>
Below a screenshot of the screen.
Your layout has a padding-top. Remove this line
android:paddingTop="?attr/actionBarSize"
I'm using Bottom Navigation View for my application. Here I have added 3 items in menu and have customized the app:itemTextAppearanceActive & app:itemTextAppearanceInactive attribute of Bottom Navigation View as follows:
<android.support.constraint.ConstraintLayout
android:layout_below="#+id/tb_home"
android:id="#+id/cl_home_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.design.widget.BottomNavigationView
android:id="#+id/bnv_home_stats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:soundEffectsEnabled="true"
app:labelVisibilityMode="labeled"
app:itemTextAppearanceActive="#style/navTextActive"
app:itemTextAppearanceInactive="#style/navTextInactive"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/navigation"/>
</android.support.constraint.ConstraintLayout>
in style xml file:
<style name="navTextInactive">
<item name="android:textSize">20dp</item>
<item name="android:textColor">#color/colorInActive</item>
</style>
<style name="navTextActive">
<item name="android:textSize">20dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/colorActive</item>
</style>
When a click is made on navigation menu, there is no change in Active and Inactive status of other menus. Active selection remains of first menu even if you select other menu.
Complete xml file for activity view:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity"
tools:showIn="#layout/app_bar_home">
<RelativeLayout
android:id="#+id/rl_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<android.support.design.widget.TabLayout
android:id="#+id/tb_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/MyCustomTabText"
android:visibility="gone">
<android.support.design.widget.TabItem
android:id="#+id/tb_home_billing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_billing"/>
<android.support.design.widget.TabItem
android:id="#+id/tb_home_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_stats" />
</android.support.design.widget.TabLayout>
<RelativeLayout
android:layout_below="#+id/tb_home"
android:id="#+id/rl_home_billing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<Button
android:id="#+id/bt_home_billing_order"
android:layout_alignParentStart="true"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="#string/title_order"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="#+id/bt_home_billing_parcel"
android:layout_alignParentEnd="true"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:text="#string/title_parcel"
android:textSize="20sp"
android:textStyle="bold"/>
<ExpandableListView
android:id="#+id/elv_home_billing_unsettled"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/bt_home_billing_order"
android:layout_marginTop="30dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
<android.support.constraint.ConstraintLayout
android:layout_below="#+id/tb_home"
android:id="#+id/cl_home_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.design.widget.BottomNavigationView
android:id="#+id/bnv_home_stats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:soundEffectsEnabled="true"
app:labelVisibilityMode="labeled"
app:itemTextAppearanceActive="#style/navTextActive"
app:itemTextAppearanceInactive="#style/navTextInactive"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/navigation"/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
You first add in main layout this code:
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="40dp"
tools:ignore="MissingConstraints" >
</com.google.android.material.tabs.TabLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
And in menu named bottom_nav_menu.xml :
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_dashboard"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_dashboard" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/title_notifications" />
And which fragment you want to switch, you should add those fragment.
And for activity code is :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
And from Android-Studio you can start this automatically:
New Project> Phone and Tablet > Navigation Drawer Activity> Done
Or For new activity:
New > Activity > Bottom Navigation Activity> Done.
Hope it will help you otherwise you can ask me in comment.
Thanks.
I have a CardView as parent and inside the CardView I have a ConstraintLayout and defined a border for the ConstraintLayout. But the problem is that the border disappears when it reaches the imageView I put inside ConstraintLayout and it can't be seen anymore
https://imgur.com/dELnfoB
Now to fix this problem, I set the top and bottom and right margins as 1dp for imageView because my border is 1dp wide, but still the problem is that the corner angle has a problem and gets narrow and is not displayed clearly
By the way, I don't want to use cardElevation
item code :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv_item_parent"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusable="true"
android:layoutDirection="ltr"
app:cardCornerRadius="4dp"
app:cardElevation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pc_corners_parent_gray_stroke">
<ImageView
android:id="#+id/iv_item_avatar"
android:layout_width="130dp"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/wqe" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/iv_item_avatar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tv_item_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:ellipsize="end"
android:maxLines="1"
android:text="4.0 ★"
android:textColor="#color/colorTextPrimary"
android:textDirection="ltr"
android:textSize="#dimen/text_tiny_other"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
corners code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/colorWhite"/>
<corners android:radius="4dp"/>
<stroke android:width="1dp" android:color="#color/colorCardViewsBackgroundSet" android:dashWidth="1dp"/>
</shape>
You can put the ImageView inside a CardView and it should keep the corners round.
I am not sure if it's the best way to go about it but it works.