Manufacturing Unique R.id Values

If you have been developing in Android for any length of time, you’ll most likely be aware that one of the most useful portions the resource framework is the fact that Views and other resources can be given an android:id tag in their XML declaration, and Android will make sure to compile all the ids into unique integer values that can be accessed from Java code by calling R.id.. This allows us to reduce the number of event listeners an application requires because it can uniquely identify the view that triggered the event by matching its id. And, of course, we prefer using integer ids for this as opposed to matching Objects or String tags because the ids fit nicely into switch statements!

For example, a layout with two buttons, both pointing at the same action method:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/firstButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click Me!" android:onClick="onButtonClick" /> <Button android:id="@+id/secondButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click Me Too!" android:onClick="onButtonClick" /> </LinearLayout>

We can determine which button was pressed in the Activity by checking the sender’s id:

public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onButtonClick(View v) { switch(v.getId()) { case R.id.firstButton: //Handle first button click break; case R.id.secondButton: //Handle second button click break; default: break; } } }

This is not breaking new, and is just one of many useful properties of android:id…
But what if we didn’t use XML to create our layouts/menus/etc.? What if we created a series of Views in Java code? Are we destined to go wanting without the assistance of the unique R.id generator? Certainly not!

Making id Elements Yourself

To reserve a set of ids for your own use in application, simply create an ids.xml file in the res/values directory of your project. The syntax coupled with creating this file is pretty self-explanatory

res/values/ids.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="firstButton"/> <item type="id" name="secondButton"/> <item type="id" name="thirdButton"/> </resources>

Now let’s take these fresh ids and add them to a series Buttons created in a dynamic layout:

public class MyActivity extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); Button button; button = new Button(this); button.setOnClickListener(this); button.setText("Click Me First!"); button.setId(R.id.firstButton); layout.addView(button); button = new Button(this); button.setOnClickListener(this); button.setText("Click Me Next!"); button.setId(R.id.secondButton); layout.addView(button); button = new Button(this); button.setOnClickListener(this); button.setText("Then Click Me!"); button.setId(R.id.thirdButton); layout.addView(button); setContentView(layout); } public void onClick(View v) { switch(v.getId()) { case R.id.firstButton: //Handle first button click break; case R.id.secondButton: //Handle second button click break; case R.id.thirdButton: //Handle third button click break; default: break; } } }

That’s it! Just another practical application of a little known resource type in the Android framework.

Note: you can access this website: http://wiresareobsolete.com/wordpress/2011/04/manufacturing-ids/

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android 开发技术周报 Issue#292
  2. Android音频系统之USB设备通路(Android 5.
  3. android 2.2数据连接过程
  4. Android - 主要的UI元素。
  5. [Android]从新旧API看android代码演进
  6. Android获取外置SD卡读写路径
  7. Android Studio 中关于NDK编译及jni head
  8. Android之AsyncTask的内存泄露问题
  9. Android系统源码目录解析
  10. Android软键盘处理开发规范