Let's assume we have a fragment and somewhere along its contents we have a part that is supposed to display X views.
We don't know the X number since it is not fixed.
Each of the X views can completely different. E.g.
Separator
TextView
Separator
Linear View with children
Separator
TextView
Separator etc
you get the idea.
So I was thinking
1) should this be build as some kind of custom component? How would I pass the data?
2) I originally thought this should be an inner fragment but then I thought that I don't care about the activity lifecycle about this.
3) Another approach is to just have a small function inside my fragment to add these programmatically but then how could I make this reusable?
What is the best strategy on these kind of designs?
Related
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.
I want to create a grid or table of fixed number of rows and columns(Ex. 6x6 grid) in a layout of Android Studio. I tried using GridLayout and GridView but it requires you to put 36 EditText(Plain text) Views in it for creating a 6x6 grid. Same is the case with TableLayout where you can only insert TableRows but cannot fix the number of columns.
Bottom line is, I want a 6x6 grid which has only a 6-letter word(one character in each cell) in the beginning and one letter is typed in every turn to make a word with the existing letters.
For this, from any cell, I should be able to read the characters in the adjacent cells. I don't think creating a GridView or TableLayout and creating 36 objects of EditTextView will be the best idea. Is there a good and efficient way to do this? I need the .xml file code and also its Java class file's code.
Why you don't think that creating a layout holding views in Android framework is good and efficient way for exposing some data in a graphical interface? This is the only reason for which View class even exists. It's a main building material of your application's GUI.
Also you don't need to create it by hand. You just need to be able to address your ViewGroup (Layout) object from your Java code where you build your Activity instance. From there you have an addView() method and you can add views in a simple loop (notice that this way you can create grids of every size, not only 6x6).
Please familiarize yourself with official Android Developers site where you can read pretty much about anything relating Android Framework. For more info about layouts click here.
Try this library it might help you.
https://github.com/InQBarna/TableFixHeaders
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'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
My experience and good books had taught me that repeating of the same thing in code is bad. But when I use Android views, I often repeat the same thing even 4 times:
twice in XML:
<LinearLayout -1
android:id="#+id/lila"
...
>
...
</LineraLayout -2
and twice in code:
3 4
LinearLayout ll=(LinearLayout) findViewById(R.id.lila);
Of course, when I have to change the type of the view, I have to do it in four places, what causes errors. In the XML I could use refactoring to change simultaneously the class of layout or even of a view or widget. But that doesn't help much, because the most dangerous disrepancy, not catched by the compiler, can appear between code and XML. And later I have to waste time and look for the source of a runtime error.
Are there other possibilities to address views from layouts? Not so dangerous?
If not, do you know some trick to change or at least check all these places simultaneously?
Thank you.
Something that could help you (depending on case, of what you want to do for example with your LinearLayout) is declaring your Views as generic as possible. Here, in your example you could keep your LinearLayout as, ViewGroup if you do not need any LinearLayout specific methods. So, if later you decided to change that layout in xml, in RelativeLayout or FrameLayout, etc, you will not get any cast problems.
You could take the same approach for Buttons, or other Views, depending on what methods you use on them. For example if you just set an onClickListener on your Button, you can keep a refrence to it just to an View, (ex. View button=findViewById(R.id.button);), and later you could change it in a ImageView maybe, and still don't have any cast problems, since they both are extended from View.
So the solution in big lines : use a class at the highest level as you can.
for xml layout reusing, you can use include and merge layouts.
http://developer.android.com/resources/articles/layout-tricks-merge.html
As for Activity, you must use super class references as far as possible, because it provides you a flexibility to change specific object class.
Like instead of using we can
ViewGroup ll=(ViewGroup ) findViewById(R.id.lila);
now this can address relative layout, linear layout, table layout, etc.