Adding a view on runtime to a ViewFlippers - android

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);

Related

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.

Dynamically hide a viewstub when a new one is created

I made a viewstub that is inflated and created again many times. The layout it uses includes a "delete" button that hides the viewstub (or deflates it).
When I'm creating a new viewstub I call this code:
ViewStub eventStub = new ViewStub(this);
eventStub.setLayoutResource(R.layout.event_container);
eventContainerMain.addView(eventStub);
eventStub.inflate();
Problem is, in the viewstub layout the delete button is created with the new viewstub. So how do I make the delete button hide the viewstubthats inside?
Most importantly, How do I make other methods affect only the view its inside?
You aren't quite using view stubs correctly...
First, point your view to the layout you desire for the view you want to duplicate by using this:
ViewStub stub = new ViewStub(this);
stub.setLayoutResource(R.layout.viewStubLayout);
stub.inflate();
Next, go into your XML and make sure the buttons on your layout have the android:onClick option using the correct method, which is declared in your class.
For deleting view stubs, you don't actually delete them... you're supposed to use .setVisibility(GONE) or .setVisibility(VISIBLE) to manage if users can see it or not. This would be used in the method in which onClick is directed to. Also, using "this" when referring to the view stub your objects are in will allow you to manage what happens in the specified view the user clicks in.
You do not delete view stubs in the way that you want them to be deleted. When inflated, ViewStubs simply disappear from the parent and are replaced with a View object. If you want to use methods on the view that is created you can use the android:inflatedId attribute in the ViewStub xml file.
You say that the layout you are using has a "delete" button that "deflates" the ViewStub, but in reality what is happening is that you are deleting the View that is created when you inflate the ViewStub. Once a ViewStub is inflated it is deleted from the parent automatically and so after inflation there wouldn't be a Viewstub in existence to delete.
What it sounds like you want is to dynamically delete the View created by the inflated ViewStub. For this I suggest looking at Add and Remove Views in Android Dynamically?

When to use getparent()

In my custom view I need to use the getParent method and set the visibility on some of it's child views depending on my custom view's state. The problem is that I want to instantiate the child views just once. Where is the best place to do this?
I'm not exactly sure what you want to do. But if all of this is occurring in the same Activity screen why don't you just assign ids (e.g. android:id="#+id/someId" to your elements in the layout.xml file.
This way you can reference any element programatically in your code by calling:
View someView = findViewById(R.id.someId);
I am unclear why you would need to call getParent. If you are trying to manipulate views in a different activity then I think you will need to use a Handler.

Creating multiple objects of a view defined in the xml

I have to dynamically add a list of views (the views use RelativeLayout). Can I do this by specifying the view definition in xml and instantiate multiple objects off it? This gives the usual benefits of separating the view part from the code (for e.g., making it easy for the UI guys to alter things) or Is going the ArrayAdapter the suggested/only route?
are you saying that you want to do this?
View v1 = (View) findViewById(R.id.someView);
View v2 = (View) findViewById(R.id.someView);
If you do this, you will merely have 2 references to the same view; it does not create two separate View objects. However, if you want to make a vertical list of views, look into ListActivity. in this case you will make a layout xml that will be used for every item in the list. you will need to implement a ListAdapter, or use a SimpleArrayAdapter.
does that help?

Android: Custom view based on layout: how?

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.

Categories

Resources