I have an image view in my Layout
<ImageView
android:id="#+id/img"
android:layout_width="340dip"
android:layout_height="240dip"
android:layout_marginBottom="60dip"/>
I am setting an img.setimageresource(R.drawable.apple);
Now what I need is I have a i value which is incrementing in onclick of a button as soon as i value is incremented, image should be displayed and its is displaying. Again when i value is incremented it should display the same image just below the 1st image and so on...
Make another xml having Imageview only. and remove this Imageview from parent xml. Now use inflater to inflate the imageview xml and add this view in parent xml's layout.
EDIT:
First make two layouts, parent and child. Parent layout has only on linearlayout(vertical) and child layout has only one ImageView. Now Use an Inflater like this:
LayoutInflater inflater = (LayoutInflater)getSystemServices(Context.Layout_InflaterService);
Make a view of child layout like this:
View view = inflater.inflate(R.layout.child,null);
Add this view to parent xml's layout.
layout.add(view);
Hope its clear now.
Related
When creating an ImageView for example, I know I can create it within a Layout:
Example a LinearLayout:
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
</LinearLayout>
But can I define an View outside a layout, and then add it to any other layout?
I want to have a RelativeLayout which adds/removes views programmatically and dynamically, so that the RelativeLayout starts off with no views inside it, then I add some, remove some etc. Would there be any way to do this? Or is it better just to have these views already inside some other Layout, and then I add the Layout (whatever it is--containing my view(s)) to my RelativeLayout?
What you are searching for is LayoutInflator
Create a xml file - buttons.xml
<LinearLayout...>
<ImageView
android:id="#id/hello_world_id"/>
In your activity access it by
final LayoutInflater inflater
= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.buttons,
null);// this is a layout in your master activity
lLayout = (LinearLayout)findViewById(R.id.layout1);
lLayout.addView(itemView);
Hope this helps.
I'd like to fill dynamic ImageViews in a LinearLayout. In addition i like to include at the top of every ImageView a TextView. Implemeting this in a static way with the xml file is no problem but I don't got any idea how to implement this problem?
For example I select 5 pictures out of the gallery and show all pics side by side. At the top of every pic you can see the name of the pic.
[EDIT]
LayoutInflater inflatter=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myLayout = (LinearLayout)findViewById(R.id.mylinlayout);
// AbsoluteLayoute has got an ImageView and a TextView
AbsoluteLayout test = (AbsoluteLayout)inflatter.inflate(R.layout.test2, null, false);
TextView texttest = (TextView)test.findViewById(R.id.name);
ImageView image=(ImageView)test.findViewById(R.id.image);
texttest.setText("test");
myLayout.addView(test);
Sounds like you're trying to make a scrollabe list, where each item in the list is a picture and a title text, right?
Have you thought about using an ArrayAdapter in a ListView and in the adapter's getView() you can inflate a layout file that contains an ImageView and a TextView?
You could create a custom layout in xml with the ImageView and TextView arranged the way you want for each item.
AbsoluteLayout
TextView (above ImageView)
ImageView
Then, as you dynamically get the images, you inflate this custom layout using a LayoutInflater and assign the TextView with the desired text and the ImageView with the desired image. Next, you take the inflated view and add it as a child to the LinearLayout. Iterate through your images and do this and you should have what the desired appearance.
Edit: If you have many images that might require scrolling, you would have to wrap the LinearLayout in a ScrollView to allow scrolling if the LinearLayout gets too large.
As someone else just mentioned, a ListView will take custom xml layouts and inflate them the same way using adapters and such, and it will handle scrolling for you. ListView link and example.
I want to cast and ImageView to RelativeLayout. Because the RelativeLayout is the parent View and there are many images as Child views in this. So I have written code to download image and set it to the Image view. But the case here i want to set this downloaded image as background of the parent view that is of RelativeLayout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.bg_image);
ImageView bgImage=(ImageView) findViewById(R.id.bground_image);
downloader = new ImageDownloader();
downloader.download(item.imageurl(), bgImage);
/// Image is now set in bgImage//
///////// Now here i want bgImage to be set as a background of layout////
thankx in advance
To set a background image you can use something like
layout.setBackground(new BitmapDrawable(<your image>));
Best way I think is take one another Imageview with same dimensions as Relativelayout(may be fill parent) with scaletype fitxy and set the image to this as you are doing lazy loding and cant pass that relativelayout to loader....... this fitxy will work as background of parent layout
Or another way is change your downloder code to accept relative layout and set background once you have the image
Just add the image to the background of RelativeLayout. It should be 1st in order to be behind all other views in the layout. Then you can use it with code that expects an ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="#+id/an_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--other views go here-->
</RelativeLayout>
And no, you can't cast between these types, obviously. This is Java!
No this is simply not possible because these are completely different types.
You can cast any RelativeLayout or ImageView to View, because this is the base class for all views.
But what you are trying to do should be a little different. You can inflate the ImageView from the RelativeLayout because it is the parent.
I will add a code snippet later.
Update :
// First inflate your layout
View layout = (View)findViewById(R.layout.relativelayout);
// Then inflate the imageview from the layout
ImageView imgview = (ImageView)layout.findViewById(R.id.bground_image);
// Then do your download logic with the imageview
downloader = new ImageDownloader();
downloader.download(item.imageurl(), imgview);
Let me know if this works ;).
I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);
i want to add Layout Dynamically on Add button click and on Dynamic Layout show Datepicker,Timepicker Dialog and set value in given Edit Text. show in image on Date Click set Date Right side . Here problem Start when Add Second Same layout and set date it set only on newly created layout
For example you need to create xml layout file with ScrollView and LinearView inside.
Then in your Activity class:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = inflater.inflate(R.layout.your_layout, null);
setContentView(main);
LinearLayout linear = (LinearLayout)main.findViewById(R.id.linear_layout);
and then in onClick method just:
View yourView = inflater.inflate(R.layout.yourView, null);
// Do whatever you want with your View, set up some variables etc.
and to add your view to main view:
linear.addView(yourView);
I know that this is not a direct answer to your question, but maybe will help you with dynamically adding Views.