Drawing into a LinearLayout - android

I'm trying to draw an image into a LinearLayout, or TableRow, but I'm having already a XML FILE (I want to draw inside some LinearLayout). I don't know exactly how to do it. I was trying this:
TableRow mytable = (TableRow) findViewById(R.id.tablatexto);
ImageView myImage = (ImageView) findViewById(R.drawable.personaje1);
mytable.addView(myImage);
Thanks
Editing: I want to explaint it better. What I want to is creating a animation drawing images depending of a thread. So, I want to draw to some in LinearLayout from the Activity.

If you wan't to add dynamic layout in the container layout then you can use containerLayout.addView(childView);
childView is the dynamic layout which you can create using code or you can get a view of the layout using LayoutInflater class.

Related

How to combine dynamic an ImageView with TextView

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.

Android Overlapping Image Views

I want to create this sort of a view where the cross should be separate image view as I want it to be clickable. How can I achieve this ?
It would be great If I can create this view programatically as I am a dynamic list of images and I am programatically creating the image Views. All I need now is to add the overlapping imageview as well.
Thanks in advance
Use FrameLayout and you can overlay views on top of each other
Ex:
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
ImageView image = new ImageView(this);
iv.setImageResource(R.drawable.image);
ImageView cross = new ImageView(this);
i.setImageResource(R.drawable.cross);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
i.setLayoutParams(layoutParams);
frame.addView(image);
frame.addView(cross);
Create a RelativeLayout programmatically, which contains two ImageViews. Place your image in the first one, and your second image in the second one. Adjust the margins accordingly to place it in the top right corner.
First create a completely new layout to use as an placeholder for example "partial_layout.xml". Then in your code first make a LayoutInflater with something like this:
LayoutInflater mInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
then try to get a fresh copy of this layout with something like this:
View convertView = mInflater.inflate(R.layout.partial_layout, null);
now put your current data to this view, and finally add this view to your content view.
If you create a list of views, you can still use XML by inflating it only when needed - just watch the lecture "the world of listView" in order to do it correctly. Using ListView is much more efficient than creating as many views as the number of items to show (for example, if there are 100 images to show, only those that are on screen need to be created).
Anyway, the xml can contain either FrameLayout or RelativeLayout as the root view, and you just put the 2 imageViews according to the right position you wish to have. You can use the UI designer for this, or edit the XML itself by adding margin-top for the larger image. also, make sure the larger image is written there before the small one.
as for the clicking, you can use setOnClickListener on the small imaveView.
BTW, if it's just images, you should probably use GridView instead of ListView.

Android: Is there any way to to cast ImageView to RelativeLayout

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

Compose button with layout

Is it possible to create a Button, whose background drawable is a layout?
For example, I would like to create a layout with some horizontal images and some textviews, and all the resulting view should be clickable all at once as a single button.
Would it be possible, and how?
Thank you.
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_nested_layout);
yourLayout.setOnclickListener(...);

How do I add FrameLayout and ImageViews within it Programmatically?

There are plenty of similar questions asked in SO, but then also, please take time to read my question.
I need to create a UI programmatically that would have multiple ImageView, all positioned at different location of the screen and having their associated click events (its a game app). I found that FrameLayout is appropriate choice, where I can set margins on my own and have it positioned at desired location.
Now, I'm confused whether to have FrameLayout for every single ImageView I create, or to keep single FrameLayout and add all ImageViews within it, but set each imageview at different position.
In either of the case, how can I add FrameLayout, and ImageView within it, programmatically and also set its margin such that it can be placed anywhere on the screen.
Note that my main canvas, which will carry all these ImageViews has background, and the canvas is a LinearLayout set via XML, so my onCreate() already has setContentView(R.layout.game_canvas);, and I'd be using addContentView() to add additional views, but this method too accepts LayoutParams object as it second parameter, so what exactly should I set for this, when I add my FrameLayouts using this method?
My question might be confusing itself, so please let me know if I need to elaborate.
FrameLayouts are designed to only hold one View, so this isn't really the appropriate choice.
Use a RelativeLayout to hold all of your ImageViews. You can position each ImageView by setting the margins in their LayoutParams.
E.g. The following code would place an ImageView at coordinates 50,50:
RelativeLayout imgLayout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.an_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(50, 50, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imgLayout.addView(iv, lp);
You can then add this RelativeLayout to your main LinearLayout using its addView() method.

Categories

Resources