I have been working on an app that adds views programmatically to a linear layout.
The problem is if I add too many views it will go off the screen.
I would like to know how to check if a certain child has hit the end of the same view group so I could add it into another layout (a linear layout below the first one) before it "flows" and go off the screen. How might I accomplish this?
Rather than reinvent the wheel yourself, I suggest that you check out the FlexboxLayout project by Google: https://github.com/google/flexbox-layout
A FlexboxLayout will automatically give you the behavior you're describing, plus the potential for much more.
Well, there are a good number of ways you could implement this in android other than going through this hustle. What ever you are trying to do at the moment may fall under one of the following cases.
Creating views programmatically most likely means you have a dynamic data set probably from an external source.
Your dataset is limited or static but just more than the average screen can display.
if any of the above apple then you are better off using a ListView or RecyclerView (Recommended). That way your data is full displayed as a scroll-able list and you don't have to worry about some items or views not showing or going of the screen. This can range from simple string list to complex nested views.
This will be very efficient as it will automatically handle optimization and usage of memory as well as performance.
Related
I have been digging around in the search view source code for quite sometime now trying to understand how exactly does the search view even have a user interface to be rendered. First of all here is the source code:
SearchView source code
So what I don't understand is how does the user interface element of the search view even get rendered when the search view doesn't even have any onDraw() method. All that I can see is responsible for the display element is a bunch of view at the start which are in the constructor, the SearchView gets a reference to and change the background and set the image of these view. If all that I can see was done is getting references to some views and changing the background as well as the image without having it within the proper view hierarchy then how exactly does it even get rendered?
I understand what you are probably wondering why do I even need to understand this. Well I want to understand this so I can create my own custom search view. Since I only need like 2 function on my search view I figure it would be a lot better to make one that suits my need instead of the thousand of lines of code in the source one. Plus, I want to create one that I know I will understand how to use not the complex default one.
I have a program I am writing where I have 2 tabs, when one is clicked, the recyclerview on the page updates with information X (Name, email, etc). When they click on the second tab it updates with information Y (phone, password, etc).
Is it better resource management / smarter to create 2 separate recyclerviews / adapters and change which is active? Or is it better to use 1 recyclerview with 2 adapters that adjust themselves dynamically?
My goal is to make it effective, manage resources efficiently, and also be able to change between the two very quickly.
-Pat
I would create a Fragment for each tab (could be the same class). Don't worry about the resources, recyclerviews are highly optimized.
For me its the more logical approach and it's easier to understand, especially for others if you're working in a team. Also you could change the view much easier and more dynamic in future, for example in a tablet/large screen view.
greets
I am using ScrollPane on my JavaFX application. The Scrollpane has TilePane as its content, having thousands of tiles displaying. The scolling is too slow. How to increase the scrolling of the ScrollPane? Thanks.
Update - recommended approach - use a ControlsFX GridView
The ControlsFX, 3rd party JavaFX controls project looks like it has a control which fits your requirements. ControlsFX names the control GridView. It is a virtualized control as recommended in the solution in this answer, but it comes pre-built so that you don't have to implement your own control or heavily customize ListView to get this behaviour.
Here is a description of the GridView (from it's Javadoc):
A GridView is a virtualised control for displaying getItems() in a
visual, scrollable, grid-like fashion. In other words, whereas a
ListView shows one ListCell per row, in a GridView there will be zero
or more GridCell instances on a single row. This approach means that
the number of GridCell instances instantiated will be a significantly
smaller number than the number of items in the GridView items list, as
only enough GridCells are created for the visible area of the
GridView. This helps to improve performance and reduce memory
consumption.
Because each GridCell extends from Cell, the same approach of cell
factories that is taken in other UI controls is also taken in
GridView. This has two main benefits:
GridCells are created on demand and without user involvement,
GridCells can be arbitrarily complex. A simple GridCell may just have
its text property set, whereas a more complex GridCell can have an
arbitrarily complex scenegraph set inside its graphic property (as it
accepts any Node).
Obsolete Answer
Rather than using a TilePane for your content, consider using a ListView, which is a virtualized component (e.g. it only creates and renders the nodes which are visible on the screen, rather than all possible nodes). As long as you implement this correctly, it will likely be more efficient than a single large TilePane with thousands of nodes. If ListView doesn't quite fit for you, you can make use of the concepts in the ListViewSkin (you can check out it's application source), to create your own similar virtualized layout for your application (don't expect that to be particularly easy to do though).
how to change the orientation of the ListView?
list.setOrientation(Orientation.HORIZONTAL)
But maybe your question isn't about orientation (I'm now a bit unclear what you are really asking).
Maybe what you want to do is create a normal, vertically oriented ListView of custom Row objects, where each row would consist of a HBox or single row TilePane. Basically, you will need to do more work to get the performance you want, you won't find an out of the box control which will do exactly what you want and perform well.
Unfortunately, providing full example code to completely solve your issue is outside of reasonable scope for a StackOverflow answer.
I'm currently developing an android app and I'm just wondering if there is a way to move the elements around on the graphical view without moving other elements around.
It's really a pain to get everything lined up the way I want it when moving one edittext moves 3 others too.
Thanks all!
This will depend on what your layout is so the simple answer is no. With certain layouts and attributes it might work but everything kind of depends on everything else.
If you're using a RelativeLayout then a View will be positioned relative to certain other views so this will cause problems.
If it's a LinearLayout then moving one to the top is going to move other things.
Solution
Do yourself a favor, learn to design them in the xml and don't use the graphical editor to place the Views/ViewGroups. I use the graphical tab only for checking how it looks. Using xml is easier and more flexible, IMHO
I'm trying to write some automation scripts for an app I have. I've done the tutorial on Robotium's site and have a basic understanding on how I can automation. However from what I can tell regarding the app I'm testing is by using the android hierarchy viewer I see that all of the views have no ids that were explicitly defined.
As you can see from the screen capture there are views upon nested views. The IDs for them read like 0x17e0 or 0x17de. How can I reference these, specifically, in a robotium script? The end result is I'm trying to get it to fire a click even on one of the Text Switcher views. So far I've only been able to make it work if I give it a pixel point to go to, or if I give it the text that appears in the button (but the text is dynamic and would make for a poor test).
Do I have to use the getCurrentViews() to filter down to the text switchers? Or do I have to figure out a way to traverse the entire tree going from FrameLayout>RelativeLayout>FrameLayout>LinearLayout>TextSwitcher ?
If I have to traverse the entire tree how do I get view upon view upon view?
While I couldn't get the ViewGroup() and getChildAt() methods to work for me I ended up doing something different:
// Grabbing all the LinearLayout views found under view with with id of 'content'
ArrayList<LinearLayout> linearLayouts = solo.getCurrentViews(LinearLayout.class, solo.getView(R.id.content));
// The 4th item in the linearLayouts list is the one I need
View pickerList = linearLayouts.get(3);
// Grabbing the buttons in the pickerList
ArrayList<TextSwitcher> buttons = solo.getCurrentViews(TextSwitcher.class,pickerList);
// Now I can click on the buttons
solo.clickOnView(buttons.get(0));
I will say this is slow. It takes about 10 seconds for the first button click to fire. But once it goes it flys.
I would vote to say you really probably want to get some IDs added somehwere in the hierarchy, it is a one line change that will make your life a hell of a lot easier.
But i am not without help if for some reason you cannot get this done, to do this you are going to have to walk the entire to get to the view you want.
Getting the top level view, you will be able to cast it into a ViewGroup. ViewGroups have a method called getChildAt() which you can then use to get a child at a given index, the index is 0 based and will match what you see in the hierarchy viewer so you can chain together theses commands to get to the view you want to interact with.
I have not used Robodtium very much, but i know that AndroidViewClient does exactly what you want. Here is a code snippet that dumps Id of the home view:
ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
Here is the result dump:
com.android.launcher2.CellLayout id/cell3 None
com.android.launcher2.ShortcutAndWidgetContainer NO_ID None
com.android.launcher2.BubbleTextView NO_ID Email
com.android.launcher2.LauncherAppWidgetHostView NO_ID None
android.widget.AnalogClock id/0x7f0f0014 None
com.android.launcher2.BubbleTextView NO_ID Camera
com.android.launcher2.BubbleTextView NO_ID Settings
com.android.launcher2.BubbleTextView NO_ID Gallery
com.android.launcher2.LauncherAppWidgetHostView NO_ID None
android.widget.LinearLayout id/0x7f080167 None