Is it possible to implement a TabLayout with activities - android

I have a Tablayout kind of a bottom navigation, and I want that when the user clicks in a cardview that I have it sends him to another Tablayout but when that happens the bottom navigation does not appear anymore. That is only possible with activities right?

A TabLayout is just a view like any other. It can be inflated as a part of another view, or as the content view for a Fragment or an Activity. So, you're not restricted to using just an Activity with a TabLayout.
Now, if you want it so that when you click on a CardView you're taking to another screen without a TabLayout, you have a couple of options:
Open a new activity
Since this second Activity is going to have a different content view, it's not going to have the TabLayout from the first activity.
Hide the TabLayout and change the content surrounding the CardView
Since we're sticking with the first Activity, we're going to need to make all of our content changes to the views in the Activity. This means setting the visibility on the TabLayout to View.GONE and potentially lots of changes to the rest of the views depending upon what your layout contains.
I noticed that you didn't mention a ViewPager at all. Typically, this is what will be used with a TabLayout to swap between Fragments when you click on each tab. You could have all of your tabs' content in separate Fragments and then when you click on a CardView, just swap that tab's Fragment out for a different one and hide the TabLayout.
So, to answer your question, it is easier to just open a new activity, but it is possible to not use a second activity if you want to put in the work to modify your first one in runtime.

Related

Correct way to manage showing multiple custom toolbars?

The structure of the app is one Activity and multiple Fragments(+ 20).
The Activity has a Toolbar with a title and back button that i used in multiple Fragments.
Then there is two more types of Toolbars that i use inside multiple Fragments, one contains an EditText and the other one is an Expandable Toolbar.
An example would be:
Fragment A: Uses Toolbar from Activity. Show that one, hide if another is showing.
Fragment B: Has a Toolbar of it own, now i need to hide the Activity toolbar.
What would be the correct approach to show/ hide the correct toolbar?
Things i tried:
Using onDestinationChangedListener checking the id of the fragment and show/ hide the correct toolbar. The problem here is that it cause the screen to flicker and i have to check for every id.
toolbar.isVisible = destination.id in listOf(id1, id2...)
Hide or show the correct Toolbar from the Fragment. But then i need to set the toolbar on every Fragment.
(requireActivity() as MainActivity).showToolbar(true)
Removing the Activity Toolbar and creating a new one inside every Fragment. But i don't know if the best way and if it is expensive to constantly inflate the Toolbar.

Display slidingUpPanel across all activities

I'm trying to add a slidingUpPanel(Sliding up panel) across all activities. This panel consists of a viewpager which in turn consists of two fragments. One of these fragments has a recyclerView (more than 1000 items) and the other fragment has some data that changes dynamically depending on the users' choice. This panel is very similar to that of google play music and soundcloud. Now to display this panel I tested two approaches:
1) I created a base activity and added the sliding up panel to it and then extended rest of the activities to base activity. So this way I have to create only one panel and viewpager.
2) I included the sliding up panel in all activities. But this approach is quite unmanageable as I have create viewpagers for all activities and if data changes in one activity I have to write the code to reflect that change across all activities.
Now the problem with both the methods is that each time I open a new activity, a new instance of the viewpager and fragments is created. So, suppose if I have 1000+ items in the recyclerview fragment, switching activities takes more than 2-3 seconds, because each time new instance of fragment is created and the data is loaded all again. This will definitely result in bad user experience.
Is there any way by which the viewpager and the fragments are created only once(when the app starts) and are destroyed only when user closes the app? And data should not load each time user switches activities. I just want to reduce the activity switching time. Any ideas?
Thank you.
Well, for such ui elements as your sliding panel, which stays the same for many items it is preferable to have a single activity.
So if you have 1 activity, you can have a viewpager inside sliding panel, and that part stays untouched. Next, inside your activity you can have FrameLayout wich can be used to host fragments. Doing this you can achieve single instance of sliding panel and navigation between content with fragments.
Having some heavy data collections makes you wishing minimum recreation of that items.
Two approaches for this are (assuming you're using SlidingUpPanelLayout by sothree)
With bottom navigation view
Easy way to do it is creating a bottom navigation view and keeping it in MainActivity and attaching the sliding up panel layout to the bottom navigation view ( layout_above = bottomnavbar_id ) since bottom navigation stays throughout the app so sliding up panel will also have to stay with it
Without bottom navigation view
Create a frame layout inside MainActivity give attributes width and height as match_parent and create slidinguppanelayout and give attributes gravity="bottom"
make the frame layout stay above that slidinguppanellayout , use that frame_layout to show content your want to show from fragments
that's all

Android TabLayout & Fragments

I have an Activity which contains a TabLayout and a ViewPager. The first tab contains a calendar and the second tab contains a timer.
Tapping on a date in the first tab should display a fragment with some event details while simultaneously hiding or removing that menu tab.
To this end, how can I use a FrameLayout as a fragment container when my layout already contains a ViewPager? Should I create a new Activity to get what I want, or is there a more efficient solution?
What you are describing is basically a list/details pattern. In short, yes, you should create a new activity for the details. It is the expected flow of an android app and it makes the implementation for you so much easier.

How to implement horizontal view android studio

im trying to implement an android aplication when your activity, cointains 3 or more 'main windows' like in the image -> 'A'. 'B'.'C'. so how is posible make when you slide you touch screen change from A, to B, for example, i was thinkin in a horizontal view, and inside of each item use a relative layout, but im not sure, its my first time with this kind of problem, thanks.
Try using android ViewPager. Each ImageView would be inside a Fragment that would reside inside your Activity. Check out this example
Use Tabbed Activity template when you want to make this type of Activity. ViewPager, Adapter and tab layout will be automatically implemented for you. Just make small changes in your Adapter according to your needs and use one Fragment for one tab and you can create as many tabs as you want in an Activity.
If you want i will post an example of an Adapter as i just implemented an Activity with 3 tabs.
To get your basics strong on ViewPager, tab layout and Fragment sections adapter read out this link.

How to include extra fragments onto a ViewPager, in Android?

I am currently using a ViewPager with a TabLayout, the ViewPager as of now consists of three separate fragments.
I would like to be able to switch to a fourth fragment using a button on one of the already present fragments.
However, I do not want the fourth fragment to be accessible through the usual ways (aka scrolling) without using the button to reach the fourth fragment. Similarly, I would like scrolling to be disabled when I am currently displaying my fourth fragment.
What would be the best way to do so?
The current hack that I can think of would be to create a hidden fragment within my main layout. I can then display it when the button is pressed while hiding the ViewPager at the same time.
Are there any better ways to do this?
Include your ViewPager in fragment and onClick make replace for the next fragment, and you can return to ViewPager`s fragment onBackPress.

Categories

Resources