Android: Best way to load views dynamically in Android 2.1 - android

I am developing an application in Android 2.1 which requires loading of views dyamically based on button click. Here is the rough layout of my Activity.
Say on Clicking button1, it should display imageview, and on clicking button2, it should show listview, for button3 - linearlayout of button and images, and so on. All the views should be loaded in the provided white space only.
The approach I thought to follow is "Inflating layouts".But on clicking another button, old inflated layout should be removed and new inflated layout should be visible.
Are there any better ways to achieve this?? If Inflating layouts is the only better way, how can I remove old inflated layout and load new inflated layout.

You can use a ViewFlipper to hold the three layouts and simply call showNext() or setDisplayedChild() to determine which layout is shown.

Inflating is good when you have more complex layout than just one ImageView or so. In this case you should just create an ImageView dynamically and add it to "the white space" (it should be some ViewGroup (i. e. RelativeView)). In this case:
mRelativeView.removeAllViews();
ImageView newView = new ImageView(mContext);
newView.setImageResource(R.drawable.someImage);
mRelativeView.addView(newView);
When having more complex layout, do it with LayoutInflater (but remember inflating is very slow):
mRelativeView.removeAllViews();
View newView = LayoutInflater.from(mContext).inflate(R.layout.some_layout, null);
mRelativeView.addView(newView);
In above examples mContext can be current Activity for instance.

It sounds like you're trying to do something very similar to TabHost, try reading on TabHost here:
http://developer.android.com/reference/android/widget/TabHost.html
And see the tutorial here:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
I think you'd better off using built-in components like this then implementing something similar on your own

Related

How to remove a custom view and rearrange linear layour

First of all I will explain my scenario.
I have an activity with a linear layout and I am inflating that linear layout with custom views. In the custom view I have a remove button to remove the particular child.
I know to remove the view I have to add an onClick Listener to the button in the custom view. But what I am not sure about is that how can I remove the view from its parent view from that onClick Listener.
I am populating the linear layout from items stored in shared preferences. So I thought first to update the shared preference by removing the string of the particular custom view I am clicking. But I don't know how I cn manage to re-populate the linear layout, since the function is in the parent activity.
I am a complete newbie in android. Thanks in advance
update
I'll make the scenario a little more simple.
I have a custom view with two imagebuttons - update & remove.
If i click in the remove button, then the custom view should be removed from the linearlayout.
And if I click in the update imagebutton, a function of the Activity which hosts the Linearlayout should be called with the text in the customview as a parameter.
How can i do this. I tried many ways, but failed :-(
I found the answer finally :
For the first requirement, ie., to remove the view, I used:
LinearLayout parentLyt = (LinearLayout) RecentSearch.this.getParent();
parentLyt.removeView(RecentSearch.this);
Thanks Very much to Piyush Gupta (#piyush)
For the second option, to access the hosting activity, I used getContext() and casted it to my activity class, then called the method.
Thanks every one who replied.

How to display several icons in row

I have a button. It calls another activity with custom listView, that contain images and checkBoxes, marked positions are passed to another activity. It's works well, but I need that retrieved several icons to be displayed in a row into the button, as shown:
create a xml file named row_lauout.xml and use LinearLayout in it with orientation horizontal and arrange all your button in that.
in your main.xml create a List and aligned it as per your design.
in java code write a class called ListAdapter which extends either BaseAdapter(or any other adapter based on your need) and override getView method.
inside getView method inflate row_layout.xml as
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,
null);
then implement onClick for all buttons in getView method.
one more suggestion search for cell reuse concept in android. it will help to make scroll very smooth.
Put ImageViews in a LinearLayout? Your question is really hard to understand, but if I take it at face value that's your answer.
In your XML layout, use a LinearLayout with a Horizontal orientation.
The items that you place within the container should have a fixed width or set to wrap_content

How to show and hide layout dynamically

I want to make layout that expands dynamically, like some kind of menu.
It should look like this
http://imageshack.us/photo/my-images/845/dialog.jpg/
Step 1:
When I click on TextView it should inflate the new layout, remove transparent one, and move text to the left side.
Step 2:
When I click again on TextView (it's vertical custom TextView btw) it should go back to Step1
I want to put this layout into custom dialog and it should always be on my right side of the screen?
Any ideas how to solve this?
I can do this with two layouts and changing contentView of dialog on every click, but it seems like a very dirty solution. Is there some nice and fancy way to do this?
The simplest solution is probably just inflating all the Views, set their visibility accordingly, and move the TextView when the user clicks on it.
From the looks of it, it seems like you want to slide between a lot of views by clicking the TextView? If so, you might want to look into something called ViewPager, with a little customization you might be able to archive that.
Create two different layout's mentioned in images like 1) layoutone.xml 2)layouttwo.xml
Now crete on linearlayout in add that layout in your alertdialog. Also add layoutone in that linearlayout by inflating that layout. Now on click of that textview just removeallviews from that linearlayuout & inflate second layout & vice varsa.

Graphical Layout shows first view of ViewFlipper, how to see others?

I am using Eclipse and a ViewFlipper. In Graphical Layout, I want to see the second, third, and forth layouts of my views - right now, I can only see the first view. Any suggestions?
If I'm understanding you correctly, you want to see each view in the 'Graphical layout' tool? The way I do this, is instead of having all the layout work done in one xml (where your viewflipper is) I make each view a new layout xml. And then have each view (xml file) included into the view flipper by using this....
<include
layout="#layout/layout_media"
android:id="#+id/flipper_media" />
Hope this helps.
just put each layout in relative layout or linear what ever you are working with then with each layout you will work with the first one in the order and etc.. then at the end put each layout in the order you want later
I had to subclass the ViewSwitcher class to display an indeterminate ProgressBar until data is ready to display in the second view. I used isInEditMode() to determine whether I was actually running the app or just previewing in AS.
You should be able to add a custom attribute to choose which child to display. This might look a bit overkill, but if you happen to already have to subclass your ViewSwitcher or ViewFlipper, i think it is not a big deal.
I will try to put an example later.

creating layouts dynamically and getting control on each layout

i have to create a layouts dynamically depending on the some action for example depending on action i have created 5 layouts which is horizontal scroll and i want the control of each layout how to go about it please give me some suggestion
Your requirement is not entirely clear. If you want to load layouts from a xml resource fiel you can use LayoutInflater. This will also return a handle to the loaded layout that you can use for further manipulation.
You should use the layoutInflater to load the layout.xml files, later in List add the handlers in for loop.
unless you set the inflated layouts to some Activity to display it has no effect. Hence consider ViewStub and inflate the things inside the ViewStub.
If you are inflating and adding it as a part of some component ( like listView items), then you can get the references using a global ArrayList, to store all those handles.
I am unable to understand your question fully, can you please elaborate your problem?

Categories

Resources