Manage textview and switches inside tab fragments - java

So guys my question is simple, i'm migrating my app layout to android tabview.
I had all the code of the switches and textviews inside my activity_main.xml layout file.
Now i made 3 layout fragments resource xml files and i migrated everything there, plus i included the layout app bar in the activity_amin file
I left all the java codes of the layout elements inside MainActivity.java without changing anything.
Now the app crashes becouse all the layout elements declared inside the fragment layout files return null.
Do i have to set the layout elemnts codes inside the frgament java associated files?
Thank you

You should definitely use the Fragment.java-classes for addressing the gui-components of a fragment.
If you need a refresh for how to work with fragments you can read thru the codelab here: https://codelabs.developers.google.com/codelabs/advanced-android-training-fragments/index.html?index=..%2F..index#0
Best
Sebi

That's simple you probably working inside onCreateView of the fragment
try to work inside the onActivityCreate, because onCreateView the view is not ready and every thing will return null, so if you work on :
Kotlin:
override fun onActivityCreated
Java :
#Override
public void onActivityCreated
that dosen't happen.

Related

How to Show Custom View (FAB) On All Screen in Android

I want to show my custom floating button on all screen in my app without putting it in each activity.
Do we have some Global Activity like put a code once will show the Custom View all screen and hide/remove when app killed
I tried lot of things like Screen Overlay display over all apps
Please check code here - How to display custom view on all screen in android from my library
I've never tried to do it the Java/Kotlin way. But have you considered using Fragments?
You can declare xml layout and code for one Activity with a FrameLayout container and an FAB. The container holds all your screens and they switch between each other in the container. The FAB is on top and therefore will be displayed no matter the screen.
The Activity has a reference to any screen that might be displayed in it, so the FAB can behave accordingly.
Not sure if this helps. Perhaps if I see some pictures, I could suggest better.
Related Read:
Creating a fragment: here
Fragment Transactions: here

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

XML layout display Order

I have more than one XML-layout on my app and i am looking for a way to change the order in which my XML-layout displays when running my app
like which one of the layouts is displayed first ?
I think I didn't understand your question exactly but you can have as many as layouts you want. your activity shows the layout inside oncreate with setContentView(R.layout.yourLayout). If you want some particular parts in your layout, you can use fragments.

Android StackScrollLayout

I need help creating a Layout like the one Android uses to render notifications inside it's notification Panel.
Basically what happens at the end of the view when notifications don't fit the screen anymore is what interests me. Here is the view in Android source code.
Any suggestions or ideas are appreciated. Thanks!
1-Include the view inside your layout xml
<com.bartoszlipinski.flippablestackview.FlippableStackView
android:id="#+id/stack"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2-FlippableStackView is based on the specific PageTransformer used with the ViewPager. Therefore to fill the View you can use just a typical implementation of a PagerAdapter. In your onCreate method (or onCreateView for a fragment), setup all the parameters of the FlippableStackView.
FlippableStackView stack = (FlippableStackView) findViewById(R.id.stack);
stack.initStack(2);
stack.setAdapter(mStackAdapter);
//assuming mStackAdapter contains your initialized adapter
3-Important Note: The current implementation of the library will display the elements from the Adapter in the reverse order. In other words: view at position 0 of your adapter will be displayed at the bottom of the stack and view at position adapter.getCount()-1 will be visible first (available for the first flip).
for a working example and reference check the library here
FlippableStackView

Viewpager with listviews

Has anybody had any luck with a view pager switching between fragments that contain listviews? In particular, the listviews I am working with inflate two separate layouts to get the desired effect. However, to my understanding this is causing the viewpager to disappear as well as the tabhost. I believe this to be so because it is working with other fragments that only inflate once.
Edit 1:
I was trying to see what would happen if I used one of the fragments that showed the tabhost and used the viewpager first. I would switch views and see the correct next one. However, shortly thereafter one of the fragments that does not show the tabhost or use the viewpager, for some odd reason, would load up.
Edit 2:
It's weird it is not even loading up on the right page. It should load up on 3 but instead it loads up on 2 and replaces the former screen that was actually supposed to be there.
Adding listviews to two fragments is very easy.
In short, you want to have a main activity that's the viewpager itself. Next, the viewpager is going to host two tabs (can be as many as you want, really) which will both contain separate layouts...each with a listview of its own.
Code
The first thing we need to do is add some classes. I've made a GitHub Gist of 4 classes that I'd like you to implement into your project. You'll need to change the package name and R class to meet your project's needs.
Gist: https://gist.github.com/Andrew-Quebe/b3e9f1d0f8223ba2f8df
Second, we need to make our host activity. This is what will show the tabs and toolbar. See this next Gist as I don't want to spam up this answer with tons of code.
Gist: https://gist.github.com/Andrew-Quebe/8add2fc064397ab8efe4
You've probably gotten an error in the MainActivity.java file due to a missing ViewPagerAdapter class. That's up next!
Gist: https://gist.github.com/Andrew-Quebe/fd70ee97c2e00d72f025
And finally, the tabs that'll show our listviews!
Gist: https://gist.github.com/Andrew-Quebe/3e2a87706c98a69e7353
My apologies for taking so long in my response...I actually took the time to build all this code and error check it for you. I had an example of tabs once before but it was outdated...you weren't the only reason I made all this code. The full project can be found on GitHub here: https://github.com/Andrew-Quebe/SlidingTabsExample
Hope this helps!
Edit:
Download the sample APK to see how everything looks: https://github.com/AMQTech/SlidingTabsExample/blob/master/APKs/Sample.apk?raw=true
First of all I would like to apologize. I pointed you all in the wrong way. I did some research and as it turns out you cannot have the fragment container (frame layout) in the activity layout already. All I had to do was put the fragment container into a different layout and inflate it when the time came to switch to another fragment class and that fixed it. Thanks to everyone.

Categories