How to insert an image in the linear layout? - android

I just wanted to insert an image in linear layout similar to this
Can anyone help me with this???

You are probably looking for ImageButton since your picture shows something similar to that.
Alternatively, take a look at ImageView

set layout's orientation to horizontal, then add 4 images in xml, one after another, add ids like image1, image2 etc, then call them in your onCreate like ImageView image
ImageView image1 = (ImageView) findViewById(R.id.image1)
and so on, then you can either set "src" attribute in yoru xml or say image1.setImageBitmap or any other method available in ImageView class to set the image you want from your res/drawable/

you can use ImageButton in horizontal LinearLayout
make background of the ImageButton #null
then put the margins between them

Related

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

Align a layout top of a image that is centered

I have a relative layout, inside of it is a ImageView that is centered in its parent.
I want to add a share up to the image. If I put this attribute android:layout_alignTop="#+id/myImage" the button will appear over the image. I want to be upper than this.
Use
android:layout_below="#+id/shareId"
in your <ImageView> tag. Or
android:layout_above="#+id/myImage"
in your share's View tag depending on what works better for you. If this doesn't fix it then please post the xml and a picture of what you want/have if possible.
RelativeLayout Docs shows the different properties that can be used.

Android, Specify what View should be shrinked if no space

I've got next problem.
There is ImageView and button panel below it. I want ImageView to be as large as it needs, but if there is no enough space button panel should be present on screen and Imageview should be shrinked. What is the best solution for that?
Have a look to the android:layout_weight attribute.
What does android:layout_weight mean?
In case there is only 2 views in one layout (ie. Imageview and button) you can put both view in linearlayout and give value 1 for imageview weight layout (and doesn't give any value for the button)

How to make a gallery with Image and text for android

I wanna make a Gallery, with a picture in the background and a textfield with invisible background... So I can see the text and the Image behind.
I already made the Gallery but i cant figure out how to put text in the foreground.
You create a custom adapter to use for your gallery view, see Hello Gallery tutorial
In the method getView() you have two different ways to achieve the same result:
A. Make an ImageView and a TextView and set the image and text:
ImageView iv = new ImageView(context); //and so on, remember to set the LayoutParams etc to fit your design.
Make a RelativeLayout and add the ImageView and TextView to it
Return the RelativeLayout.
B. Declare a layout in xml, with a RelativeLayout containing a TextView and an ImageView
In getView, inflate this layout, and using
inflatedLayout.findViewById(R.id.myTextView); //and so on
set the text and image and return the layout.

Layout for android

I need to have a layout for my widget, but I don't know how to arrage my images on widget screen.
I need to have 4 small Images on first row
then 2 Images on 2nd row
1 Image on 3rd row
and 1 Image on 4th row
Could you please someone create the xml file ?
You could use TableRows within a TableLayout.
Refer Hello TableLayout for an example.
Using the hierarchyviewer.bat in your sdk|tools folder along the way might be a very good idea as well.
LinearLayout(vertical)
LinearLayout(horizontal)
ImageView
ImageView
ImageView
ImageView
LinearLayout(horizontal)
ImageView
ImageView
ImageView
ImageView
Okay?

Categories

Resources