Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load an imageView from xml [closed]

I want to use a command like this:

ImageView image = (ImageView) findViewById(R.id.x2);

where should I put in the file main.xml the following piece of code?

<ImageView 
    android:id="@+id/x2"
    android:src="@drawable/book"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
like image 497
Antilope Avatar asked Jun 12 '12 12:06

Antilope


People also ask

How do you load an image from a file and set on an ImageView?

If you're working with an Android application, this source code works as a way to load an image from a file: Bitmap bitmap = BitmapFactory. decodeFile(pathToPicture);

How can I bring ImageView in front?

Just one single method will let you to bring your view to the front and the magic words are : myView. bringToFront(); “.

Is ImageView clickable?

For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

What is difference between ImageView and Imagebutton?

There's no differences, except default style.


1 Answers

Dheeresh Singh is ok. main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <ImageView 
          android:id="@+id/x2"
          android:src="@drawable/book"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
</LinearLayout>

If you want to position the image in a different place, you can modify different attributes

 <ImageView 
              android:id="@+id/x2"
              android:src="@drawable/book"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="42dp"  <!--for example  -->
              android:layout_marginLeft="25dp"
              android:layout_marginRight="25dp" 
              android:layout_marginTop="25dp"/>

For more information about this, look at the android tutorials

http://developer.android.com/reference/android/widget/ImageView.html (for ImageView attributes) http://developer.android.com/resources/tutorials/views/index.html (for view info)

Excuse me for my english. Good Luck!

like image 190
SolArabehety Avatar answered Sep 20 '22 16:09

SolArabehety