Android: Custom view based on layout: how? - android

I am building a Android app and I am a bit struggling with custom Views.
I would like to have a reusable View that consist of a few standard layout elements. Let's say a relativelayout with some buttons in it.
How should I proceed. Should I create a custom view class that extends RelativeLayout and programmaticly add those buttons? I would think that's a bit overkill?
What's the way to do it properly in Android?

Here are some rough steps regarding one way to create a custom aggregate view:
extend RelativeLayout
Provide a constructor in your new class that accepts Context and AttributeSet, making sure to call the superclass first. Do no add anything at this point. Wait until the next step.
override the onFinishInflate method, where you can add your contents through Java code or inflating an XML resource
Add any event handlers, etc
Optionally create a resources file if your widget will require attributes to be set.

Related

Define and Populate a Layout for multiple activities

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?

How to dump as XML programmatically generated Layout

Is there any possible method to dump programmatically generated Layout?
For example I create Layout
LinearLayout mainLayout = new LinearLayout(this.mContext);
//...some code here
mainLayout.addView(picker);
mainLayout.addView(mOldColor);
mainLayout.addView(separator);
mainLayout.addView(mNewColor);
Now how to dump to Lod.d for example and get XML representation of Layout with attributes?
Afraid not. Views themselves know nothing about the xml they came from, so there's no way to force them to serialize back to xml.
Now if you wanted to do it on your own you can walk a view hierarchy fairly easily. But since views don't turn in to xml, you'd have to query each property individually and build the xml by hand. And it wouldn't work at all for custom views that could have custom attributes. And you'd have to use reflection to get the class name for the view type in the xml. Basically a lot of work for something fairly fragile.

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.

How can I allow multiple activities to share the same inflated "included" view?

I see this question here, and it makes me wonder if what I'm asking isn't really possible:
How to share a view across various activities
Basically, I have a common footer view that I'm inflating (including) in all of my views. However, it uses the same repetitive code to do that. My thought was to create a parent activity class to do this, but it doesn't seem correct to have one activity render the view of another. So should I just create a utility class of some sort, or is there a better way?
You can include other layout XML files directly in another layout file. So whenever you set content to a layout file, along comes your footer for the ride.
If your footer needs code to drive it, just create a custom class for it along with the layout file. Then perhaps during instantiation you can drive the code that needs to execute.
This is a blog of how to do it.
include is very useful while reusing View components.
But remember, if any problem occurs while using include tag, wrap the included view by an arbitrary layout.
If I understand your question, another way of having multiple Activities use the same View instance is by doing something like creating your own Application class (it's seriously easy).
MyApplication extends Application ...
#Override
public void onCreate(), onConfigurationChanged(), onLowMemory(), onTerminate(), getIstance().
As there is only a single intance of "Application" that is statically available it makes it a good place to store and share various objects that need to be passed around.

Adding a view on runtime to a ViewFlippers

I have defined two views ExampleView1, ExampleView2, ExampleView3 and ExampleView4 in resources.
In my Activity I have an empty ViewFlipper. Based on doing some logic I want to add either ExampleView1 and ExampleView2 to the ViewFlipper and show the view.
Later I want to add based on internal logic either ExampleView3 and ExampleView4.
How do I do this? Is there some tutorial or can someone help me with example code?
Just use the addView method, which ViewFlipper inherits from ViewGroup. If your views are custom ones, you will have something like this:
flipper.addView(new ExampleView1());
On the other hand, if the views are defined inside an XML layout, you will have to inflate them first:
View view = LayoutInflater.from(context).inflate(R.layout.your_view, null);
flipper.addView(view);

Categories

Resources