Fragment replaced in activity does not show setDisplayHomeAsUpEnabled arrow - android

In my activity I have one FrameLayout that needs to be swapped out with another fragment on button press. When the second fragment is pushed, the actionbar does not show the back arrow. However, pressing the back triangle does pop the second fragment and returns to the Main Fragment.
The initial Activity with the Main fragment
You can see that the second fragment is added, but the arrow is not present to take the user back. Even though in my NewCallFragment I have the line
activity?.actionBar?.setDisplayHomeAsUpEnabled(true)
What am I doing wrong?
My Activity
private const val TAG = "DashboardActivity"
class DashboardActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
if (savedInstanceState == null) {
supportFragmentManager.commit { add<MainFragment>(R.id.fragment_container, null, intent.extras) }
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putCharSequence(TAG, title)
}
override fun onSupportNavigateUp(): Boolean {
if (supportFragmentManager.popBackStackImmediate()) {
return true
}
return super.onSupportNavigateUp()
}
class MainFragment : Fragment(R.layout.fragment_dashboard_main) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.btnNewCall).setOnClickListener {
onShowNewCall()
}
}
private fun onShowNewCall() {
activity?.supportFragmentManager?.commit {
replace<NewCallFragment>(R.id.fragment_container, null, null)
addToBackStack(null)
}
}
}
class NewCallFragment : Fragment(R.layout.fragment_new_call) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.actionBar?.setDisplayHomeAsUpEnabled(true)
activity?.title = "My Second Fragment"
}
}
}
Activity Layout
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dashboard_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="#dimen/parent_padding"
tools:context=".activitiesx.DashboardActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Main Fragment 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">
<com.google.android.material.button.MaterialButton
android:id="#+id/btnNewCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:text="#string/new_call"
app:icon="#drawable/ic_add" />
</RelativeLayout>
New Call Fragment Layout
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new call"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Set a Fragment inside a Fragment

I have seen similar questions here, but none of them helps.
I have MainActivity and a few Fragments which works fine, but I want to set one fragment inside of another.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomMenu)
val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainer) as NavHostFragment
val navigationController = hostFragment.navController
bottomNavigationView.setupWithNavController(navigationController)
setupActionBarWithNavController(navigationController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragmentContainer)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Parent Fragment:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
incomeButton = view.findViewById(R.id.addIncomeButton)
expenseButton = view.findViewById(R.id.addExpenseButton)
val childFragment: Fragment = ChartFragment()
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.childFragment, childFragment).commit()
return view
}
Child Fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return ComposeView(requireContext()).apply {
setContent {
Text(
text = "here is another important code",
fontSize = 28.sp
)
}
}
The layouts for Activity:
<?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"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="#menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="475dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomMenu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_map" />
</androidx.constraintlayout.widget.ConstraintLayout>
The layouts for Fragment:
<?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"
tools:context=".fragments.MainFragment">
<Button
android:id="#+id/addIncomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="21dp"
android:backgroundTint="#color/primary"
android:text="#string/addIncomeButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/addExpenseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/red"
android:text="#string/addExpenseButton"
app:layout_constraintBottom_toTopOf="#+id/addIncomeButton"
app:layout_constraintEnd_toEndOf="parent" />
<FrameLayout
android:id="#+id/forChart"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The most of the same questions' solvations say to use childFragmentManager so do I but it doesn't work.
I get an error java.lang.IllegalArgumentException: No view found for id 0x7f08023f.
I guess I do not understand something, but I can't realize what.
Thank you!
Seems that you're mixing the ids of the container for the transaction - that's also what the error says. You're trying to put the ChartFragment into a container with R.id.childFragment id:
transaction.replace(R.id.childFragment, childFragment).commit()
And from what I see in the MainFragment layout, the container id is R.id.forChart. Try changing it to this:
transaction.replace(R.id.forChart, childFragment).commit()

Error :Binary XML file line : Inflating class fragment

