Difference between revisions of "ANDROID: Resources"

From OnnoWiki
Jump to navigation Jump to search
Line 125: Line 125:
  
 
Consider next example where res/values/strings.xml has following definition −
 
Consider next example where res/values/strings.xml has following definition −
 +
 +
Pertimbangkan contoh berikutnya di mana res/values/strings.xml memiliki definisi berikut
  
 
  <?xml version="1.0" encoding="utf-8"?>
 
  <?xml version="1.0" encoding="utf-8"?>
Line 132: Line 134:
  
 
Now you can set the text on a TextView object with ID msg using a resource ID as follows −
 
Now you can set the text on a TextView object with ID msg using a resource ID as follows −
 +
 +
Sekarang Anda dapat mengatur teks pada objek TextView dengan pesan ID menggunakan ID sumber daya sebagai berikut
 +
  
 
  TextView msgTextView = (TextView) findViewById(R.id.msg);
 
  TextView msgTextView = (TextView) findViewById(R.id.msg);

Revision as of 06:46, 14 July 2022

Masih banyak lagi item yang kita gunakan untuk membangun aplikasi Android yang baik. Selain pengkodean untuk aplikasi, kita menangani berbagai resource lain seperti konten statis yang digunakan kode kita, seperti bitmap, warna, definisi tata letak, string interface pengguna, instruksi animasi, dan banyak lagi. Resource ini selalu dipelihara secara terpisah di berbagai sub-direktori di bawah direktori res/ proyek.

Tutorial ini akan menjelaskan bagaimana kita dapat mengatur sumber daya aplikasi, menentukan sumber daya alternatif dan mengaksesnya di aplikasi.

Pengaturan resource di Android Studio

MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java  
      res/
         drawable/  
            icon.png  
         layout/  
            activity_main.xml
            info.xml
         values/  
            strings.xml 


Caption text
Sr.No. Directory Resource Type
1 anim/ XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class.
2 color/ XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.
3 drawable/ Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class.
4 layout/ XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class.
5 menu/ XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class.
6 raw/ Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files.
7 values/ XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory −
  • arrays.xml for resource arrays, and accessed from the R.array class.
  • integers.xml for resource integers, and accessed from the R.integer class.
  • bools.xml for resource boolean, and accessed from the R.bool class.
  • colors.xml for color values, and accessed from the R.color class.
  • dimens.xml for dimension values, and accessed from the R.dimen class.
  • strings.xml for string values, and accessed from the R.string class.
  • styles.xml for styles, and accessed from the R.style class.
8 xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time.

Alternatif Resources

Aplikasi kita harus menyediakan alternatif resources untuk mendukung konfigurasi perangkat tertentu. Misalnya, kita harus menyertakan alternatif drawable resources (i.e. images) untuk resolusi layar yang berbeda dan alternatif string resources untuk bahasa yang berbeda. Saat runtime, Android mendeteksi konfigurasi perangkat saat ini dan memuat resources yang sesuai untuk aplikasi kita.

Untuk menentukan alternatif konfigurasi khusus untuk satu set resources, ikuti langkah-langkah berikut -

  • Buat direktori baru di res/ bernama dalam bentuk <resources_name>-<config_qualifier>. Di sini resources_name akan menjadi salah satu resources yang disebutkan dalam tabel di atas, seperti tata letak, dapat digambar, dll. Kualifikasi akan menentukan konfigurasi individual untuk resources mana akan digunakan. Kita dapat memeriksa dokumentasi resmi untuk daftar lengkap qualifier untuk berbagai jenis resources.
  • Simpan masing-masing alternatif resources di direktori baru ini. File resource harus dinamai sama persis dengan file resource default seperti yang ditunjukkan pada contoh di bawah ini, tetapi file ini akan memiliki konten khusus untuk alternatifnya. Misalnya meskipun nama file gambar akan sama tetapi untuk layar resolusi tinggi, resolusi yang di hasilkan akan secara automatis juga tinggi.

Di bawah ini adalah contoh yang menentukan gambar untuk layar default dan gambar alternatif untuk layar resolusi tinggi.

MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java   
      res/
         drawable/  
            icon.png
            background.png
         drawable-hdpi/  
            icon.png
            background.png  
         layout/  
            activity_main.xml
            info.xml
         values/  
            strings.xml

Di bawah ini adalah contoh lain yang menentukan tata letak untuk bahasa default dan tata letak alternatif untuk bahasa Arab.

MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java   
      res/
         drawable/  
            icon.png
            background.png
         drawable-hdpi/  
            icon.png
            background.png  
         layout/  
            activity_main.xml
            info.xml
         layout-ar/
            main.xml
         values/  
            strings.xml

Mengakses Resource

Selama pengembangan aplikasi, kita perlu mengakses resource yang ditentukan baik dalam source code, atau dalam file layout XML. Bagian berikut menjelaskan cara mengakses resource di kedua skenario

Mengakses Resources di Code

Saat aplikasi Android dikompilasi, R class akan dibuat, yang berisi resource ID untuk semua resource yang tersedia di direktori res/ . Kita dapat menggunakan R class untuk mengakses resource tersebut menggunakan sub-direktori dan nama resource atau resource ID secara langsung.−

Contoh

Untuk mengakses res/drawable/myimage.png dan mengatur ImageView, kita dapat menggunakan kode berikut

ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

Di sini baris pertama code menggunakan R.id.myimageview untuk mendapatkan ImageView didefinisikan dengan id myimageview dalam file Layout. Baris code kedua menggunakan R.drawable.myimage untuk mendapatkan gambar dengan nama myimage yang tersedia di sub-direktori drawable di bawah /res.


Contoh

Consider next example where res/values/strings.xml has following definition −

Pertimbangkan contoh berikutnya di mana res/values/strings.xml memiliki definisi berikut

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string  name="hello">Hello, World!</string>
</resources>

Now you can set the text on a TextView object with ID msg using a resource ID as follows −

Sekarang Anda dapat mengatur teks pada objek TextView dengan pesan ID menggunakan ID sumber daya sebagai berikut


TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);

Example

Consider a layout res/layout/activity_main.xml with the following definition −

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical" >
   
   <TextView android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a TextView" /> 

   <Button android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a Button" />     
</LinearLayout>

This application code will load this layout for an Activity, in the onCreate() method as follows −

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
}

Accessing Resources in XML

Consider the following resource XML res/values/strings.xml file that includes a color resource and a string resource −

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <string name="hello">Hello!</string>
</resources>

Now you can use these resources in the following layout file to set the text color and text string as follows −

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:textColor="@color/opaque_red"
   android:text="@string/hello" />

Now if you will go through previous chapter once again where I have explained Hello World! example, and I'm sure you will have better understanding on all the concepts explained in this chapter. So I highly recommend to check previous chapter for working example and check how I have used various resources at very basic level.

Referensi