All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Add Image In Android Studio

Last Updated : Mar 11, 2024

How To Add Image In Android Studio

In this article we will show you the solution of how to add image in android studio, the most crucial folder again for Android applications is this component location,which houses all of the non-code sources such as graphics,XML layouts, and UI strings.

The drawable folder,layout folder,mipmap folder,values folder, etc. are all located under the res folder on Android Studio.

The drawable folder among them contains the various picture types used in the application's development.

Each of the photos should be uploaded to a drawable folder in order to create the application.

Images are used by Android apps to provide more user-friendly behavior and functionality.

Use the ImageView class inside an Android application to display an image file.

The various screen sizes through Android devices make image files easy to use but difficult to manipulate.

Android has improved this same best UI design widgets, allowing us to develop aesthetically pleasing UI-based applications.

ImageView offers a range of setup options to handle different scale types. With scale type parameters, an image's borders can be resized so fit within the imageview's confines.

There are several setup properties for scaleTypes, including centre, centre crop, fit xy, fitStart, etc.For complete information, see our ScaleType lesson.

Step By Step Guide On How To Add Image In Android Studio :-

package com.example.sairamkrishna.myapplication;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
   Button b1, b2, b3;
   ImageView im;
   private Bitmap bmp;
   private Bitmap operation;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);
      b3 = (Button) findViewById(R.id.button3);
      im = (ImageView) findViewById(R.id.imageView);
      BitmapDrawable abmp = (BitmapDrawable) im.getDrawable();
      bmp = abmp.getBitmap();
   }
public void gray(View view) {
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      double red = 0.33;
      double green = 0.59;
      double blue = 0.11;
      for (int i = 0; i < bmp.getWidth(); i++) {
         for (int j = 0; j < bmp.getHeight(); j++) {
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            r = (int) red * r;
            g = (int) green * g;
            b = (int) blue * b;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   public void bright(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig());
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            r = 100 + r;
            g = 100 + g;
            b = 100 + b;
            alpha = 100 + alpha;
            operation.setPixel(i, j, Color.argb(alpha, r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
public void dark(View view){
      operation= Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            r = r - 50;
            g = g - 50;
            b = b - 50;
            alpha = alpha -50;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   public void gama(View view) {
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            r = r + 150;
            g = 0;
            b = 0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
public void green(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      for(int i=0; <bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            r = 0;
            g = g+150;
            b = 0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
   public void blue(View view){
      operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            r = 0;
            g = 0;
             b = b+150;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      im.setImageBitmap(operation);
   }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Image Effects" />
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true"
      android:src="@drawable/abc"/>
<Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Gray"
      android:onClick="gray"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginBottom="97dp" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="dark"
      android:onClick="dark"
      android:id="@+id/button2"
      android:layout_alignBottom="@+id/button"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Bright"
      android:onClick="bright"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_centerHorizontal="true" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red"
      android:onClick="gama"
      android:id="@+id/button4"
      android:layout_below="@+id/button3"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Green"
      android:onClick="green"
      android:id="@+id/button5"
      android:layout_alignTop="@+id/button4"
      android:layout_alignLeft="@+id/button3"
      android:layout_alignStart="@+id/button3" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="blue"
      android:onClick="blue"
      android:id="@+id/button6"
      android:layout_below="@+id/button2"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView" />
</RelativeLayout>
  1. If you're not familiar with Android Studio, you can find out how to create a new project by visiting the How to Create and Start a New Project in Android Studio page. If Java is the programming language you want to use, make sure it is selected.
  2. The first step is to create three types of public classes: compact, medium, and expanded.
  3. The main activity is then to create the class.
  4. After that, replace with a known container to which you can safely add a view without affecting the layout or the view and without being replaced.
  5. Then, to hook, we add a utility view to the container.
  6. Then we included View. The configuration was altered. All activities, including those that do not handle configuration changes, require this.
  7. We are also unable to use Activity. Because there are some cases where that will not be called when the configuration changes.
  8. You can edit the activity_main.xml file by going to the app > res > layout > activity_main.xml file and adding the below code. An example of an activity_main.xml file can be found below. The activity_main.xml file should contain a simple ImageButton.
  9. You can create Drawable Resource Files by going to the app's menu, selecting Res > Drawbe, right-clicking and choosing New > Drawable Resource File, then naming the files custom_button1, custom_button2, custom_button3, and applying them one by one to ImageButton.

Conclusion :-

Android allows for image alteration by adding numerous effects to the photos.

You may easily utilize image processing techniques to create particular types of effects to images.

A change in brightness,blackness, or grayscale could be one of the effects.

The Bitmap class on Android is used to manage photos. You can find this in Android.graphics.bitmap.

Bitmaps can be created using a variety of methods. The images are converted into a bitmap using the imageView.

I hope this article on how to add image in android studio helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