I have an activity MainActivity in which I have and fragment signup_fragment I want to call fragment from an activity but it gives an exception of Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
and Caused by: java.lang.RuntimeException: com.example.pickingredients.MainActivity#d9ff1e7 must implement OnFragmentInteractionListener
Below are my code
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fragment=Signup()
supportFragmentManager.beginTransaction().add(R.id.sigup_fragment,fragment).commit()
}
SignupFragment
class Signup : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private var listener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_signup, container, false)
}
// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(uri: Uri) {
listener?.onFragmentInteraction(uri)
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
} }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity"
android:background="#color/light_grey">
<TextView
android:text="#string/what_is_your_mood"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/mood_tv"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.03"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"
android:textSize="18sp"/>
<androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/guideline" app:layout_constraintGuide_begin="16dp"
android:orientation="vertical"/>
<fragment
android:layout_width="362dp"
android:layout_height="98dp" android:name="com.example.pickingredients.ImagesliderFragment"
android:id="#+id/sigup_fragment" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="#+id/guideline"
android:layout_marginStart="8dp" app:layout_constraintTop_toBottomOf="#+id/mood_tv"
android:layout_marginTop="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_signup.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Signup">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment"/>
</FrameLayout>
Just remove android:name tag from <fragment .../>..
And use FrameLayout instead of fragment.
Or you can use below code.
<FrameLayout
android:layout_width="362dp"
android:layout_height="98dp"
android:id="#+id/sigup_fragment"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/mood_tv" />
Problem is here com.example.pickingredients.ImagesliderFragment. Either this fragment not found or having issues on that fragment.
And you should use FrameLayout instead of Fragment in xml.
Moreover your MainActivity should implement OnFragmentInteractionListener

NavigationDrawer not working with BottomAppBar

I am trying to use a BottomAppBar, but when I try to show the NavigationView it does not work. I have read about showing NavigationView in BottomAppBar and there are 3 main steps:
1- setSupportActionBar() - In the Activity/Oncreate method
2- set up your menu in onCreateOptionsMenu
3- catch the listener and show the menu in onOptionsItemSelected(item: MenuItem?)
Here is my Activity Code:
class HomeActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(bottom_app_bar)
}
override fun onClick(view: View?) {
when(view!!.id){
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.navigation_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) {
android.R.id.home -> {
val bottomNavDrawerFragment = BottomNavigationDrawerFragment()
bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
}
}
return true
}
}
And Fragment menu code:
class BottomNavigationDrawerFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bottom_navigation_drawer, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
navigation_view.setNavigationItemSelectedListener { menuItem ->
// Bottom Navigation Drawer menu item clicks
when (menuItem.itemId) {
// R.id.nav1 -> context!!.toast(getString(R.string.nav1_clicked))
}
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
true
}
close_imageview.setOnClickListener {
this.dismiss()
}
disableNavigationViewScrollbars(navigation_view)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet!!)
bottomSheetBehavior.setBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (slideOffset > 0.5) {
close_imageview.visibility = View.VISIBLE
} else {
close_imageview.visibility = View.GONE
}
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
}
}
})
}
return dialog
}
private fun disableNavigationViewScrollbars(navigationView: NavigationView?) {
val navigationMenuView = navigationView?.getChildAt(0) as NavigationMenuView
navigationMenuView.isVerticalScrollBarEnabled = false
}
}
Anybody have any idea of what's wrong?
I just change the order in the xml and it works.
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/content_main" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorPrimary"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/ic_menu"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_anadir"
app:tint="#null"
app:layout_anchor="#id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml:
<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="#android:color/background_light"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:background="#drawable/shape_coordinator"
>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/nestedScrollView"
tools:ignore="PrivateResource"
app:layout_behavior="">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

NavigationComponent recyclerview return shared element transition

I use Navigation Component in project. Trying to set up shared element transitions from recycler. And enter transition in the second fragment works fine, but when I return to the first fragment - there is no return transition.
I tried to explicitly set enter and return transitions in the first fragment like I did in the second like this
val transition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
sharedElementEnterTransition = transition
sharedElementReturnTransition = transition
but it didn't help.
Also tried to remove enter and exit fragment animations. For the second fragment enter transition works with animations, but who knows.
also tried to use the solution from this question, but in my case it didn't work.
https://stackoverflow.com/a/52922835/10951565
mainFragment
stickyAdapter.onItemClick = { event, imageView, textView ->
val args = bundleOf(EventDetailFragment.EVENT to event)
val extras = FragmentNavigatorExtras(
imageView to imageView.transitionName,
textView to textView.transitionName
)
findNavController().navigate(R.id.action_global_eventDetailFragment, args, null, extras)
}
in adapter onClick I set unique transition names for the views:
mainFragmentAdapter
eventImageView.setOnClickListener {
titleTextView.transitionName = "${event.title}$TRANSITION_TITLE"
eventImageView.transitionName = "${event.title}$TRANSITION_IMAGE"
onItemClick?.invoke(event, eventImageView, titleTextView)
}
in detailsFragment I get transition names from arguments (copypasted them to be sure there is no mistakes and for enter transition it worked)
I call postponeEnterTransition() in OnCreate method to wait until i can set transition names, sharedElementEnterTransition and sharedElementReturnTransition in onViewCreated and then call startPostponedEnterTransition()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
postponeEnterTransition()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_event_detail, container, false).apply {
val event = arguments?.getSerializable(EVENT) as Event?
toolbarImageView.transitionName = "${event?.title}$TRANSITION_IMAGE"
titleTextView.transitionName = "${event?.title}$TRANSITION_TITLE"
val transition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
sharedElementEnterTransition = transition
sharedElementReturnTransition = transition
startPostponedEnterTransition()
toolbar.setupWithNavController(findNavController())
event?.let {
Picasso.get()
.load("${EventGroupAdapter.BASE_SMALL_IMAGE_URL}${event.smallimage}")
.into(toolbarImageView)
}
}
}
main_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".view.activity.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="?android:colorBackground"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true" />
<com.rd.PageIndicatorView
android:id="#+id/pageIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/viewPager"
android:layout_centerHorizontal="true"
android:layout_marginBottom="7dp"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_radius="3.5dp"
app:piv_selectedColor="#color/colorIndicatorSelected"
app:piv_unselectedColor="#color/colorIndicatorUnselected"
app:piv_viewPager="#id/viewPager" />
<include
android:id="#+id/horizontalScrollView"
layout="#layout/fragment_main_horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/viewPager" />
</RelativeLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/emptyMessage"
android:layout_marginTop="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layoutAnimation="#anim/layout_animation_from_bottom"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_empty_events"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/empty_events_message"
android:textSize="18sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="48dp"
android:nestedScrollingEnabled="true"
tools:listitem="#layout/fragment_main_event_item" />
</RelativeLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/programTextView"
android:layout_gravity="center"
android:visibility="gone"
tools:visibility="visible" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:colorBackground"
app:menu="#menu/bottom_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Please share your recipe of the return shared element transition with Navigation Component
I ran into this the other day where going from the MainFragment item to a DetailFragment animated fine, but the sharedElementReturnTransition wasn't working when pressing back.
The fix for me was to postpone the transition on the MainFragment. At first, I was trying to do so in onCreate, but onCreate isn't always called when going back, so the fix was to postpone it in onViewCreated (I would guess onCreateView might work too) and then start the transition once the RecyclerView is ready.
DetailFragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val transition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
sharedElementEnterTransition = transition
sharedElementReturnTransition = transition
}
MainFragment:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
postponeEnterTransition()
recycler_view.post { startPostponedEnterTransition() }
}

Setprogress for a progress bar across fragments android using Kotlin

I am an amateur to both, android studio and kotlin. I am implementing a scenario where I have some fragments, as and when we finish with one fragment, the progress is shown on the main layout. These fragments replace the main frame layout and switching from one fragment to next is done in the previous fragment using the setonclicklistener. However, the problem is that I am unable to see the update happening to the progressbar. By the way, I am not using any progress dialogue. All I need is to update the progress bar as I switch from one fragment to another. When I run the App, it doesn't crash but the update doesn't happen. The main_layout file as given below and the frame layout is replaced by the fragments.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/parentmain_progress"
android:layout_width="match_parent"
android:layout_height="25dp"
style="#style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:scaleY="2"/>
<FrameLayout
android:id="#+id/parentmain_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/parentmain_progress">
</FrameLayout>
</RelativeLayout>
One of the fragments where the progress is not updated:
class ParentChildInfoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_parent_info_relation, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
headerparentrelation_text?.text= arguments?.getString("name")
parentmain_progress?.max=4
parentmain_progress?.setProgress(3)
val string = "Your Cild is a....\n Girl/Boy"
specifying_relation_txt?.text = string
relation_mom_image?.setImageResource(R.drawable.girl)
relation_dad_image?.setImageResource(R.drawable.boy)
val girlstring = "Girl"
mom_txt?.text = girlstring
val boystring = "Boy"
dad_txt?.text=boystring
nextbtn_parentrelation.setOnClickListener {
fragmentManager?.beginTransaction()?.replace(R.id.parentmain_frame,ParentInfoFragment())?.commit()
}
}
}
Any help appreciated.
Like that ?
On this example I use the new package structure : Androidx
If you do not use it yet, simply change
import androidx.fragment.app.FragmentTransaction
by
import android.app.FragmentTransaction
You will find the code on this deposit :
https://github.com/Amadou19/Android-Multiple-fragment-with-progressBar---Kotlin.git
/****************************/
fragment1.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c15d5d">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 1"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Click Me"/>
</RelativeLayout>
/****************************/
Fragment1
/****************************/
class Fragment1 : Fragment() {
interface Fragment1Listner {
fun onClickFragment1()
}
var a1: Fragment1Listner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment1, container, false)
view.findViewById<Button>(R.id.button).setOnClickListener {
Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
a1?.onClickFragment1()
}
return view
}
}
/****************************/
fragment2.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5d80c1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 2"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Click Me"/>
</RelativeLayout>
/****************************/
Fragment2
/****************************/
class Fragment2 : Fragment() {
interface Fragment2Listner {
fun onClickFragment2()
}
var a2: Fragment2Listner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment2, container, false)
view.findViewById<Button>(R.id.button2).setOnClickListener {
Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
a2?.onClickFragment2()
}
return view }
}
/****************************/
fragment3.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fab049">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 3"/>
</RelativeLayout>
/****************************/
Fragment3
/****************************/
class Fragment3 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment3, container, false)
return view }
}
/****************************/
activity_main.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.amadoutirera.progressbar2.MainActivity">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Base.Widget.AppCompat.ProgressBar.Horizontal" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progress_bar"/>
</RelativeLayout>
/****************************/
MainActivity
/****************************/
class MainActivity : AppCompatActivity(), Fragment1.Fragment1Listner, Fragment2.Fragment2Listner {
lateinit var progressBar : ProgressBar
private lateinit var fragment1: Fragment1
private lateinit var fragment2: Fragment2
private lateinit var fragment3: Fragment3
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*------------------------------------------*/
progressBar = findViewById<ProgressBar>(R.id.progress_bar)
progressBar.max = 90
progressBar.progress = 30
/*------------------------------------------*/
fragment1 = Fragment1()
fragment2 = Fragment2()
fragment3 = Fragment3()
/*------------------------------------------*/
fragment1.a1 = this
fragment2.a2 = this
/*------------------------------------------*/
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment1)
.commit()
}
/*--------------- Onclik interface implementation -------------------*/
override fun onClickFragment1() {
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment2)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit()
progressBar.incrementProgressBy(30)
}
override fun onClickFragment2() {
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment3)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit()
progressBar.incrementProgressBy(30)
}
/*------------------------------------------*/
override fun onBackPressed() {
super.onBackPressed()
if(progressBar.progress ==60 || progressBar.progress ==90) progressBar.incrementProgressBy(-30)
}
}
try to Put your code in onCreateView() method.
Hope it works!!
Try
parentmain_progress?.post({
parentmain_progress?.progress = 3
})
instead of parentmain_progress?.setProgress(3)

Categories

Resources