Tuesday, March 26, 2019

How to display image in android app.

How to display image in android app.

Images in android using ImageView.
Displaying images in android app is the most amazing thing to do. In this documentation, we are going to display some images using ImageView component.

Step1:- Create a new project and name it.
Step2:- Open up your XML layout file as in my case my main work is done in activity_main.xml.
Step3:- Copy and paste the below code as given.

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:orientation="horizontal"    
tools:context=".MainActivity">

 <RelativeLayout    
    android:layout_width="wrap_content"    
    android:layout_height="wrap_content">
    <ImageView        
        android:id="@+id/image1"        
        android:layout_width="fill_parent"        
        android:src="@drawable/logo2"        
        android:layout_height="wrap_content" />

    <ImageView        
      android:id="@+id/img2"        
      android:layout_width="fill_parent"        
      android:layout_marginTop="5dp"        
      android:src="@drawable/logo6"        
      android:layout_below="@id/image1"        
      android:layout_height="wrap_content" />
 
 </RelativeLayout>
</LinearLayout>


Step4:- Open up your JAVA file as named MainActivity.java.
Step5:- Copy and paste the below code as given.


import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView img1 = (ImageView)findViewById(R.id.image1);




    img1.setPadding(15,15,15,15);
    img1.setBackgroundColor(Color.DKGRAY);
    img1.setScaleType(ImageView.ScaleType.FIT_XY);


        ImageView img2 = (ImageView)findViewById(R.id.img2);

        img2.setPadding(15,15,15,15);
        img2.setBackgroundColor(Color.DKGRAY);
        img2.setScaleType(ImageView.ScaleType.FIT_XY);



    }
}

Step6:- Now run and test your app.

As you can see, we have used two images, one image resource is added in design mode were as another image resource is added programmatically.

Remember to have two image file in the drawable folder before adding.
In the above code in the MainActivity.java file, we have a method setPadding(), this method is used to provide some padding to the image as TOP, BOTTOM, LEFT and RIGHT.
Method setBackgroundColor() , provides a background color to the image.
Method setScaleType(),this method provide to scale your image as FIT_XY.

Thank you for your support if you have any doubt then give a comment below and also do not forget to subscribe to channel Android Buddy

No comments:

Post a Comment