Define and Populate a Layout for multiple activities - android

I have an app where I have to add an informative box in many acivities.
This box is basically a LayoutView with various TextView and an ImageView.
Is there a way to define he whole box in a single class and add this everytime that I need?
I could copy-paste both Layout xml code and methods tha populae the Layou for each Activity, but I want to avoid this (I really hate the redundant code).

Why not use Fragments? They are meant to be reusable.

yoh have tow option
the one is to make your box as different layout then you can include it on other layout
http://developer.android.com/training/improving-layouts/reusing-layouts.html
the seacond is to create custom component
the link below will help you toward this
How can i create custom controls?

Related

best method to frequently change view inside layout

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

How to reuse Android XML layouts with minor differences?

Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.

Android: Creating custom view and adding it to layout dynamically

I want to create a custom view with some text and two buttons all on one line. I need to be able to add multiple (any number) of these views to an existing layout dynamically (needs to be able to scroll). I want to pass a custom object to the view and set the text and buttons. I need access to the button event handlers from the activity. I've looked a little into custom views but I'm still at a loss for how to do what I want. I'm used to .NET custom controls, and I'm looking for the same effect. Any help or example code would be greatly appreciated.
What you want is custom compound view. You should write you own class (usually extending one of the layouts) and whole behavior, inflate the layout the way you want etc.
More of it: http://developer.android.com/guide/topics/ui/custom-components.html
This one helped me a lot too: http://javatechig.com/android/creating-custom-and-compound-views-in-android-tutorial
If you use list activity or list fragment, you will automatically have the many features you have asked for. You only need to create a adapter class for your listview. You can define your layout for your row view(buttons, text etc..) Try to look at the examples on the web for cusom adapter and lists.

Multiple layouts for the Fragment

Can I choose different layout for the Fragment based on the Button that has been clicked on the Activity, or should I create one Fragment class for each Button?
I know that Fragments exist to accommodate different UI but in my case I have many buttons that on click display a FrameLayout and I was thinking if it is possible to save time from copy/paste ?
I'd say that depends on how your layouts/fragments look like. If they are huge and very similar it could be better to have one layout. You could also think about using <merge /> or <include/> in that case.
This way you can maintain equal parts in (sub-)layouts without the need of code repetition.
Have a look at Googles site here.

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