Wednesday, 18 January 2017

How to Use ImageView in Android Studio

1 comment
In Android, We can use  android.widget.ImageView  class to display image.
1. Add image to Resource 
 Put image file to res/drawable folder
Now remember your image file name, if your filename is android3.png, then image ID is android3.



2. Add ImageView to Activity.
Create a activity res/layout/activity_main.xml 
And just add an ImageView 


<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <imageview android:id="@+id/imageView" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="47dp" android:layout_width="match_parent"></imageview> </relativelayout>
3. Now Third Step is to edit Java file So

 open MainActivity.java

Create Reference and object of ImageView.

so We just created ImageView img;

and last step is to set your resource id using this method

 img.setImageResource("R.dawable.android3");

Now to Full code of MainActivity.java is
package in.flipmobi.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img= (ImageView) findViewById(R.id.imageView); img.setImageResource(R.drawable.a); }
}

Finish

Comment below if you have problem

1 comment :