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.
drawable | Description |
---|---|
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.
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>
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);
};
}
}
}
In this tutorial, we introduced you the concept of Drawable resources. We will discuss this topic deeply, in our upcoming tutorials.