Close
Topic : Introduction to Drawable Resources:



Drawable Resources-Android : infobrother

Drawable Resources:

A Drawable resource is a general concept for a graphic that can be drawn to the screen. Most often we will deal with Drawable as the type of resources retrieved for drawing things to the screen.

Every Drawable is stored as Individual files in one of the Resources/drawable folders. The simplest form of drawable is a graphical file known as bitmap. There are several different types of drawable as shown in the given table.


drawableDescription
Bitmap:A simple Graphic file - PNG, JPG, or gif image.
Nine Patch:A PNG file with stretchable regions to allow resizing based on content.
Layers:A compound drawable, which draws multiple underlying drawable on top of each other.
State:A compound drawable that selects one of a set of drawable based on its state. (Example: Use different Image, when button is pressed.)
Level:A compound drawable that selects one of set of drawable based on its level.
Scale:A compound drawable that changes the size of another drawable based on its current level state.
Transition:A compound Drawable that can cross-fade between two drawable resources.
Insert:A Compound drawable that inserts another drawable by a specified distance.
Clip:A compound Drawable that clips another drawable based on this drawable current level value.
Shape:A Compound drawable that defines a geometric shape, including color and gradients.


Let's have a simple example to know how we can use drawable resources in our Application. we will add two images in Drawable resources, and then using button, we will show these two images on our screen. follow the given steps to make the project.

This is just an Introduction to Drawable resources, we will discuss it deeply in our upcoming tutorials.


  • Step-1: Open New Project:

    » Open Visual studio, and create new Project.
    » Select Blank Android app from the template.
    » Give it a name, browse your location and click ok.



step-2: edit main.axml file: infobrother
  • Step-2: Edit Main.axml File:

    » From Solution Explorer Tab, Open Resources » Layout » Main.axml:
    » Using Toolbox, Drag and Drop Button in Designer Window.
    » Using Toolbox, Drag and Drop ImageView in Designer Window.
    » Click at Source button to get into the XML coding file.



step-3: Add Some pictures in Drawable resource: infobrother
  • Step-3: Includes Files in Drawable Resource:

    Let's use Drawable resources in our project. Include some pictures in Drawable directory, and later, we will use these picture in our project. Follow the given steps to add picture.
    » From Resources, Right click on drawable and add existing items, browse your picture and add them.
    » I added two pictures in my project. i will use these two pictures to make the project.



In Main.axml file, Compare the given code with your code and if there is any line of code missing in your code, add it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
          android:text="ChangeImage"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/button1" />
    <ImageView
          android:src="@drawable/logo"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/imageView1"
          android:scaleType="fitCenter"
          android:layout_marginBottom="0.0dp" />
</LinearLayout>

NOTE: Drawable are referred to in XML via @drawable/filename where, filename is the filename without file extension. For example, if we want to access the Resource/drawable/logo.png drawable, we would use @drawable/logo to access it.


  • Step-4: Edit MainActivity.cs file:

    Finally, we need to access these pictures in our project using MainActivity.cs file. Look at the given steps to understand the code.
    » From Solution Explorer tab, Open MainActivity.css file to edit.
    » We need to Create the Object of Button class.


    Button button = FindViewById<Button>(Resource.Id.button1);
    


    » On Button Click, the program will change the Image using this code.


    button.Click += delegate
    {
           var imageview = FindViewById<ImageView>(Resource.Id.imageView1);
          imageview.SetImageResource(Resource.Drawable.logo2);
    };
    


    » The complete code is given below.



using Android.App;
using Android.Widget;
using Android.OS;
 
namespace resources_
{
    [Activity(Label = "resources_", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
              base.OnCreate(savedInstanceState);
 
              // Set our view from the "main" layout resource
              SetContentView(Resource.Layout.Main);
 
              Button button = FindViewById<Button>(Resource.Id.button1);
 
              button.Click += delegate
              {
                  var imageview = FindViewById<ImageView>(Resource.Id.imageView1);
                  imageview.SetImageResource(Resource.Drawable.logo2);
              };
        }
    }
}


step-5: Run the Project: infobrother
  • Step-5: Run Program:

    Our Project as been completed, now its time to run the program in our emulator. run the program, you will see your first image on your emulator. click the button, and you'll see another image in your emulator.



In this tutorial, we introduced you the concept of Drawable resources. We will discuss this topic deeply, in our upcoming tutorials.















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement