best method to frequently change view inside layout - android

I want to add and remove some views frequently like
Recycler view,textview,seekbar,customview,imagebutton etc.!!!!
I know following methode to perform it
Add all view to layout and just play with Visibility.GONE, Visible and other..
using layout params add and remove view...
Use View.inflater to add and remove predefined XML views(I'm using)
So question is
1.Is there any other method to do it?
2. Which one you prefer and why?

When you make Visibility.GONE all the views and associated resources such as image, sound files will be kept in the memory and if you have a lot if resources that may slow down your application. I think better way would be to use Fragments.
Check out this link. Hope this helped

Related

How to handle dynamic view created in Recycler View

Actually, I am working on RecyclerView that shows some details along with images and all data comes from a database.
In my RecyclerView, I have used three diffs layout and each layout is used according to data coming from the database that means if only one image comes from database then one_image.xml layout comes to play, if two then two_imag.xml layout comes to play and if more than three then the third layout I am using.
My apps working fine for some extents but when i am trying to scroll down further, it get crashed. I come to know that the problem is on OnCreateViewHolder, meaning it doesn't get called when I go down further in Recycler View.
I searched alots on google but nothing work for me. Is it possible to call OnCreateViewHolder every time or some other way to solve this problem?
Thanks in advance
Sounds like the best option would be to use one layout file that has all three image views but just default them to visibility "gone" so they dont take up space. Then when you need to use them, set them to visibility "visible" when you are creating your viewholder.
Without seeing any actual code it hard to be more specific.

Is it possible to make a sample android view (Card View) in XML and store it in a a variable then use it again and again?

I want to use a cardView having some internal structure at multiple places. Is there a way to make a cardView in an extra layout XML file and then store it in a variable programmatically and later use that variable to show that cardView where ever I want by adding it dynamically?
I want to make that cardView sample in XML as it much easier to edit there.
Currently, when I try above method I get an error "view already has a parent" for obvious reasons. I can definitely copy paste code but i would like to know if there is any smart way.
Any help will be appreciated. Let me know if you need any other detail or find it hard to understand my question.
you can do it.......
have a separate xml for your card view...........let say card.xml
in your main/activity xml make/select a parent layout (Linear / Relative) where you want to place it......or may be different view..
private void addMyCard(){
ParentLayoutType parent=(ParentLayoutType) findViewById(R.id.idGivenToParent);
View yourCardViewName= LayoutInflater.from(this).inflate(R.layout.card,null);
parent.addView(yourCardViewName);
}
Hope it helps

How can I get the underlying XML from a dynamically modified view?

Thanks in advance...
I allow users to drag/drop views(text, image, etc) onto a RelativeLayout to create 'presentations'. I would ideally like to extract the resulting xml so the presentation can be saved to a fragment or .axml file, and recreated later either on the same device or a different one. I know I can iterate the children and manually create the xml I need, but it seems to me that since it must exist in the relative layout, that there is a way to extract it. Any ideas are appreciated!
You can't extract the XML for dynamically generated views. But what you can do is use XMLSerializer to make an XML of that view that you're generating. Check out my question.
To be frank, you can not inflate that XML dynamically (in future if you were going to do that). So, make sure you know what you're doing.

Android: Best way to fill Activity with new data

i got the following "problem".
I want to have an activity thats shows me the standings of some teams at a specific gameday.
therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.
Whats the best way to create this activity?
assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml
assemble the layout during runtime, add ids, update via ids
assemble the layout during runtime. on update remove old views and create new ones
assemble the layout during runtime. on update restart the activity
just whant to know which one is the best. or is there any other way to achieve that
thx Cheetah
If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.
In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.
Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.

Android Dynamic Content Design

I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);

Categories

Resources