Android Lesson 3 part 2: work with ImageView


Example with ImageView.setImageResource

File conntent_main.xml:

<ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        />

File MainActivity.java

After: public class MainActivity extends AppCompatActivity {

add

public ImageView imgview;

And in method onCreate add

 imgview = (ImageView) findViewById(R.id.imgview);
        if (!imgview.isInEditMode()) {
            imgview.setImageResource(R.drawable.logo);
        }

We will get the result:

imageview setimagesource

Example with ImageView.setScaleType

File content_main.xml

<ImageView
        android:id="@+id/imgview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/logo"
        android:layout_marginTop="70dp"
        />

File MainActivity.java
And in method onCreate add

 imgview = (ImageView) findViewById(R.id.imgview);
        if (!imgview.isInEditMode()) {
            imgview.setScaleType(ImageView.ScaleType.FIT_START);
        }

The result we will get:
imageview setscaletype

Leave a Reply